You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Feature?]: File-based routing for middlewares #1860
Middlewares that run only on a given route, with file-based routing. The file rules would be similar to those of a layout that works on all routes of a given directory.
A good example can be seen in the Fresh framework, this is a good reference of the feature:
This is a separate request from #1811. That issue was closed after it was answered with "check the request path in your middleware", not by adding directory-scoped middleware. I'm closing this one as not planned, mainly because of how it would interact with server functions.
Server functions aren't sent to the page's path. On both 1.x and 2.x, every server function call is a POST /_server, whichever page it was called from. A middleware scoped to routes/admin/ would run for /admin/* page requests, but not for server functions called from those pages. An auth check placed there would look like it protects the admin data while the server functions behind it stay reachable. One middleware module that checks the path sees every request, including /_server, so it doesn't have that gap.
Scoping middleware to paths already works on both lines.
I checked both on the respective lines: the path-scoped middleware ran only for matching paths, and could set headers or block a request (403). A server function called from an /admin page arrived as POST /_server, so protect server functions with checks inside the function, or with a middleware that also handles /_server.
Activity
Sleepful commentedon Mar 20, 2025
Tangentially related issue: #1811
brenelz commentedon Sep 18, 2026
So can this be closed as resolved. The other issue is closed
lxsmnsyc commentedon Oct 14, 2026
This is a separate request from #1811. That issue was closed after it was answered with "check the request path in your middleware", not by adding directory-scoped middleware. I'm closing this one as not planned, mainly because of how it would interact with server functions.
Server functions aren't sent to the page's path. On both 1.x and 2.x, every server function call is a
POST /_server, whichever page it was called from. A middleware scoped toroutes/admin/would run for/admin/*page requests, but not for server functions called from those pages. An auth check placed there would look like it protects the admin data while the server functions behind it stay reachable. One middleware module that checks the path sees every request, including/_server, so it doesn't have that gap.Scoping middleware to paths already works on both lines.
2.x, with h3 middleware:
1.x, with
onRequest:I checked both on the respective lines: the path-scoped middleware ran only for matching paths, and could set headers or block a request (403). A server function called from an
/adminpage arrived asPOST /_server, so protect server functions with checks inside the function, or with a middleware that also handles/_server.