Astro info
❯ astro info
Astro version v2.10.14
Package manager npm
Platform linux
Architecture x64
Adapter @astrojs/node
Integrations None or couldn't determine.
What browser are you using?
Chrome
Describe the Bug
I'm using Astro in SSR mode with nodejs integration.
I have middleware with onRequest handler where I'm trying to get html markup from the response:
const html = await response.text();.
If a page does exist, it will return HTML markup, but if it does not exist, this middleware will not be executed, and 404 page will be shown. If somepage.astro returns new Response(null, {status: 404}), the middleware will get executed, but the response.text() will return an empty string.
Moreover, if I reconstruct the response in the middleware in this way:
const response = await next();
const html = await response.text();
return new Response(html, response);
200 pages will be displayed correctly, but the somepage.astro will return 404 page with text/plain to the browser.
In the provided stackblitz you can try to visit:
/awd - this page does not exists and you will get 404
/somepage - returns text/plain
What's the expected result?
- The middleware should get executed if a page is not found
- the response from
next() should return the 404.astro content with a header Content-Type: text/html.
Also, I got into my thought, what if I want to return 404 response from middleware before it gets to next(); for some "unknown" reason? It contradicts the behavior I described above.
Link to Minimal Reproducible Example
https://stackblitz.com/edit/github-oiqdwp?file=astro.config.mjs,src%2Fpages%2F404.astro,src%2Fpages%2Fnotfound.astro,src%2Fmiddleware.js
Participation
Astro info
What browser are you using?
Chrome
Describe the Bug
I'm using Astro in SSR mode with nodejs integration.
I have middleware with
onRequesthandler where I'm trying to get html markup from the response:const html = await response.text();.If a page does exist, it will return HTML markup, but if it does not exist, this middleware will not be executed, and 404 page will be shown. If
somepage.astroreturnsnew Response(null, {status: 404}), the middleware will get executed, but theresponse.text()will return an empty string.Moreover, if I reconstruct the response in the middleware in this way:
200 pages will be displayed correctly, but the
somepage.astrowill return 404 page with text/plain to the browser.In the provided stackblitz you can try to visit:
/awd- this page does not exists and you will get 404/somepage- returns text/plainWhat's the expected result?
next()should return the404.astrocontent with a headerContent-Type: text/html.Also, I got into my thought, what if I want to return 404 response from middleware before it gets to
next();for some "unknown" reason? It contradicts the behavior I described above.Link to Minimal Reproducible Example
https://stackblitz.com/edit/github-oiqdwp?file=astro.config.mjs,src%2Fpages%2F404.astro,src%2Fpages%2Fnotfound.astro,src%2Fmiddleware.js
Participation