Skip to content

Middleware isn't invoked before rendering the default 404 page #8244

@ohansemmanuel

Description

@ohansemmanuel

Astro info

Astro version            v2.10.14
Package manager          npm
Platform                 darwin
Architecture             arm64
Adapter                  Couldn't determine.
Integrations             None or couldn't determine.

What browser are you using?

Chrome

Describe the Bug

Middleware isn't invoked before rendering the default 404 page.

NB: I'm uncertain if this is a bug or the intended behaviour

  1. Consider the basic middleware below:
import { defineMiddleware } from "astro/middleware";

export const onRequest = defineMiddleware((context, next) => {
  console.log("in middleware");
  return next();
});
  1. Visit a random page route: e.g., /uhoihoijoijoijoijoi

Result: The middleware is never invoked. No logs.

  1. Add a custom 404.astro page
// src/pages/404.astro
---
console.log("404 page");
---

Now, the middleware will be invoked before rendering the custom 404.astro page.

#logs 
in middleware
404 page

What's the expected result?

Consistently invoke the middleware for both custom and default 404 pages.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-i9px2m?file=src%2Fmiddleware%2Findex.ts

Participation

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs triageIssue needs to be triaged

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions