Replies: 7 comments 4 replies
-
This sounds like the feature request #7213 |
Beta Was this translation helpful? Give feedback.
-
Also of my #8573 Seems like this topic is becoming very asked about |
Beta Was this translation helpful? Give feedback.
-
You could try to return a promise from |
Beta Was this translation helpful? Give feedback.
-
A useful tip aside your question. |
Beta Was this translation helpful? Give feedback.
-
Thanks for all the replies. I'm going to try returning a promise from In the meanwhile there is still something I'm not quite getting: When I first load the page (for example by refreshing it), However if I do So, is the page rendered with SSR and then is all the javascript in |
Beta Was this translation helpful? Give feedback.
-
Ok, so after playing with promises I came up with something that works. It's not super elegant as I have to nest the promise into one the props (to avoid it being automatically resolved in
If anyone can think of a better way to achieve it, I'm all ears.
<script>
import { browser } from '$app/environment';
export async function load() {
let promiseFromServer = {};
let responseFromServer = false;
// we nest the promise so that it's not resolved automatically
// (we only want it resolved if we're on the server)
promiseFromServer.promise = [get the data];
if (!browser) {
// if we're on the server, we resolve the promise
responseFromServer = await promiseFromServer.promise;
}
return { promiseFromServer, responseFromServer }
}
<script>
export let data;
let { promiseFromServer, responseFromServer } = data;
let products = [];
let isLoading = true;
loadData();
async function loadData() {
let response;
if (!responseFromServer) {
// if we're here it means we're on the client side (because the promise was not resolved)
response = await promiseFromServer.promise;
}
else {
// if we're here it means we're on the server side
// the promise has already been resolved and we've got the data
// that's how we can get SSR
response = responseFromServer;
}
products = response.products;
isLoading = false;
}
</script>
{#if isLoading}
[some nice loading animation]
{:else}
{#if products.length}
<ul>
{#each products as product}
<li>
{product.name}
</li>
{/each}
</ul>
{/if}
{/if} |
Beta Was this translation helpful? Give feedback.
-
Have a look at my example repo: david-plugge/sveltekit-lazy-data |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Let's say I've got a page displaying a list of products:
+page.js
+page.svelte
Ok, so that works well. If I first load this page,
+page.js
will run on the server, pass the data to+page.svelte
, and sveltekit will render the page nicely. Then if I navigate to another page and come back to this page,+page.js
will run on the client.All good.
However, once
+page.js
is running on the client, when I click on the link going to that page, the site will pause while+page.js
is loading the data. I can use$navigating
to show some indication that it's loading, but the page will only render after the data has been passed to+page.svelte
. So there is a distinct lag between clicking on a link and the page being displayed.What I want instead is to render the page immediately after clicking on the link, and then load the data.
So of course I can do this in
+page.svelte
:Here I don't use
+page.js
at all. So when clicking on a link to that page, it will display the page instantly with the nice loading animation. When the data is loaded it will be displayed. But of course we don't get SSR.So I thought of mixing the two:
+page.js
+page.svelte
The idea is:
+page.js
+page.svelte
where we skip the code inonMount
+page.js
+page.svelte
inonMount
The result is that we get SSR on the first load of the page, and when we click on a link leading to this page from within the site we don't get any lag - it loads instantly and displays the nice loading animation until the data is loaded and then displayed.
There are however two issues with this:
+page.js
and in+page.svelte
. This could be avoided by creating a custom endpoint, but it's something additional to write.So my question is: am I missing something and is there a better way to do it? (probably)
Beta Was this translation helpful? Give feedback.
All reactions