Set `page.data` programatically from `goto()` options while navigating
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 20.8k
- Forks
- 2.3k
- Avg merge
- 1d 16h
- Merged PRs (30d)
- 156
Description
Describe the problem
SvelteKit currently can preload data on hover and tab. I'm trying to make some routes in my website preload data on viewport too just like preloadCode
Having #12122 implemented would fix my issue, because then i would just be able to preloadData multiple pages at any time and navigating to any of those routes will just happen instantly.
Or even have data-sveltekit-preload-data="viewport" directly.
I tried to make a simple action to kinda solve my problem.
When a link enters the viewport. I preload its data then cache it in memory. but i couldn't find a way to set the page data programatically on navigation, instead of letting sveltekit re-run the server load function.
you can do that with Page.State but not with Page.Data.
so heres my action currently:
import { goto, preloadData } from '$app/navigation';
const cache = new Map<string, Record<string, unknown>>();
async function preloadFromHref(href: string) {
const result = await preloadData(href);
if (result.type === 'loaded') {
cache.set(href.split('?')[0], result.data);
} else {
preloadFromHref(result.location);
}
}
export function preload(node: HTMLAnchorElement) {
const observer = new IntersectionObserver((entries) => {
entries.forEach(async (entry) => {
if (entry.isIntersecting) {
await preloadFromHref(node.href);
observer.unobserve(entry.target);
}
});
});
node.addEventListener('click', async (e) => {
const cached = cache.get(node.href);
if (cached) {
e.preventDefault();
goto(node.href, {
// setting the page data programmatically instead of triggering the server load function
data: cached
});
}
});
observer.observe(node);
return {
update() {
observer.unobserve(node);
observer.observe(node);
},
destroy() {
observer.unobserve(node);
}
};
}
Describe the proposed solution
Add a data option to goto() that allows setting page data directly, bypassing the load function?
goto(node.href, {
// changing the page data directly instead of triggering the server load function
data: cached
});
I already see some problems in types, and making it not break the app if the data is incorrect. but i just though i share the idea.
Alternatives considered
No response
Importance
would make my life easier
Additional Information
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the $app/navigation goto() and preloadData() entry points referenced in the issue, along with the supplied preload action. Define how a new goto() data option should interact with navigation and server load behavior; done means cached preloaded data can be supplied during navigation without rerunning the server load function.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- frontend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100