使用CF Worker的修改版本,分页问题已得到解决。我在cloudflare post 哪一个指向这个workers script
addEventListener(\'fetch\', event => {
event.respondWith(handleRequest(event.request))
})
// keep track of all our blog endpoints here
const config = {
hostname: "blog.domain.com",
mainhostname: "www.domain.com",
targetSubdirectory: "/blog",
assetsPathnames: ["/wp-content/", "/wp-includes/"]
}
async function handleRequest(request) {
const parsedUrl = new URL(request.url)
const requestMatches = match => new RegExp(match).test(parsedUrl.pathname)
/**
* Best practice is to only assign new properties on the request
* object (i.e. RequestInit props) through either a method or the constructor
*/
let newRequestInit = {
}
// Change URL
let url = parsedUrl.href.replace(parsedUrl.hostname + config.targetSubdirectory, config.hostname)
if (url.includes(\'/2020/06/01/some-post\') && url[url.length-1] !== \'/\') {
url = url + \'/\'
}
// Change just the host
url = new URL(url)
// Best practice is to always use the original request to construct the new request
// thereby cloning all the attributes, applying the URL also requires a constructor
// since once a Request has been constructed, its URL is immutable.
const newRequest = new Request(url, new Request(request, newRequestInit))
try {
return await fetch(newRequest)
} catch (e) {
return new Response(JSON.stringify({ error: e.message }), { status: 500 })
}
}
另外,有一行代码可以修复URL,但没有斜杠,这对于开箱即用的脚本不起作用,但我们应该能够修改js代码来处理这种情况,而无需对单个URL进行硬编码