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.

Svelte Stores causes rendering order mishaps

See original GitHub issue

Bug description

Trying to have an “all routes needs a login, except few whitelisted one, such as an about page” type of application

Adding this to the layout :

<script>
	[...]
	const whitelistedRoutes = ["/about"];
	
	$: isWhitelisted = whitelistedRoutes.some((route) => $isActive(route));
</script>

{#if isWhitelisted}
	<slot />
{:else}
	<h1>DO THE LOGIN</h1>
{/if}

works pretty well, but as soon as you add a store to any element referenced in this layout, going from a non-whitelisted page to a whitelisted page might flash the content of the non-whitelisted page very briefly (I’m talking about ~20 milliseconds here)

Environment

I created a new project using the default routify-starter template, added a basically empty about page, and this code to the src/pages/_layout.svelte file :

<!-- routify:options preload="proximity" -->
<script>
	import { isActive } from "@roxi/routify";
	import { test } from "../store";

	const whitelistedRoutes = ["/about"];

	$: isWhitelisted = whitelistedRoutes.some((route) => $isActive(route));
</script>

<nav>
	<a href="/">Home</a>
	<a href="/about">about</a>
</nav>

{#if isWhitelisted}
	<slot />
{:else}
	<h1>DO THE LOGIN</h1>
{/if}

{#if $test}
	<span>My store is active</span>
{/if}

with a basic store at src/store.js :

import { writable } from "svelte/store";

export const test = writable(false);

Live example

https://cranky-jepsen-7974d1.netlify.app/

Simply go back and forth between the home and about links, you should see the text / image of the home page appear briefly when going from the home page to the about page, despite the home page being “protected” by a “login”

This does not happen 100% of the time, so you might need to try to go back and forth more than once.

If the code in the above example :

import { test } from "../store";

[...]
{#if $test}
	<span>My store is active</span>
{/if}

wouldn’t be there, I could not get a single time the content of the home page to show.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jakobrosenbergcommented, Feb 19, 2021

Happy this got solved. I’m closing the issue for now.

0reactions
V-edcommented, Feb 19, 2021

I noticed you’re calling $ready() unconditionally while isWhitelisted is set reactively. This means $ready() will always be called before isWhitelisted.

Could you try this?

$: if (!isWhitelisted) $ready()

Yes, that’s actually the first thing I tried when you explained the decorators solution earlier, it unfortunately doesn’t work 😦

Read more comments on GitHub >

github_iconTop Results From Across the Web

When do Svelte derived stores cause a re-render?
Svelte uses a function called safe_not_equal to check whether a value potentially changed; this is not unique to derived stores. derived ...
Read more >
Svelte suspense (request for comments) · Issue #3203 - GitHub
Basically both rendering the next possible state (to perform data loads), while showing the current state that is completely interactive without ...
Read more >
Nested Stores: Awesome or a trap? : r/sveltejs - Reddit
$list is reassigned causing a reactive update, but Svelte only renders the changes. I've logged my stores to see exactly what changes happen ......
Read more >
Stores / Writable stores • Svelte Tutorial
In Svelte, we do this with stores. A store is simply an object with a subscribe method that allows interested parties to be...
Read more >
Svelte Is Unappealing | Hacker News
Debugging the svelte compiled code is easy though, it has an order of magnitude less indirection and layers than equivalent React code. Ever...
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