-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Description
Describe the bug
I'm building a SvelteKit web app with an external REST server, and I've come up with a solution to minimize the amount of requests that are done to the REST server, and to allow content to be updated in real time.
The idea is that the list view (let's say /books
) fetches all books from the server, and stores it in a Svelte store. Then /books/[id]
can just read the Svelte store, and if that book is part of the stored content, it can show it immediately, no fetch needed. But if it's not part of the array of books, it can fetch that single book from the REST server and append it to the Svelte store. All pages use the Svelte store to show the books, so any updates to the store automatically update the pages. The content can be changed via websocket updates - when they come in, I can just update the Svelte store, all pages updates, everything is great!
However, when I reload the page in the browser, I am seeing the old version of the content show up very briefly, after which it updates to the new version of the content. It looks pretty weird and buggy and I have absolutely no idea how to work around it.
I think it's related to how SSR works: it seems to store my Svelte store server-side on the initial page load, but after that when the content in the store changes, and I refresh the browser, it first loads that very first stored content before updating it in the browser with the latest version. It might simply be a huge mistake to even try to use Svelte stores in a load function, but in that case I wonder how else I am supposed to architect my app.
Reproduction
I've made a minimal, reproducible example: https://github.com/kevinrenskers/sveltekit-reproduce. If you run this one and refresh the page, you'll see content flashes.
If you keep an eye on the terminal and the console in the browser, you will see that the first time you open the page, the content is fetched first from SSR, and then from the browser too. But when you refresh the page, the fetching is only done in the browser.
System Info
System:
OS: macOS 12.0
CPU: (12) x64 Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
Memory: 7.43 GB / 32.00 GB
Shell: 3.3.1 - /usr/local/bin/fish
Binaries:
Node: 16.5.0 - /usr/local/bin/node
Yarn: 1.22.10 - /usr/local/bin/yarn
npm: 7.19.1 - /usr/local/bin/npm
Browsers:
Brave Browser: 92.1.28.105
Firefox: 91.0
Safari: 15.0
npmPackages:
@sveltejs/kit: next => 1.0.0-next.144
svelte: ^3.34.0 => 3.42.1
Severity
serious, but I can work around it
Additional Information
I'm not sure how to answer the severity question. It seems like a serious issue, I can NOT work around it, but perhaps it's just me holding it wrong? Let me put it like this: the way I've built the app at the moment, things just look buggy and weird when you refresh the browser.