From af1a4614eb6c8465c49940fa2243e49d1ee70358 Mon Sep 17 00:00:00 2001 From: Ming Di Leom <2809763-curben@users.noreply.gitlab.com> Date: Sun, 6 Oct 2024 10:53:03 +0000 Subject: [PATCH] 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 --- cf-images/index.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/cf-images/index.js b/cf-images/index.js index 5d7bf1c..08f3b45 100644 --- a/cf-images/index.js +++ b/cf-images/index.js @@ -72,7 +72,9 @@ export default { // Build a request that passes through request headers // 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, { headers: request.headers }) @@ -88,6 +90,7 @@ export default { response.headers.set('Vary', 'Accept') return response } else if (response.status === 404) { + const { readable, writable } = new TransformStream() // Custom 404 page const { status, statusText } = response @@ -95,16 +98,17 @@ export default { ...request.headers, Accept: 'text/html' }) - const page404 = new Request('https://curben.pages.dev/404', { + const page404 = new Request('https://curbengh.github.io/404', { headers: htmlHeader }) - response = await fetch(page404) - const html = await response.text() - return new Response(html, { + const res404 = await fetch(page404) + res404.body.pipeTo(writable) + + return new Response(readable, { status, statusText, headers: { - ...response.headers, + ...res404.headers, 'Cache-Control': 'no-cache', 'Content-Type': 'text/html; charset=utf-8' }