Title: Let global middleware declare the paths it runs on · Issue #808 · voidzero-dev/void · GitHub
Open Graph Title: Let global middleware declare the paths it runs on · Issue #808 · voidzero-dev/void
X Title: Let global middleware declare the paths it runs on · Issue #808 · voidzero-dev/void
Description: Version void 1.8.0, and main at 004624f Problem Every file in middleware/ runs on every request, so a middleware that only matters for one section has to test the path itself: // middleware/04.admin.ts const ADMIN_PATH = /^\/admin(\/|$)/...
Open Graph Description: Version void 1.8.0, and main at 004624f Problem Every file in middleware/ runs on every request, so a middleware that only matters for one section has to test the path itself: // middleware/04.admi...
X Description: Version void 1.8.0, and main at 004624f Problem Every file in middleware/ runs on every request, so a middleware that only matters for one section has to test the path itself: // middleware/04.admi...
Opengraph URL: https://github.com/voidzero-dev/void/issues/808
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Let global middleware declare the paths it runs on","articleBody":"### Version\n\n`void` 1.8.0, and `main` at 004624f\n\n### Problem\n\nEvery file in `middleware/` runs on every request, so a middleware that only matters for one section has to test the path itself:\n\n```ts\n// middleware/04.admin.ts\nconst ADMIN_PATH = /^\\/admin(\\/|$)/;\n\nexport default defineMiddleware(async (c, next) => {\n if (!ADMIN_PATH.test(c.req.path)) return next();\n // …\n});\n```\n\nVoid's own dashboard does the same in `platform/packages/dashboard/middleware/01.auth.ts`, with `PUBLIC_PATHS` and `PUBLIC_PREFIXES`.\n\nThat's only boilerplate, but having middleware at all also costs every static file its free request. `resolveAssetWorkerBypass()` in `packages/void/src/asset-worker-bypass.ts` gives up the bypass as soon as there's any middleware:\n\n```ts\nfacts.hasMiddleware ||\n```\n\nSo in a build with middleware, every hashed JS, CSS and image file starts the Worker, and each middleware runs for it. Any middleware that reads a session, KV or D1 has to skip asset paths by hand, or every asset waits on that read. Void can't tell which middleware might touch `/assets/`, so it has to assume all of them do.\n\n### Proposal\n\nLet a middleware file export the paths it runs on, in the same glob syntax as `run_worker_first`:\n\n```ts\n// middleware/04.admin.ts\nexport const matcher = ['/admin', '/admin/**'];\n\nexport default defineMiddleware(async (c, next) => {\n // runs only for /admin and below\n});\n```\n\n- A file without `matcher` runs on every request, as today, so existing apps see no change.\n- `!` patterns exclude paths, as in `run_worker_first`, so a middleware that sets shared data for every page can still leave assets out with `['/**', '!/assets/**']`.\n- Files still run in filename order; one that doesn't match is skipped.\n- `hasMiddleware` becomes \"has middleware that can reach the assets folder\": if every file has a `matcher` and none of them can reach it, checked with `patternCanReachUnder()` as the bypass already does for route, page and auth patterns, the bypass stays on.\n\nMatching on the request path alone keeps it decidable at build time. A function form, like `matcher: (c) => …`, couldn't be checked against the assets folder, so it would have to keep the Worker in front.\n\n### Prior art\n\n- Next.js: `export const config = { matcher: [...] }` in `middleware.ts`, with path patterns and negative lookaheads, so `_next/static` can be left out.\n- TanStack Start: middleware attached to a route runs only for that route and below.\n- TanStack Router: `beforeLoad` on a layout route runs for its children only.\n","author":{"url":"https://github.com/karthickeyan-dev","@type":"Person","name":"karthickeyan-dev"},"datePublished":"2026-11-13T01:48:35Z","url":"https://github.com/voidzero-dev/void/issues/808"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:9543b17c-6793-1ded-3b86-e0b197b94c35 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | ED58:26BE6B:1C3F843:266AB60:6AFB96DF |
| html-safe-nonce | 25030e811b6d9db222c2b9abd8202163affbcbdb2126e91ceabb9a0e08ed2e15 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFRDU4OjI2QkU2QjoxQzNGODQzOjI2NkFCNjA6NkFGQjk2REYiLCJ2aXNpdG9yX2lkIjoiNDc3ODgwODI5MTU0MDgzNDAxNSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 735929d807756dc9592e99e2a3ca0f9045f0fcde82a71fdd82c0f4238c015766 |
| hovercard-subject-tag | issue:6362936450 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/voidzero-dev/void/808/issue_layout |
| twitter:image | https://opengraph.githubassets.com/525bd40affe005c7babb904f8396ab90939c6c1efcdb081ff84ce97c7a0ce58b/voidzero-dev/void/issues/808 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/525bd40affe005c7babb904f8396ab90939c6c1efcdb081ff84ce97c7a0ce58b/voidzero-dev/void/issues/808 |
| og:image:alt | Version void 1.8.0, and main at 004624f Problem Every file in middleware/ runs on every request, so a middleware that only matters for one section has to test the path itself: // middleware/04.admi... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | karthickeyan-dev |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4f01efe06174a5ba6342fd58e42d02295e0675bedbb465d6bef2dbaec8e46556 |
| turbo-cache-control | no-preview |
| go-import | github.com/voidzero-dev/void git https://github.com/voidzero-dev/void.git |
| octolytics-dimension-user_id | 149750581 |
| octolytics-dimension-user_login | voidzero-dev |
| octolytics-dimension-repository_id | 1164437080 |
| octolytics-dimension-repository_nwo | voidzero-dev/void |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1164437080 |
| octolytics-dimension-repository_network_root_nwo | voidzero-dev/void |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | a52db8866b6543b4e83730cd6a6b1360f1a9e4dc |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width