Skip to content

fix(pages): diagnose misplaced middleware - #1055

Merged
cpojer merged 7 commits into
mainfrom
fix/pages-middleware-diagnostic
Dec 6, 2026
Merged

cpojer merged 7 commits into
mainfrom
fix/pages-middleware-diagnostic

Conversation

@hyfdev

@hyfdev hyfdev commented Dec 6, 2026 •

Copy link
Copy Markdown
Contributor

Void Pages ignores a _middleware.ts placed under pages/, so a guard written there never runs and the pages it was meant to protect stay reachable.

#808 asks for routes/admin/_middleware.ts, and the same file under pages/ would silently do nothing.

Builds and the dev server now warn about a _middleware.ts, _middleware.mts, _middleware.js, or _middleware.mjs file in the Pages tree:

pages: Directory middleware is unsupported in Void Pages ("pages/admin/_middleware.ts"). Use global middleware in middleware/ or a shared guard in page loaders and actions.

Routes keep working while the file is there.

A pages/ directory owned by another framework is not checked, and void prepare output is unchanged.

The server routing guide now shows how to protect a Pages section with global middleware or a shared check in loaders and actions.

Part of #808.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Dec 6, 2026 •

Copy link
Copy Markdown

Cloudflare builds and deploys previews for this pull request. Learn more about Cloudflare previews.

Worker Status Deployment Commit
void-cloud Deployed Preview 6b26404

Updated: Dec 6, 2026, 12:28 UTC | Dashboard

@hyfdev
hyfdev added this pull request to stack #1062 December 6, 2026 06:01
@cpojer
cpojer marked this pull request as ready for review December 6, 2026 12:27

@cpojer cpojer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix!

@cpojer
cpojer merged commit b84e7ba into main Dec 6, 2026
22 checks passed
@cpojer
cpojer deleted the fix/pages-middleware-diagnostic branch December 6, 2026 12:55
cpojer added a commit that referenced this pull request Dec 6, 2026
A `_middleware.ts` file under `routes/` now guards every HTTP and
WebSocket route in its directory and below, as suggested in #808.

```ts
// routes/admin/_middleware.ts
import { defineMiddleware } from 'void';
import { getUser } from 'void/auth';

export default defineMiddleware(async (c, next) => {
  if (!getUser()) return c.text('Unauthorized', 401);
  await next();
});
```

Today the same guard has to be added to every handler under
`routes/admin/`, or written as global middleware that checks the path.

Void runs these guards after global middleware, from `routes/` down to
the directory of the file that answers the request, before that file's
handler runs.

Guards see the route params, and their responses behave the same as in
global middleware.

An existing `_middleware.*` file under `routes/` now runs as a guard, so
a helper with that name needs a new one.

Pages are unchanged.

Builds on #1055.

Part of #808.

---------

Co-authored-by: Christoph Nakazawa <cnakazawa@cloudflare.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants