Let global middleware declare the paths it runs on
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 35/100
- Tipo di issue
- Funzionalità
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Attiva
- Stack tecnologico
- typescript
- Ambito
- tooling
Direzione di ricerca
Start by reading packages/void/src/asset-worker-bypass.ts and the middleware loading and execution flow; the issue does not name those entry points or any tests. Trace how patternCanReachUnder() is used for route, page, and auth patterns. Done means middleware matchers skip nonmatching requests and the asset bypass remains enabled when no middleware matcher can reach assets, while files without a matcher retain current behavior.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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(\/|$)/;
export default defineMiddleware(async (c, next) => {
if (!ADMIN_PATH.test(c.req.path)) return next();
// …
});
Void's own dashboard does the same in platform/packages/dashboard/middleware/01.auth.ts, with PUBLIC_PATHS and PUBLIC_PREFIXES.
That'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:
facts.hasMiddleware ||
So 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.
Proposal
Let a middleware file export the paths it runs on, in the same glob syntax as run_worker_first:
// middleware/04.admin.ts
export const matcher = ['/admin', '/admin/**'];
export default defineMiddleware(async (c, next) => {
// runs only for /admin and below
});
- A file without
matcherruns on every request, as today, so existing apps see no change. !patterns exclude paths, as inrun_worker_first, so a middleware that sets shared data for every page can still leave assets out with['/**', '!/assets/**'].- Files still run in filename order; one that doesn't match is skipped.
hasMiddlewarebecomes "has middleware that can reach the assets folder": if every file has amatcherand none of them can reach it, checked withpatternCanReachUnder()as the bypass already does for route, page and auth patterns, the bypass stays on.
Matching 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.
Prior art
- Next.js:
export const config = { matcher: [...] }inmiddleware.ts, with path patterns and negative lookaheads, so_next/staticcan be left out. - TanStack Start: middleware attached to a route runs only for that route and below.
- TanStack Router:
beforeLoadon a layout route runs for its children only.
- Lingua principale
- TypeScript
- Stelle
- 684
- Fork
- 83
- Merge medio
- 15h 22m
- PR unite (30g)
- 219
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di voidzero-dev/void
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
voidzero-dev/void#835 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
voidzero-dev/void#834 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
voidzero-dev/void#833 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 32/100
voidzero-dev/void#832 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 45/100
voidzero-dev/void#825 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di voidzero-dev/void
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
open-webui/tether#52 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
AykutSarac/jsoncrack.com#616 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
melgarafael/DeskcommCRM#4679 ·
I maintainer di solito rispondono entro 1 giorno
-
enhancement size:medium
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno