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.

__layout.svelte may run twice after browser reload in dev

See original GitHub issue

Describe the bug

This issue only affects the context script for src/routes/__layout.svelte. Other layout files are not affected, and #1214 is different.

At first, everything works fine across browser reloads, updating some other files isn’t a problem at all. But updating the top level layout file, or any of it’s imported dependencies (components or modules) will trigger this issue, to cause the context script to run twice when I reload the browser’s tab. Clearing the browser’s cache won’t help until I reload the development server.

Before the update before update

After the update (notice the abnormal additional log changing positions) update   reload 1 update   reload 2 update   reload 3

Reproduction

  • sveltekit skeleton project: npm init svelte@next __layout-ctx-run-twice
  • add the top level layout file containing:
<!-- src/routes/__layout.svelte -->
<script context="module">
    console.log('%c module: this may runs twice.', 'color: purple; font-weight: bold;')

    export function load () {
        console.log('%c load: this runs once.', 'color: magenta; font-weight: bold;')
        return {}
    }
</script>

<script>
    console.log('%c instance: this runs once.', 'color: blue; font-weight: bold;')
</script>

<slot></slot>
  • start the development server: npm run dev
  • navigate to localhost:3000, all the logs are logged once respecting the same order across reloads, and they’re all coming from __layout.svelte? [sm]
  • force update src/routes/__layout.svelte, then reload the browser tab.
  • inspect the logs again to notice the first log in code (purple) is logged twice. One is coming from __layout.svelte? [sm] as the other logs, and the abnormal one is coming from __layout.svelte
  • reload as much as you like and you’ll notice that the abnormal log may not respect the same log order.
  • finally, reload the development server, and the issue will disappear.

Logs

No response

System Info

System:
    OS: Linux 4.19 LMDE 4 (debbie) 4 (debbie)
    CPU: (4) x64 Intel(R) Core(TM) i5-3570K CPU @ 3.40GHz
    Memory: 3.28 GB / 15.56 GB
    Container: Yes
    Shell: 5.0.3 - /bin/bash
  Binaries:
    Node: 14.17.1
    npm: 6.14.13
  Browsers:
    Chrome: 87.0.4280.141
    Firefox: 68.8.0esr

Severity

annoyance

Additional Information

Tested using both Chrome and Firefox.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:27
  • Comments:20 (4 by maintainers)

github_iconTop GitHub Comments

27reactions
benbesencommented, Feb 1, 2022

I’ve been struggling with the same issue. Honestly I think it is quite an important bug, which really influences my productivity with Sveltekit as every item or component that gets initialized globally in __layout, is added multiple times. I have this kind of setup in more then 50% of my projects and to really check if the site is working I always need to build it which is quite annoying. If anybody knows a workaround for development i would be very thankful.

11reactions
onderbakirtascommented, Feb 4, 2022

This issue happens to me as well. One of a few issues that makes SvelteKit is not that mature enough (this is stated in docs). Thanks for the comments above that made me understand the problem. Honestly I think this is a deal breaker for v1.0.

Read more comments on GitHub >

github_iconTop Results From Across the Web

svelte - Fetch requet is triggered twice on page load and after ...
__layout.svelte may run twice after browser reload in dev · Svelte module script being run multiple times. I'm using a workaround by adding...
Read more >
load from +page.ts called twice - why? : r/sveltejs - Reddit
As the title says: I have a setup that has some code in a +page. ts , that fetches some data from an...
Read more >
Lifecycle / beforeUpdate and afterUpdate • Svelte Tutorial
The beforeUpdate function schedules work to happen immediately before the DOM is updated. afterUpdate is its counterpart, used for running code once the...
Read more >
Handling common accessibility problems - MDN Web Docs
Users of older devices that might not have the latest browsers. ... Note: Like many things in web development, accessibility isn't about ...
Read more >
Hot Module Replacement - webpack
You can use the CLI to modify the webpack-dev-server configuration with the following command: webpack serve --hot-only . Now let's update the index.js...
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