fix(cf-images): switch origin to github pages

stream response
  https://www.mickaelvieira.com/blog/2020/01/27/custom-404-page-with-cloudflare-workers.html
This commit is contained in:
Ming Di Leom 2024-10-06 10:53:03 +00:00
parent 5efe1f8867
commit af1a4614eb
No known key found for this signature in database
GPG Key ID: 32D3E28E96A695E8
1 changed files with 10 additions and 6 deletions

View File

@ -72,7 +72,9 @@ export default {
// Build a request that passes through request headers // Build a request that passes through request headers
// Images are stored on https://gitlab.com/curben/blog/-/tree/site // Images are stored on https://gitlab.com/curben/blog/-/tree/site
const imageURL = new URL(imgPath, 'https://curben.gitlab.io/') // curben.pages.dev returns 502 error
// curben.gitlab.io returns 403 error
const imageURL = new URL(imgPath, 'https://curbengh.github.io/')
const imageRequest = new Request(imageURL, { const imageRequest = new Request(imageURL, {
headers: request.headers headers: request.headers
}) })
@ -88,6 +90,7 @@ export default {
response.headers.set('Vary', 'Accept') response.headers.set('Vary', 'Accept')
return response return response
} else if (response.status === 404) { } else if (response.status === 404) {
const { readable, writable } = new TransformStream()
// Custom 404 page // Custom 404 page
const { status, statusText } = response const { status, statusText } = response
@ -95,16 +98,17 @@ export default {
...request.headers, ...request.headers,
Accept: 'text/html' Accept: 'text/html'
}) })
const page404 = new Request('https://curben.pages.dev/404', { const page404 = new Request('https://curbengh.github.io/404', {
headers: htmlHeader headers: htmlHeader
}) })
response = await fetch(page404) const res404 = await fetch(page404)
const html = await response.text() res404.body.pipeTo(writable)
return new Response(html, {
return new Response(readable, {
status, status,
statusText, statusText,
headers: { headers: {
...response.headers, ...res404.headers,
'Cache-Control': 'no-cache', 'Cache-Control': 'no-cache',
'Content-Type': 'text/html; charset=utf-8' 'Content-Type': 'text/html; charset=utf-8'
} }