Metabeast
04/02/2022, 3:51 PMWalshy | Pages
04/02/2022, 3:56 PMhanpolo
04/03/2022, 3:25 AMMetabeast
04/03/2022, 12:36 PMjs
router.get('/callback', () => {
const res = fetch('url').then(res => res.json());
new Response(index, {
myVariable: res
})
})
html
<div>
<h2>${myVariable}</h2>
</div>
Isaac McFadyen | YYZ01
04/03/2022, 2:02 PMIsaac McFadyen | YYZ01
04/03/2022, 2:02 PMcvejic092
04/04/2022, 10:10 AMpatrickheneise
04/04/2022, 2:36 PMrequire('fs')
hanpolo
04/05/2022, 4:13 AMShahzaib Sheikh
04/05/2022, 6:57 AMalinador
04/05/2022, 12:07 PMsimpson
04/05/2022, 4:04 PMdis01
04/06/2022, 12:30 AMIsaac McFadyen | YYZ01
04/06/2022, 1:31 AMBytesource
04/06/2022, 3:10 AM404.html
to be present in the root folder. However, my error file is located at /error/index.html
.Bytesource
04/06/2022, 3:13 AMjs
// _middleware.ts
const pageNotFoundHandler = async ({ request, next }) => {
const response = await next();
if (response.status === 404) {
// Fetch 404 error page from where the Kirby static site generator puts it.
const host = new URL(request.url).host
const errorPage = host + "/error/";
const notFoundResponse = await fetch(errorPage);
return notFoundResponse;
}
else {
// Page found, just return response.
return response;
}
}
export const onRequestGet = [ pageNotFoundHandler ];
The problem is that this function is never called because whatever URL I type in, I always get a 200 status code back:
GET /contactggggggggggggg 200 OK (9.58ms)
Does anyone know why that is?kian
04/06/2022, 8:11 AMalinador
04/06/2022, 9:15 AMBytesource
04/06/2022, 10:47 AM/error
should be returned. Instead, the home page is returned (which Cloudflare Pages does by default if 404.html
is not present in the root folder).
So, it seems, the middleware isn't invoked at all.JustinNoel
04/06/2022, 11:28 AMChewey
04/07/2022, 2:31 AMTransforming async functions to the configured target environment ("es5") is not supported yet
on the final stage
However ive done no config changes (Havn't deployed in a couple weeks where it worked fine)
The docs examples seem to use await which imply its fine and also mention you're unable to return a promise
So what's the correct way to use fetch within a pages worker now?Chewey
04/07/2022, 2:33 AM14:18:17.383 The target environment was set to "es5" here:
14:18:17.383
14:18:17.383 ../../../buildhome/repo/tsconfig.json:3:14:
14:18:17.383 3 │ "target": "es5",
14:18:17.383 ╵ ~~~~~
Which seems to be outside of my codebaseChewey
04/07/2022, 2:33 AMChewey
04/07/2022, 2:38 AMBytesource
04/07/2022, 12:04 PM404.html
is present in the root folder, does Cloudflare Pages directly fetch the home page? I'm asking because it seems that in case of a 404, _middleware
is not called at all.xHyroM
04/08/2022, 4:36 AMEvаn
04/08/2022, 7:30 AMfetch
.maximillian
04/08/2022, 8:55 AMEvаn
04/08/2022, 8:58 AMmaximillian
04/08/2022, 9:02 AM