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 didn't get called in production. development and preview runs ok. #5323

Closed
taganchua opened this issue Jun 30, 2022 · 6 comments
Closed

Comments

@taganchua
Copy link

taganchua commented Jun 30, 2022

Describe the bug

Before I update npms,

my project is old and use Sveltekit v1.0.0-next.326. vite 2.9.8.

svelte.config.js

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

/** @type {import('@sveltejs/kit').Config} */
const config = {
	preprocess: preprocess({}),
	kit: {
		adapter: adapter({
			out: 'build'
		})
	}
};

export default config;

hooks.ts

import { parse } from 'cookie';
import { UAParser } from 'ua-parser-js'; 

export const handle = async ({ event, resolve }) => {
	const parser = new UAParser(event.request.headers.get('user-agent') || '');
	const cookies = parse(event.request.headers.get('cookie') || '');
	if (cookies) {
		...
               event.locals.user = { .... };
	}

	return await resolve(event);
};

export function getSession({ locals }) {
	return {
		user: locals.user
	};
}

It runs fine in all enivornments.

Some npm modules are outdated and after I updated some npm modules (Sveltekit v1.0.0-next.357. vite 2.9.13), development and preview run OK. But production cannot work. getSession didn't get called.

I tried this #3722 (comment). Still cannot work.

Anyone knows? How to solve this issue?

Reproduction

https://github.com/taganchua/TestApp

Logs

No response

System Info

System:
    OS: Windows 10 10.0.19044
    CPU: (8) x64 Intel(R) Core(TM) i7-8665U CPU @ 1.90GHz
    Memory: 3.73 GB / 15.81 GB
  Binaries:
    Node: 16.15.0 - C:\Program Files\nodejs\node.EXE
    npm: 8.12.2 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Chrome: 103.0.5060.66
    Edge: Spartan (44.19041.1266.0), Chromium (103.0.1264.37)
    Internet Explorer: 11.0.19041.1566
  npmPackages:
    @sveltejs/adapter-auto: ^1.0.0-next.40 => 1.0.0-next.53
    @sveltejs/adapter-node: ^1.0.0-next.73 => 1.0.0-next.78
    @sveltejs/adapter-static: ^1.0.0-next.29 => 1.0.0-next.34
    @sveltejs/kit: ^1.0.0-next.326 => 1.0.0-next.357
    svelte: ^3.48.0 => 3.48.0

Severity

annoyance

Additional Information

No response

@gtm-nayan
Copy link
Contributor

gtm-nayan commented Jun 30, 2022

FWIW, I added a getSession to the demo app with the node adapter and it's running fine in all modes.

Please provide a proper minimal reproduction for the issue in the form of a public git repo.

@taganchua
Copy link
Author

taganchua commented Jul 1, 2022

Updated reproduction in first post.

https://github.com/taganchua/TestApp

Login button is in first page. And the cookie is UserID: undefined

After click login, UserID would be "TesterName".

Development works fine. Preview cannot work. I put console.log() in getSession(). It didn't get called in my side.

Before login
UserID: undefined

After login
Development
UserID: TesterName

Preview
UserID: undefined

@quasarchimaere
Copy link

i had the same issue, and it turned out that getSession() was called for all pages but the ones that had the prerender = true constant in the component code... not sure if that helps you

@quasarchimaere
Copy link

https://github.com/taganchua/TestApp/blob/73bc6d16bae6dc43033095b8dd4b16208f944996/src/routes/index.svelte#L2

this line is the reason why getSession() is not called in production, since this flag is setting the whole page (and its children) to be precompiled (in production) and thus accessing this route will only return a static html page to the client disregarding the hooks logic

@taganchua
Copy link
Author

taganchua commented Jul 1, 2022

i had the same issue, and it turned out that getSession() was called for all pages but the ones that had the prerender = true constant in the component code... not sure if that helps you

Forgot to remove that in this TestApp project. But my old project has set prerender = false already. I'll double check.

@taganchua
Copy link
Author

Weird, my old project now ok. It couldn't work yesterday and I didn't even change any code in my old project today.

Thanks anyway!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants