Let global middleware declare the paths it runs on
Maintainer antworten meist innerhalb von 1 Tag
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Anfängerfreundlichkeit
- 35/100
- Issue-Typ
- Feature
- Klarheit
- Größtenteils klar
- Aktivitätsstatus
- Aktiv
- Tech-Stack
- typescript
- Bereich
- tooling
Rechercherichtung
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.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
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.
- Vorherrschende Sprache
- TypeScript
- Sterne
- 684
- Forks
- 83
- Ø Merge
- 15 Std. 22 Min.
- Gemergte PRs (30 T.)
- 219
Entwicklungsumgebung
Dieses Projekt bietet weder Dev-Container noch Dockerfile noch Beitragsleitfaden – die Einrichtung liegt bei Ihnen. Beginnen Sie mit der README; die allgemeinen Schritte stehen in unserem Leitfaden für den ersten Beitrag.
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus voidzero-dev/void
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
voidzero-dev/void#835 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
voidzero-dev/void#834 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 35/100
voidzero-dev/void#833 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 32/100
voidzero-dev/void#832 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 45/100
voidzero-dev/void#825 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in voidzero-dev/void
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 66/100
open-webui/tether#52 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
AykutSarac/jsoncrack.com#616 ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
melgarafael/DeskcommCRM#4679 ·
Maintainer antworten meist innerhalb von 1 Tag
-
enhancement size:medium
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
Maintainer antworten meist innerhalb von 1 Tag