Inconsistent 404 handling in onRequest
middleware for SSR mode with nodejs integration
#8273
Labels
- P3: minor bug
An edge case that only affects very specific usage (priority)
feat: error pages
Related to 404 and 500 handling (scope)
feat: middleware
Related to middleware (scope)
pkg: node
Related to Node adapter (scope)
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
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
returnsnew 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.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/plainWhat's the expected result?
next()
should return the404.astro
content 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
The text was updated successfully, but these errors were encountered: