question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Official way to pass state from endpoint to fallthrough page routes

See original GitHub issue

Is your feature request related to a problem? Please describe. I’m implementing a login flow that for technical reasons needs to be an old-fashioned form submit. If the username and password that are entered fail to authenticate, I’d like the response to the POST to be the same login page, but with the username already pre-filled with what the user entered last time, and with a specific error message about what happened.

Describe the solution you’d like I’d like the handler for a POST endpoint to be able to fall through to a page (which it already can), and for it to be able to pass some state to that page. It turns out this is already possible, by mutating request.locals inside the endpoint. Since getSession is called anew each time we try to render a request as a page, we would be able to expose whatever information we want to the page.

The question then is whether this is desired behavior. It is convenient in my case, but it could be seen as too weird or magical. So this feature request is for solidifying this as official behavior through tests and documentation.

Describe alternatives you’ve considered In my case, there are other workarounds. I could manually implement this endpoint as part of the handle hook, so that I could be sure my locals values would still be present when Kit tries to render this as a page. Or on a failed login I could have the endpoint redirect (as a GET) to a URL that had the username and an error code in query parameters.

How important is this feature to you? Being able to implement this login flow is quite important, as it’s for work. The solution outlined here is somewhat important, as it seems like a nice way to solve this.

Additional context @GrygrFlzr and I chatted briefly about this and some related issues on Discord. It’s currently possible for getSession itself to mutate the request, but that seems really weird, impractical to prevent, and not something we should be worrying about defining the behavior of.

We also chatted about how routes are sorted by specificity and precedence. This is mostly described at https://kit.svelte.dev/docs#routing-advanced-fallthrough-routes but I’m wondering whether this could be made a bit more explicit. Endpoints do have higher precedence than pages, but only for routes that otherwise have the same specificity. This is a bit tricky to say in a way that is both accurate and comprehensible. We don’t, for example, want to just link people to the comparator function that’s used to sort them.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:11
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
dslatkincommented, Jan 12, 2022

It would be great if this could be addressed before a 1.0 release or at least become a p1 issue. It feels hard to say that SvelteKit can make transitional apps if there’s not a built-in or sanctioned way for SvelteKit to handle server-invalidated form submissions for users without JS. I’m wondering if @Rich-Harris has seen this yet and has thoughts?

Another conversation on Discord recently led some of us to the same technique as @Conduitry - loading request.locals in an endpoint before falling through to a page, at which point getSession picks up those new locals to make them available in that page’s load function. I would feel much better about it if it’s clear in the docs that it’s sanctioned behavior and not an abuse of locals. It also wasn’t entirely clear to me that getSession may run multiple times on the same request during SSR so it would be nice if this were mentioned in the docs too?

For serving entire pages from endpoints, I personally would be very wary of addressing the issue that way. I could see routing becoming much more ambiguous by comingling which pages are served at which paths in the file tree. If a request comes in for /some-page, I would think it should be either /some-page.js or /some-page.svelte which serves the response.

1reaction
Rich-Harriscommented, Feb 2, 2022

Closing in favour of #3532

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I pass state through React_router? - Stack Overflow
tl;dr your best bet is to use a store like redux or mobx when managing state that needs to be accessible throughout your...
Read more >
Routing in ASP.NET Core - Microsoft Learn
Routing is responsible for matching incoming HTTP requests and dispatching those requests to the app's executable endpoints.
Read more >
Handling SPA Fallback Paths in a Generic ASP.NET Core ...
In a client side SPA application this route triggers the client side router that knows how go to a specific album page like...
Read more >
Server-Side Rendering (SSR) - Vue.js
Before using SSR for your app, the first question you should ask is whether you actually need it. It mostly depends on how...
Read more >
Express Tutorial Part 4: Routes and controllers - MDN Web Docs
We've now created all the routes for our site, along with dummy controller ... Objective: To understand how to create simple routes.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found