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

SvelteKit restores removed or modified url hash after load function #7271

Open
heikkilamarko opened this issue Oct 15, 2022 · 4 comments
Open
Labels
Milestone

Comments

@heikkilamarko
Copy link

heikkilamarko commented Oct 15, 2022

Describe the bug

I am building a SvelteKit SPA app that is using Authorization Code Flow with PKCE with Keycloak.
I want to handle the auth redirect in the load() function of the root +layout.js file. During auth handling, Keycloak JS SDK removes the hash from the url. The problem is that SvelteKit restores the hash after load() function and the rendered url has the auth redirect hash visible.

See the repro below.

Reproduction

+layout.js

export const ssr = false;

/** @type {import('./$types').LayoutLoad} */
export async function load() {
	console.log('[+layout.js] before auth:', window.location.hash);
	await handleAuth();
	console.log('[+layout.js] after auth:', window.location.hash);
}

+page.svelte

<script>
	console.log('[+page.svelte] script:', window.location.hash);
</script>

<main>DEMO PAGE</main>

Screenshot (see the address bar and console logs):
Screenshot 2022-10-15 at 21 19 04

Logs

No response

System Info

System:
    OS: macOS 12.4
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 440.96 MB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.11.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 8.19.2 - /usr/local/bin/npm
  Browsers:
    Chrome: 106.0.5249.119
    Firefox: 105.0.3
    Safari: 15.5
  npmPackages:
    @sveltejs/adapter-static: next => 1.0.0-next.44 
    @sveltejs/kit: next => 1.0.0-next.516 
    svelte: 3.51.0 => 3.51.0 
    vite: 3.1.8 => 3.1.8

Severity

serious, but I can work around it

Additional Information

No response

@dummdidumm
Copy link
Member

dummdidumm commented Oct 19, 2022

I'm not sure what's the best way forward here - or rather, if this is "works as designed" or if we should take into account this edge case.

The reason why this happens is that the URL is used to find out the correct route to go to, and that URL is later added unchanged to the function which updates the state, which then also resets the URL hash in your case. One could make the case that at least the hash should be checked for changes.

The workaround in the meantime would be to remove the hash inside the Svelte file or after a setTimeout in the load function, which resolves after it has returned.

@heikkilamarko
Copy link
Author

heikkilamarko commented Oct 19, 2022

Thanks for the response.

I wouldn't call this an edge case. Authorization Code Flow with PKCE is a widely used auth flow in SPA applications. The current SvelteKit implementation is likely to cause a lot of confusion in these auth scenarios.

@Rich-Harris
Copy link
Member

load functions shouldn't really contain side effects (such as writing to window.location.hash) — they can be called when you're not actually navigating (e.g. you mouseover a <a data-sveltekit-prefetch> link), so the timing is all wrong. I don't really understand what the handleAuth() function is doing in your example, but is it something that could happen in an afterNavigate callback?

@Rich-Harris Rich-Harris added this to the whenever milestone Nov 17, 2022
@JorensM
Copy link

JorensM commented Apr 20, 2024

I'm having this issue as well, here is the relevant Keycloak.js issue: keycloak/keycloak#14742

@heikkilamarko did you ever manage to solve this?

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

No branches or pull requests

4 participants