-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for per-directory +hooks.server.js
route files
#6731
Comments
This might be a good way to handle the problem described in #6315 |
It would be helpful to have some concrete use cases for this. We've discussed it a fair amount but so far determined that it wouldn't be a good addition, because it would add a lot more complexity. For example, if you have a situation like this...
...then there's some stuff you have to understand about what happens when, but it basically makes sense:
If we now add a bunch of src/
├ routes/
│ ├ foo/
│ │ ├ bar/
│ │ │ ├ baz/
+ │ │ │ ├ +hooks.server.js
│ │ │ │ ├ +layout.js
│ │ │ │ ├ +page.js
│ │ │ │ └ +page.svelte
+ │ │ ├ +hooks.server.js
│ │ │ └ +layout.js
+ │ ├ +hooks.server.js
│ │ └ +layout.js
+ ├ +hooks.server.js
│ └ +layout.js
└ hooks.server.js ...then the order gets somewhat more complex:
I feel like the mixture of sequential and concurrent code would be a real source of confusion for people, particularly the fact that e.g. On top of that, while it makes sense to chain |
Thank you for taking time to consider this @Rich-Harris. Authorisation is my primary use case for adding optional I've just read the proposal to add guard functions to layouts #6912 and this is similar to what I am looking for, albeit extending the |
Reflecting on your response - and just thinking about Also, The benefit of colocating hooks with the routes that they apply to reduces the overhead of guard functions to check if the hook applies to the request or not. I accept that it would introduce complexity for the framework as it would be necessary to create route specific sequences, but the benefit will be for the developer. A lot could be achieved by in Edit: I should add, I hadn't considered |
I feel like this issue can almost be renamed to "Add support for route-level middleware". To summarize the two big use cases:
I'm not personally convinced this is actually that complicated to introduce to developers. The mental model could be as simple as:
Load functions are introduced earlier in the docs and tutorial, encouraging async by default, with hooks beign introduced under the "advanced" section in both the docs and tutorial. You could really hammer the point home with a warning like:
As for the question about Finally, this all could be made a bit more simple in SvelteKit v2 by deprecating |
I think if people are going to use SK as their backend with a filesystem based router that router regardless of its description needs to support middleware. If I were using express or koa or whatever I would have middleware groups, which solve this problem entirely. Whether it's called hooks or not, I don't see how a functional router can exist without supporting middleware. If I made an API router that you had to string match the URI to choose to run middleware it would be DOA, I don't see how SK can be a mature full stack solution without this. |
+hooks.server.js
route files+hooks.server.js
route files
SvelteKit does now support I've renamed this issue to clarify that the request here is to be able to include such a file in each |
If a hooks.server.js per directory is too complicated, maybe a hooks.server.js per routing group would be a good compromise.
|
Personally, I feel like doing it per routing group would make it more complicated.
vs. something like
|
I don't think there's a major difference in complicatedness, and having both would be even better. |
"Having both" is achieved with the second description I shared which is why I would advocate for it over a special exception for something that only works within layout groups. |
Personally, I feel like doing it per directory would make it more complicated.
vs. something like
|
That quote is not what I'm suggesting - remove |
I feel like that just makes sense. Pattern matching the pathname in a large application is a pain, and it sometimes causes performance issues since the same middleware runs for every single request. |
Wanted to share Rich's user-land implementation for having per-directory hooks and an interesting read from the same README about why we might not want to implement this natively: |
Describe the problem
The current hooks functionality is really useful, particularly when used with
sequence()
to create an event pipeline. However, I'm increasingly finding that I want to specialise hooks for a given request paths, which requires pathname checking. Whilst this works, it is brittle - changing the route folder requires changing the pathname check. It would be preferable to colocate specialised hooks with the routes they pertain to.Describe the proposed solution
I would like to be able to add a
+hooks.server.js
(or.ts
) route file to a route folder that would export server hooks specifically for that route and its children. The request event handler would call the exportedhandle
function after the currenthooks.server
handle
function had been called, effectively appending it to the global hook sequence.Where a child route folder has parent route folders with hooks, these would be appended prior to any hooks specific to the child.
Alternatives considered
Prior to the introduction of generated types I had used an interceptor pattern to wrap
Handle
and / orRequestHandler
, but this has become too unwieldy to type correctly, and has resulted in unnecessary boilerplate-like code.Importance
would make my life easier
Additional Information
No response
The text was updated successfully, but these errors were encountered: