Skip to content
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

getSession() does not get called on page load. should warn user #3722

Closed
luhagel opened this issue Feb 4, 2022 · 17 comments · Fixed by #4811
Closed

getSession() does not get called on page load. should warn user #3722

luhagel opened this issue Feb 4, 2022 · 17 comments · Fixed by #4811
Labels
bug Something isn't working help wanted PRs welcomed. The implementation details are unlikely to cause debate p2-nice-to-have SvelteKit cannot be used by a small number of people, quality of life improvements, etc.
Milestone

Comments

@luhagel
Copy link

luhagel commented Feb 4, 2022

Describe the bug

Unfortunately, I haven't been able to get our sveltekit app to work in production past kt@next.206 and adapter-node@next.56.

Since the ssr was always somewhat flunky with our - aside from the login page- completely private app we previously disabled it via the config and on all update attempts in the handle hook.

Now, the issue is always the same, everything runs fine locally, until it gets deployed, where you can't get past the login screen anymore, since the session never gets populated. I recently found out that the issue seems to lie somewhere within adapter-node, since running dev and preview works flawlessly, but running the build output directly via node _build presents the same issue our staging environment showed locally.

Any help/pointers would be appreciated, ready to replace our auth code if necessary/it's just us working against sveltekit.

If you have any further questions, feel free to ask !

Reproduction

https://github.com/luhagel/sveltekit-node-session-repro

Logs

No response

System Info

System:
    OS: macOS 11.6
    CPU: (12) x64 AMD Ryzen 5 3600X 6-Core Processor             
    Memory: 9.02 GB / 32.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 17.4.0 - /usr/local/bin/node
    Yarn: 1.22.17 - /usr/local/bin/yarn
    npm: 8.3.1 - /usr/local/bin/npm
  Browsers:
    Chrome: 98.0.4758.80
    Firefox: 96.0.2
    Safari: 15.0
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.17 
    @sveltejs/adapter-node: ^1.0.0-next.67 => 1.0.0-next.67 
    @sveltejs/kit: next => 1.0.0-next.260 
    svelte: ^3.44.0 => 3.46.4

Severity

blocking an upgrade

Additional Information

No response

@PH4NTOMiki
Copy link
Contributor

If I remove {ssr: false} from your hooks file, it works for me, both in dev and prod (build)

@zommerberg
Copy link
Contributor

I had the same issue and had to remove the handle function together with the ssr:false (sveltekit v.260), It appears that having ssr disabled prevents getSession from passing data or being run with adapter node on prod.

@eriknyk
Copy link

eriknyk commented Feb 5, 2022

same here,
sadly I cannot simply remove {ssr: false} because with ssr enabled mane dependencies are broken, and I cannot modify them since they are third-party libs that only works on client side. :(

@benmccann benmccann added this to the 1.0 milestone Feb 11, 2022
@benmccann benmccann added bug Something isn't working p1-important SvelteKit cannot be used by a large number of people, basic functionality is missing, etc. labels Feb 11, 2022
@PH4NTOMiki
Copy link
Contributor

@benmccann I'm not sure this is a bug, I just tried to debug it, and what I found out was that that page was prerendered (/protected) and that's why it was not working, because it was a static page, so the solution to the @luhagel issue is use this config:

import node from '@sveltejs/adapter-node';
import preprocess from 'svelte-preprocess';

/** @type {import('@sveltejs/kit').Config} */
const config = {
	// Consult https://github.com/sveltejs/svelte-preprocess
	// for more information about preprocessors
	preprocess: preprocess(),

	kit: {
		adapter: node({
			out: '_build',
			precompress: true
		}),
		prerender: {
			enabled: false
		}
	}
};

export default config;

@PH4NTOMiki
Copy link
Contributor

@benmccann but there should be at least a warning that session is being used on prerendered page, something like that.

@luhagel
Copy link
Author

luhagel commented Feb 11, 2022

@PH4NTOMiki Thanks for looking into this ! Gonna give it a try with the full scale non-repro project and keep you posted 👍

@benmccann
Copy link
Member

there should be at least a warning that session is being used on prerendered page

We have the same for the query string already. I haven't booked closely at this issue, but perhaps it could make sense to do the same with session

@luhagel
Copy link
Author

luhagel commented Feb 13, 2022

@PH4NTOMiki That did indeed do the trick, thanks a lot ! And yes, a warning would be helpful, especially since it was a somewhat undocumented change post .206 and ist pretty difficult to figure out without.

@PH4NTOMiki
Copy link
Contributor

@zommerberg @eriknyk what about you, does that solve the issue?

@zommerberg
Copy link
Contributor

@PH4NTOMiki Yes, it solves it.

@benmccann benmccann added help wanted p2-nice-to-have SvelteKit cannot be used by a small number of people, quality of life improvements, etc. and removed p1-important SvelteKit cannot be used by a large number of people, basic functionality is missing, etc. labels Feb 27, 2022
@benmccann benmccann changed the title getSession() does not get called on page load when using adapter-node getSession() does not get called on page load. should warn user Mar 6, 2022
@benmccann benmccann added help wanted PRs welcomed. The implementation details are unlikely to cause debate and removed help wanted labels Apr 4, 2022
@elliott-with-the-longest-name-on-github
Copy link
Contributor

@benmccann

Happy to tackle this one. Just give the a-ok.

Rich-Harris added a commit that referenced this issue May 23, 2022
 #3722 (#4811)

* feat: Failing test

* feat: Prerendering fails for pages that access session

* feat: Changeset

* fix: Unnecessary cast

* feat: Fail on server navigations in dev

* feat: Test to make sure runtime prerendering results in error

* use options.prerender.default

Co-authored-by: Rich Harris <hello@rich-harris.dev>
@JJCLane
Copy link

JJCLane commented Aug 4, 2022

Not sure if you'd like me to add another issue for this, but I just ran into a related issue where I was accessing the session through import { session } from '$app/stores'; in __layout.svelte (think user menu), and it took me a while to realise I had a stray prerender = true causing the page not to update.

@elliott-with-the-longest-name-on-github
Copy link
Contributor

Not sure if you'd like me to add another issue for this, but I just ran into a related issue where I was accessing the session through import { session } from '$app/stores'; in __layout.svelte (think user menu), and it took me a while to realise I had a stray prerender = true causing the page not to update.

Weird. I'm 95% sure that I added some error log for this before this issue got closed.

@elliott-with-the-longest-name-on-github
Copy link
Contributor

Yeah, #4811 should've fixed this.

@elliott-with-the-longest-name-on-github
Copy link
Contributor

Oh, pff, I'm stupid -- this is the session store. Yeah, I would open a new issue and reference this one. Be sure to include a small repro with StackBlitz or a GitHub repo.

@Lirben
Copy link

Lirben commented Aug 18, 2022

Hi,

I don't know if this is related, but i get an error:

"Attempted to access session from a prerendered page. Session would never be populated."

By having enabled: false. This indeed dissapears. But i want to enable prerendering.

This code triggers the error: It strange, because i don't really read the session variable. If i delete the parameter, i can run my dev server, but when building with static adapter, it throws the error again.

<script context="module">
	/**
	 * @type {import('@sveltejs/kit').Load}
	 */
	export async function load({ params, fetch, session, context })
    {
        const response = await fetch('/api_routes/coursesAPI/byID/zu9knmlb80.json');
		let course = await response.json();
        
		if (response.ok) {
			return {
				props: {
                    course: course
				}
			};
		}	
	}
</script>

thanks for any help

@elliott-with-the-longest-name-on-github
Copy link
Contributor

Hi,

I don't know if this is related, but i get an error:

"Attempted to access session from a prerendered page. Session would never be populated."

By having enabled: false. This indeed dissapears. But i want to enable prerendering.

This code triggers the error: It strange, because i don't really read the session variable. If i delete the parameter, i can run my dev server, but when building with static adapter, it throws the error again.

<script context="module">
	/**
	 * @type {import('@sveltejs/kit').Load}
	 */
	export async function load({ params, fetch, session, context })
    {
        const response = await fetch('/api_routes/coursesAPI/byID/zu9knmlb80.json');
		let course = await response.json();
        
		if (response.ok) {
			return {
				props: {
                    course: course
				}
			};
		}	
	}
</script>

thanks for any help

Intended behavior. Also, you're using an old version of Kit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted PRs welcomed. The implementation details are unlikely to cause debate p2-nice-to-have SvelteKit cannot be used by a small number of people, quality of life improvements, etc.
Projects
None yet
8 participants