Let global middleware declare the paths it runs on
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 5/5
- 预计耗时
- 一周以上
- 新手友好度
- 35/100
- Issue 类型
- 功能
- 描述清晰度
- 基本清楚
- 活跃度
- 活跃
- 技术栈
- typescript
- 领域
- tooling
调研方向
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.
由索引模型根据 Issue 内容生成。
描述
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.
- 主要语言
- TypeScript
- 星标
- 684
- 派生
- 83
- 平均合并
- 15 小时 22 分钟
- 30 天内合并 PR
- 219
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
voidzero-dev/void 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 72/100
voidzero-dev/void#835 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 82/100
voidzero-dev/void#834 ·
维护者通常 1 天内回复
-
难度 4/5 3-5 天 新手友好度 35/100
voidzero-dev/void#833 ·
维护者通常 1 天内回复
-
难度 4/5 3-5 天 新手友好度 32/100
voidzero-dev/void#832 ·
维护者通常 1 天内回复
-
难度 3/5 1-2 天 新手友好度 45/100
voidzero-dev/void#825 · 1 条评论 ·
维护者通常 1 天内回复
查看 voidzero-dev/void 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 74/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 66/100
open-webui/tether#52 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
AykutSarac/jsoncrack.com#616 ·
-
难度 2/5 1-3 小时 新手友好度 72/100
melgarafael/DeskcommCRM#4679 ·
维护者通常 1 天内回复
-
enhancement size:medium
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复