Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Nested Layouts] Fetch data on layout-level #1689

Open
brillout opened this issue Jun 12, 2024 · 0 comments
Open

[Nested Layouts] Fetch data on layout-level #1689

brillout opened this issue Jun 12, 2024 · 0 comments
Labels
enhancement ✨ New feature or request high-prio 💫

Comments

@brillout
Copy link
Member

Description

With other frameworks, when navigating a nested layout, the outer layout isn't re-rendered and its data isn't re-fetched.

With Vike, the outer layout is re-rendered, and the data() hook of the page is executed again. The data() hook works at a page-level, and there isn't a way to define nested data() hooks.

Instead of implementing nested data() hooks, the plan is to integrate a data cache into the Vike extension of data fetching tools such as TanStack Query and Telefunc.

Note

Data fetching Vike Extensions already have a cache (they always need to), it's just a matter of fine-tuning the cache.

Note

The plan is still to re-render the outer layout (because I believe it's a less error-prone approach for everyone, including the Vike user as well as the website user). In practice, only the virtual DOM is "re-rendered", so the state of the outer layout is preserved and the real DOM doesn't change.

Technical details.

Approach A

vike-{react,vue,solid} provide a new component hook useDataCacheKey() that returns a key composed of:

  • The URL, but without the pathnames of all the nested layouts that the component doesn't belong to.
  • The timestamp when Vike changed the URL.

Vike extensions can then use the key to cache data. In other words, the behavior is exactly as the website user expects: data is re-fetched only when navigating the relevant parts of the UI.

Approach B

Alternatively, vike-{react,vue,solid} wrap the nested layouts in a stateful wrapper component (e.g. using React's useState()). Upon navigation, the state of the appropriate wrapper components is updated to trigger a re-render of nested layouts (the page and outer layout aren't re-rendered). But, so far, I think Approach A is superior. (For example, I don't see how Approach B can support the use case when the user clicks on the active link merely to refresh the page.)

@brillout brillout added enhancement ✨ New feature or request high-prio 💫 labels Jun 12, 2024
@brillout brillout changed the title [Nested Layouts] Cache data of outer layout [Nested Layouts] Fetch data on layout-level Jun 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement ✨ New feature or request high-prio 💫
Projects
None yet
Development

No branches or pull requests

1 participant