Skip to content

fix(sveltekit): use afterNavigate for SvelteKit 2 and 3 - #215

Open
CNSeniorious000 wants to merge 4 commits into
vercel:mainfrom
CNSeniorious000:fix/sveltekit-3-app-stores
Open

CNSeniorious000 wants to merge 4 commits into
vercel:mainfrom
CNSeniorious000:fix/sveltekit-3-app-stores

Conversation

@CNSeniorious000

@CNSeniorious000 CNSeniorious000 commented Oct 2, 2026 •

Copy link
Copy Markdown

Problem

SvelteKit 3 removed the $app/stores module. injectAnalytics imported page from it and called page.subscribe(), which throws at runtime:

Uncaught (in promise) Error: https://svelte.dev/e/kit/app_stores_removed
    at injectAnalytics (index.mjs)

This crashes hydration for any SvelteKit 3 app using injectAnalytics.

Fix

  • Use afterNavigate from $app/navigation to observe completed navigations without patching the global History API
  • Keep the existing /sveltekit entry and update peerDependencies to ^2.12 || ^3
  • Register injectAnalytics from a component initialization, as required by SvelteKit's afterNavigate lifecycle API
  • Update the SvelteKit demo and README to use src/routes/+layout.svelte

afterNavigate is available in SvelteKit 2.12 and 3. Its callback receives the completed destination, so page views use the final { route, path } values.

Notes

  • $app/environment is still used for browser; it is deprecated in SvelteKit 3 but not removed until SvelteKit 4
  • Consumers should call injectAnalytics during component initialization, for example from +layout.svelte

@vercel

vercel Bot commented Oct 2, 2026

Copy link
Copy Markdown

@CNSeniorious000 is attempting to deploy a commit to the Analytics Test Projects Team on Vercel.

A member of the Team first needs to authorize it.

@Rich-Harris Rich-Harris mentioned this pull request Oct 5, 2026
@CNSeniorious000 CNSeniorious000 changed the title fix(sveltekit): replace removed $app/stores with $app/state for SvelteKit 3 fix(sveltekit): use afterNavigate for SvelteKit 2 and 3 Oct 6, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant