Removing session
#5883
Replies: 25 comments 116 replies
-
I'm all for removing |
Beta Was this translation helpful? Give feedback.
-
I love But I'm all for change that resolves issues and uses the new capabilities we have. |
Beta Was this translation helpful? Give feedback.
-
Is the And if so, what will know to update and how? |
Beta Was this translation helpful? Give feedback.
-
Is this still happening ? Wasn't sure of the meaning of |
Beta Was this translation helpful? Give feedback.
-
How could I reach the data from a |
Beta Was this translation helpful? Give feedback.
-
I just tried doing this on a project, and it does work quite fine for the SSR, but the moment the client goes to another page and the client side navigation has to trigger a load, the call to // +layout.server.ts
import type { LayoutServerLoad } from '../../.svelte-kit/types/src/routes/$types';
import type { Session } from '$lib/models/session';
export const load: LayoutServerLoad = ({ locals }): { session: Session } => {
console.log('load server'); // Only logged on server (as expected)
return {
session: {
authToken: locals.authToken,
},
};
}; // +page.ts
export const load: PageLoad = async ({ parent, fetch, params }): Promise<{ some: Thing }> => {
console.log({ p: await parent() });
// On server: { p: { session: { authToken: 'some_token' } } }
// On client on navigation: { p: { } }
} Otherwise I'm a big fan of the idea. I have been cursing that session object for quite a while. |
Beta Was this translation helpful? Give feedback.
-
So I'm having a similar issue with // layout.server.js
export async function load({url, request}) {
const session = await db.getSession(request.headers.cookies)
console.log("session", session) // << does get here
return session
} // layout.js
<script>
export let data
</script>
<slot />
{JSON.stringify(data) } // logout/page.server.js
import cookie from 'cookie'
import { redirect } from '@sveltejs/kit'
export async function load({ request, setHeaders }) {
const str = cookie.serialize("sid", "", {
path:"/",
expires: new Date(0)
})
setHeaders({'set-cookie': str})
throw redirect(302, "/")
} when i navigate to logout I see something in the terminal like the following which indicates
However the |
Beta Was this translation helpful? Give feedback.
-
This change makes me super happy. Mostly because it makes the DB part of this #4822 no longer a pain. |
Beta Was this translation helpful? Give feedback.
-
I'm rethinking my entire existence looking at all the changes in SvelteKit over the last couple of weeks. From now on, you need to do I have the utmost respect and admiration for everyone involved in the development of this framework, I really do, but it has to be said, this is all very disappointing and backwards. |
Beta Was this translation helpful? Give feedback.
-
Could we have this moved to "announcement"? Might make it easier for people to see these changes more visibly |
Beta Was this translation helpful? Give feedback.
-
Under "Why don't we need it any more?", the example uses // src/routes/+layout.server.js
/** @type {import('./$types').Get} */
export async function GET({ request, setHeaders }) { Shouldn't it use /** @type {import('./$types').LayoutServerLoad} */
export async function load({ request, setHeaders }) { I used this code as reference when refactoring my codebase and couldn't figure out why it wasn't working until I looked at the docs and tried changing it to load. |
Beta Was this translation helpful? Give feedback.
-
I dont think removing session is that bad. However, i dislike the loss of type safety for global state. |
Beta Was this translation helpful? Give feedback.
-
@Rich-Harris Have you migrated svelte.dev yet? I'm using supabase with their auth-helpers and now don't know how to use it with the latest sveltekit as it expects
Any ideas how to get this working without rewriting the supabase auth helpers? |
Beta Was this translation helpful? Give feedback.
-
Hey guys, In our //add the language the user's browser accepts as main language
/** @type {import('@sveltejs/kit').GetSession} */
export function getSession(event) {
const languageHeaderParsed = parseHeader(event?.request?.headers?.get('accept-language'));
let acceptedLanguage = languageHeaderParsed && languageHeaderParsed[0] ? languageHeaderParsed[0].code : 'de';
if (!supportedLanguages[acceptedLanguage]) {
acceptedLanguage = 'de';
}
return {
'user-language': acceptedLanguage
};
} We now could access the language using If I understand you correctly I now have to put the logic I've put into Thank you! |
Beta Was this translation helpful? Give feedback.
-
@dummdidumm The generated types by Svelte on this example aren't correct. You'd expect any page that taps onto the default layout's server load function to have typed in its returning value, and it completely lacks any information about that signature. It seems to be due to named layouts.
|
Beta Was this translation helpful? Give feedback.
-
I'm trying to refactor my app to the latest SvelteKit version. Updated to 414 first, took a few hours but everything works and the code got better I think (absolutely love the new data prop vs the old hooks.js import cookie from "cookie";
export async function handle({ event, resolve }) {
const cookies = cookie.parse(event.request.headers.get("cookie") || "");
event.locals.token = cookies.token;
return await resolve(event);
} +layout.server.js export async function load({ locals }) {
return {
...locals
};
} +layout.js export async function load({ fetch, parent }) {
const data = await parent();
console.log(data);
} I get an empty object here, even though the token is definitely returned from |
Beta Was this translation helpful? Give feedback.
-
Update: session was removed in #5946
One of the great joys of designing APIs is the realisation, when you finally hit upon the right design, that previously hard problems become trivial to solve. An example: while we were coming up with #5748, we realised that it gave us the opportunity to generate types for
export let data
, giving you incredibly low-effort type safety.In other cases, it's being able to remove features of the design that existed to compensate for shortcomings that no longer exist.
session
is one of those features.What is
session
?session
is an app-level construct that basically exists to get commonly-needed data from the server to the client. Most often, it's used for information about the currentuser
. The data is available inload
functions and via thesession
store.It is created by the
getSession
hook.Why don't we need it any more?
When we introduced 'page endpoints', we solved the problem of getting data from the server to the client, but only for 'leaf nodes'. After #5748, we have a much more general solution — all you need to do is put some data in your root
+layout.server.js
file......and every
load
function can access it withparent
......and every page can access it via
$page.data
:If a
load
function didn't depend onuser
, it would no longer be blocked ondb.getUser
, unlike today.Ok but is it doing any harm?
Yes.
For one thing, any concept that increases the API surface area is bad, other things being equal — it's just more stuff to learn. In the case of
session
it touches the API in three separate places —load
,$app/stores
andgetSession
.It causes confusion in other ways. Navigating from a prerendered page to a non-prerendered page results in an unpopulated
session
(#4426), which is very hard to solve cleanly. Because it's a writable store, people sometimes write to it, and that causesload
to re-run at times they don't expect (#3732/#2301).It also causes unnecessary work. On https://svelte.dev, we have to read session data from Supabase for every single request, even though the only place we actually need session data is https://svelte.dev/repl and https://svelte.dev/apps. (I only just realised this, but since we're prerendering most of the site, this means we're affected by the unpopulated
session
bug described in the last paragraph. That's how insidious the bug is!)Getting rid of all this stuff would be a huge win.
Beta Was this translation helpful? Give feedback.
All reactions