question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Subscribing to `page` store in `$layout.svelte` throws error

See original GitHub issue

Describe the bug Subscribing to the page in $layout.svelte throws an error.

Logs Screen Shot 2021-03-24 at 2 44 53 PM

To Reproduce

Add this to the <script> block in $layout.svelte

import { page } from "$app/stores";
page.subscribe(() => {});

Expected behavior It should work!

Stacktraces

Stack trace
get_current_component@http://localhost:3000/node_modules/svelte/internal/index.mjs?v=e3e1bc30:649:15
getContext@http://localhost:3000/node_modules/svelte/internal/index.mjs?v=e3e1bc30:682:12
getStores@http://localhost:3000/.svelte/dev/runtime/app/stores.js:24:27
subscribe@http://localhost:3000/.svelte/dev/runtime/app/stores.js:50:17
instance/<@http://localhost:3000/src/routes/$layout.svelte?import:215:8
run@http://localhost:3000/node_modules/svelte/internal/index.mjs?v=e3e1bc30:18:12
mount_component/<@http://localhost:3000/node_modules/svelte/internal/index.mjs?v=e3e1bc30:1416:45
flush@http://localhost:3000/node_modules/svelte/internal/index.mjs?v=e3e1bc30:746:17
init@http://localhost:3000/node_modules/svelte/internal/index.mjs?v=e3e1bc30:1504:9
Root@http://localhost:3000/.svelte/dev/generated/root.svelte?import:574:7
createProxiedComponent@http://localhost:3000/node_modules/svelte-hmr/runtime/svelte-hooks.js:245:9
ProxyComponent@http://localhost:3000/node_modules/svelte-hmr/runtime/proxy.js:240:42
Proxy<Root>@http://localhost:3000/node_modules/svelte-hmr/runtime/proxy.js:340:11
start@http://localhost:3000/.svelte/dev/runtime/internal/start.js:409:15
async*start@http://localhost:3000/.svelte/dev/runtime/internal/start.js:811:17
@http://localhost:3000/:57:9

Information about your SvelteKit Installation:

System:
  OS: macOS 11.2.3
  CPU: (8) x64 Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz
  Memory: 3.25 GB / 32.00 GB
  Shell: 3.2.0 - /usr/local/bin/fish
Binaries:
  Node: 14.16.0 - /usr/local/bin/node
  Yarn: 1.22.10 - /usr/local/bin/yarn
  npm: 6.14.11 - /usr/local/bin/npm
Browsers:
  Chrome: 89.0.4389.90
  Firefox Developer Edition: 88.0
  Safari: 14.0.3
npmPackages:
  @sveltejs/kit: next => 1.0.0-next.58
  svelte: ^3.35.0 => 3.35.0

Severity Blocking

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
Conduitrycommented, Apr 2, 2021

That would be expected. To use the stores directly, you need to subscribe to the store synchronously during the initialization of the component (and not asynchronously later during mounting). If you want to be able to subscribe later, you need to grab the store synchronously by using getStores (like one would in Sapper). This is described in https://kit.svelte.dev/docs#modules-app-stores

1reaction
furudeancommented, Mar 30, 2021

Your workaround doesn’t seem to work for me, unfortunately

Read more comments on GitHub >

github_iconTop Results From Across the Web

session.subscribe throws error when called in onMount
subscribe outside onMount it runs fine. Note: this code is part of a SvelteKit Project, inside a Svelte component, not a SvelteKit page/route....
Read more >
Errors • Docs • SvelteKit
The exception is when the error occurs inside the root +layout.js or +layout.server.js , since the root layout would ordinarily contain the +error.svelte...
Read more >
Loading data • Docs • SvelteKit
Data returned from layout load functions is available to child +layout.svelte components and the +page.svelte component as well as the layout that it...
Read more >
Routing • Docs • SvelteKit
throw error (404, 'Not found'); }) satisfies PageLoad ; This function runs alongside +page.svelte , which means it runs on the server during...
Read more >
Hooks • Docs • SvelteKit
'Hooks' are app-wide functions you declare that SvelteKit will call in ... split at sensible boundaries such as %sveltekit.head% or layout/page components.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found