Open claudijo opened 3 years ago
This is already possible using the session
store.
<script context="module">
import { fetchPost } from 'some/api/layer'
export async function preload({ params }, session) {
if (!session.posts.hasOwnProperty(slug))
session.posts[slug] = await fetchPost(slug);
return { post: session.posts[slug], slug };
}
</script>
The session will be serialized and passed to the client automatically during the SSR render process, so when the preload function re-runs client side the cache will already be warm.
I haven't tested whether mutating the session in a preload function is correctly recognized as a store mutation or not, though, so this might cause bugs if you need to reference that slice of the session store in other components.
Is your feature request related to a problem? Please describe. I am looking for a Sapper example or "native" support to render pages based on api requests in the preload function (ie. server or client side), and then cache the response on the client side. The idea is that if a fetched resource is cached, you will not need to do an additional request when navigating back to a page that is dependant on that resource.
For example, if you take the blog posts from the official sapper template installed with
npx degit "sveltejs/sapper-template#rollup" my-app
, each blog post will be re-fetched by the client when navigating between blog posts. A common use case, in my experience, is that you only want to fetch a resource once and then server the cached resource if available, or resort to fetching if there is a cache miss.Describe the solution you'd like Ideally stores would be client/server agnostic. If that is not an option, an example in the docs covering this use case would be valuable.
Below I present my current solution, which I find to be quite bloated. Comments or or suggestions for better solutions would be much appreciated.
Describe alternatives you've considered