Hi, Long time reader first time poster.
We have a site go live that is using Backblaze as our upstream origin. We have just migrated it from S3 and all is working great except everything in the Backblaze bucket is in a subdirectory. No problem we thought! Workers to the rescue! We are no very confused
e.g. a request to http://media.example.com/file.jpg should return the file from https://f000.backblazeb2.com//file/backblazebucket/file.jpg
The problem comes when we both set and donāt set the upstream hostname when querying the originā¦
- If we donāt set the hostname (code below) everything does work! And we are currently using this in production however when we run the code in the online editor we are getting 404 errors for every query.
- If we do set the hostname (uncomment line 43) then the code works in the editor however the CF-Cache-Status then becomes DYNAMIC which indicates to us that we arenāt caching as much as we want and caching is REALLY important to us.
Any advice hugely appreciated. Thank you in advance.
// Rewrites the URL's to access Backblaze in a subdirectory
// and remove Backblaze headers from the query being returned
// there's also an option of adding and changing headers
// Backblaze source
let sourceHost = "f000.backblazeb2.com"
let sourceDir = "/file/backblazebucket"
// Can be used to add as well as replace headers
// Note: Headers are also added via Page Rules
let changeHeaders = {
"X-Powered-By" : "Something Funny Here",
}
// Can be used to remove headers from origin source
let removeHeaders = [
"x-bz-file-name",
"x-bz-file-id",
"x-bz-content-sha1",
"X-Bz-Upload-Timestamp",
"x-bz-info-src_last_modified_millis",
"x-bz-info-fl-original-md5",
]
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
// Draft a new request to the origin for our files
let newURL = new URL(request.url)
newURL.pathname = sourceDir + newURL.pathname
// If I set the following line then it makes the error on the right go away
// but also stops CF cahing the origin! :-/
//Age: 252673
//Cache-Control: public, max-age=31536000
//Expires: Sat, 27 Nov 2021 14:10:02 GMT
//Vary: Accept-Encoding
//CF-Cache-Status: HIT
// ... turns into ...
//Cache-Control: max-age=0, no-cache, no-store
//CF-Cache-Status: DYNAMIC
//newURL.hostname = sourceHost
let newRequest = new Request(
newURL.toString(),
new Request(request),
)
// Make request
let response = await fetch(newRequest)
let newHdrs = new Headers(response.headers)
// Change any headers
Object.keys(changeHeaders).map(function(name, index) {
newHdrs.set(name, changeHeaders[name]);
})
// Remove any headers
removeHeaders.forEach(function(name){
newHdrs.delete(name)
})
// Return the page to the user
return new Response(response.body , {
status: response.status,
statusText: response.statusText,
headers: newHdrs
})
}