-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
request.params not available in handle hook as docs / types indicate #1194
Comments
I think I understand the reason behind this. I tried to make it work but My use case is that I want to load some data from database and have it available in the context but |
Can we reopen this please? The docs mistakenly mention that |
Hmm. I was wondering if we should move it until after For reference, right now it's called here:
|
What's the current recommended way to deal with this? I have a bunch of endpoints with an
I suppose this is workable but seems pretty clunky. |
As using params in the handle hook does not make much sense this should just be removed from the docs. If endpoints are accessed via load function then validation or other logic including params could be run prior to calling the endpoints (e.g. in a __layout.svelte). However I think there are valid usecases (as pointed out by @kamholz and @jordankaerim) to have a mechanism to execute code before endpoints are executed.
This hook could implement some middleware logic which could make something like this possible:
Endpoints themselves could also export some kind of function (not sure what to call it ... maybe This solution would improve the above discussed usecases ... however I'm not quite sure if it's really a good idea to do it this way. So please let me know what you think... |
I would like to know what the intended method is. If I have routes My initial thought was I might be able to make a wildcard endpoint at My next approach was to try doing the validation and authorization in a Handle hook, but then I realized that the params aren't available in the hooks. At least it doesn't appear they are there until after resolve is called, so I don't think that will work. My current implementation is @kamholz's solution of creating a wrapper function. As stated this kind of works, but still creates the mental overhead of making sure to wrap every handler. Is this the best solution? I'm still new to sveltekit, is there another solution I'm overlooking? Am I just approaching this wrong and should rethink my routes/endpoints? |
To chime in to this thread. I agree that BUT, I'd love a way to hook in to the route and params which produced a response, where it was successful. This would be phenomenally useful in implementing monitoring (I use Prometheus, for example), where typically a label is added to all response logs like Perhaps we could consider a Happy to open a separate issue for this request if necessary, but it's worth mentioning here as a use-case I think. |
@georgecrawford I believe the params are populated when you call resolve so you should have access to them, and the response status after calling resolve. |
You're right -
gives:
I'd suggest that And I still don't know which route served this response, which is the main thing I'm looking for in #3840 |
See related discussion on param validators here: #4291 |
* remove fallthrough * changeset * remove fallthrough documentation * tweak docs * simplify * simplify * simplify a tiny bit * add failing test of param validators * client-side route parsing * tidy up * add validators to manifest data * client-side validation * simplify * server-side param validation * lint * oops * clarify * docs * minor fixes * fixes * ease debugging * vanquish SPA reloading bug * simplify * lint * windows fix * changeset * match route before calling handle - closes #1194 * changeset * throw error if validator module is missing a validate export * update configuration.md * Update documentation/docs/01-routing.md * tighten up validator naming requirements * disallow $ in both param names and types * changeset * point fallthrough users at validation docs * add some JSDoc commentsd * expose `event.routeId` and `page.routeId` (#4345) * expose event.routeKey - closes #3840 * change routeKey to routeId * rename routeKey to routeId, expose page.routeId * rename route.key -> route.id everywhere * adapter-node sure picked a weird time to stop typechecking * oops * Update .changeset/mean-crews-unite.md
Describe the bug
Request.params
isnull
inhandle
hook.Logs
To Reproduce
src/routes/[product]/index.svelte
handle
function located atsrc/hooks.ts
request.params
inhandle
functionExample repo: https:/RaeesBhatti/svelte-params-bug
Expected behavior
request.params
should be available inhandle
function of hooks.Stacktraces
Information about your SvelteKit Installation:
Diagnostics
Your browser: Safari Version 14.0.3 (16610.4.3.1.7)
Your adapter: Cloudflare Worker but this issues comes up even in
dev
mode.Severity
Blocking usage of SvelteKit because I need params to fetch the right data from database.
Additional context
The text was updated successfully, but these errors were encountered: