在Svelte Kit中只加载一次数据,以及何时使用page.js与layout.js来加载数据。

huangapple go评论54阅读模式
英文:

Loading data only once in Svelte Kit and when to use page.js vs layout.js for loading data

问题

  1. 如何仅在第一次页面加载时加载数据?

我目前正在从我的layout.js文件中加载一些全局数据,例如用户信息。由于我正在使用客户端路由,这些数据应该只需要在第一次页面加载时加载,而不是在每次导航时加载,但现在似乎是这样的。

layout.js

import { api } from '$lib/api';

export const prerender = true;

/** 
 * @type {import('./$types').LayoutLoad}
 * @return {Promise<Record<string,[]|string>>;}
 */
export async function load({ fetch, params }) {
    // ...

    const [userRes] = await Promise.all([
        api.get(fetch, `/user`),
    ]);

    return {
        user: userRes.status === 200 ? await userRes.json() : [],
    }
}

是否有替代方案,只在第一次页面加载时运行?

  1. 何时使用layout.js与page.js,是否会影响性能?

目前,我在layout.js中加载全局数据,并在page.js中加载单个页面数据。这会对性能产生影响吗?page.js是否在layout.js之后运行?在这种情况下,如果禁用SSR,则如果页面数据与其他API调用一起作为异步API调用加载,页面应该加载得更快,对吗?

是否有理由不将所有数据加载到同一个文件中,或者是否会影响性能?感谢您在这方面提供的任何意见和信息。

英文:

I have two questions related to SvelteKit, the first one is:

1. How to load data only on the first page load?

I'm currently loading some global data from my layout.js file, such as user information. Since I'm using client side routing, this should only need to be loaded on the first page load, and not on each navigation which it seems to be doing now.

layout.js

import { api } from &#39;$lib/api&#39;;

export const prerender = true;

/** 
 * @type {import(&#39;./$types&#39;).LayoutLoad}
 * @return {Promise&lt;Record&lt;string,[]|string&gt;&gt;}
 */
export async function load({ fetch, params }) {
    // ...

    const [userRes] = await Promise.all([
        api.get(fetch, `/user`),
	]);

    return {
        user: userRes.status === 200 ? await userRes.json() : [],
    }
}

Is there an alternative to this, which would only run on the first page load?

2. When to use layout.js vs page.js, does it impact performance?

Currently I'm loading the global data in layout.js, and I'm loading the individual page data in page.js. Does this have any impact on performance? Does page.js run after layout.js? In that case, if SSR is disabled, the page should load faster if the page data is also loaded as an asynchronous API call together with the other API calls?

Is there are reason that not all data should be loaded in the same file, or does it impact performance at all? Would appreciate any input and information on that part.

答案1

得分: 2

Question 1:
据我所知,只要布局没有重新渲染,它就不会执行load函数。除非涉及(group)布局,我似乎无法重现load函数在每次页面导航时都运行的问题。
这是一个示例:Stackblitz App
然后,如果将+layout.js文件移动到根目录,load函数就不会再次运行。

Question 2:
顺便说一下,+page.js+layout.js之间的区别在于,当渲染+layout.svelte时,+layout.js会运行,与之相关的+page.js也会运行。它们遵循它们的.svelte父组件,并且只在该组件被渲染时运行。

目前,我在layout.js中加载全局数据,在page.js中加载各个页面的数据。

是的,这是我通常使用+page.js+layout.js的方式。要明确,它不是真正的“全局”,因为只有使用特定布局的页面才具有该数据。

page.jslayout.js之后运行吗?

是的,在大多数情况下,+page.js会在+layout.js完成后运行。您还可以在上述Stackblitz应用程序中看到这个示例。

在这种情况下,如果禁用SSR,如果将页面数据与其他API调用一起作为异步API调用加载,页面应该加载更快,对吗?

这个我不太确定,理论上应该是这样,但这还取决于API调用需要多长时间。

这是我对您问题的回答和信息,如果我回答有误,请在下面评论以便我更正。

英文:
Question 1

As far as I know, as long as the layout isn't re-rendered, it shouldn't execute the load function. I can't seem to reproduce the issue of the load function running every page navigation except when there are (group) layouts are involved.
Here's an example: Stackblitz App

Then again, if the +layout.js file is moved to the root directory, the load function won't be run again.

Question 2

While we're at it, the difference between +page.js and +layout.js is that +layout.js runs when the +layout.svelte is being rendered, so does page.js with it's respective +page.svelte. They follow their .svelte parent and only run when that component is being rendered.

> Currently I'm loading the global data in layout.js, and I'm loading the individual page data in page.js

Yes, this is how I would typically use +page.js and +layout.js. To be clear, it's not "global" per se, since only pages that uses the specific layout has that data.

>Does page.js run after layout.js?

So yes, in most cases +page.js will run after +layout.js is done. You can also see that example in the Stackblitz app above.

> In that case, if SSR is disabled, the page should load faster if the page data is also loaded as an asynchronous API call together with the other API calls?

This I'm not to sure, in theory, it should be, but then again it depends on how long the API calls take

This was my input/information to your questions, I will definitely have something incorrect and if I do answer something incorrectly, please comment below so I can correct it.

huangapple
  • 本文由 发表于 2023年7月13日 16:30:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76677377.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定