我正在尝试根据这篇文章在我的网站上建立一个博客(作为子目录/子页面)。所以,我的网站托管在这里,我希望这个子页面(/officiano) 呈现来自我的其他网站之一的内容 -这个自定义域甚至它的 Netlify 域。
我已经使用路由 .remotework2020.com/ 设置了一个可能的工作人员,并且工作人员的代码与帖子中的代码相同。我可以看到评估正确发生(通过控制台日志),但是,当我转到子目录时,它只会引发 404 错误。
任何人都可以帮助解决问题吗?
编辑:
在下面粘贴整个工人代码:
// keep track of all our blog endpoints here
const myBlog = {
hostname: "theremoteweekly.com",
targetSubdirectory: "/officiano",
assetsPathnames: ["/public/", "/assets/"]
}
async function handleRequest(request) {
// returns an empty string or a path if one exists
const formatPath = (url) => {
const pruned = url.pathname.split("/").filter(part => part)
return pruned && pruned.length > 1 ? `${pruned.join("/")}` : ""
}
const parsedUrl = new URL(request.url)
const requestMatches = match => new RegExp(match).test(parsedUrl.pathname)
// if its blog html, get it
if (requestMatches(myBlog.targetSubdirectory)) {
console.log("this is a request for a blog document", parsedUrl.pathname)
const targetPath = formatPath(parsedUrl)
console.log(targetPath)
console.log(`https://${myBlog.hostname}/${targetPath}`)
return fetch(`https://${myBlog.hostname}/${targetPath}`)
}
// if its blog assets, get them
if ([myBlog.assetsPathnames].some(requestMatches)) {
console.log("this is a request for blog assets", parsedUrl.pathname)
const assetUrl = request.url.replace(parsedUrl.hostname, myBlog.hostname);
return fetch(assetUrl)
}
console.log("this is a request to my root domain", parsedUrl.host, parsedUrl.pathname);
// if its not a request blog related stuff, do nothing
return fetch(request)
}
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request))
})