Let global middleware declare the paths it runs on

Abierto
#808 1 comentario 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
35/100
Tipo de issue
Nueva funcionalidad
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
typescript
Área
tooling

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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 matcher runs on every request, as today, so existing apps see no change.
  • ! 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/**'].
  • Files still run in filename order; one that doesn't match is skipped.
  • 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.

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: [...] } in middleware.ts, with path patterns and negative lookaheads, so _next/static can be left out.
  • TanStack Start: middleware attached to a route runs only for that route and below.
  • TanStack Router: beforeLoad on a layout route runs for its children only.
Lenguaje dominante
TypeScript
Estrellas
684
Forks
83
Merge medio
15 h 22 min
PR fusionados (30 d)
219

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de voidzero-dev/void

Todos los issues de voidzero-dev/void

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.