Skip to content

Inconsistent 404 handling in onRequest middleware for SSR mode with nodejs integration #8273

@artmsv

Description

@artmsv

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

  • I am willing to submit a pull request for this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    - P3: minor bugAn edge case that only affects very specific usage (priority)feat: error pagesRelated to 404 and 500 handling (scope)feat: middlewareRelated to middleware (scope)pkg: nodeRelated to Node adapter (scope)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions