Replies: 1 comment 1 reply
-
This could be done in a server side fetch as described here: In your import { posts } from './data.js';
export function load() {
// The "content" field is not included in the response..
return {
summaries: posts.map((post) => ({
slug: post.slug,
title: post.title
}))
};
} Instead of transporting the data to the client side via a data attribute, the data will be inlined in the generated script tag of your component: <script>
{
__sveltekit_dev = {
env: {},
base: new URL(".", location).pathname.slice(0, -1),
element: document.currentScript.parentElement
};
// <---- Inlined data ahead ----->
const data = [null,{"type":"data","data":{summaries:[{slug:"welcome",title:"Welcome to the Aperture Science computer-aided enrichment center"},{slug:"safety",title:"Safety notice"},{slug:"cake",title:"This was a triumph"}]},"uses":{}}];
Promise.all([
import("/node_modules/@sveltejs/kit/src/runtime/client/start.js"),
import("/home/httpslearnsveltedev-hss2/.svelte-kit/generated/client/app.js")
]).then(([kit, app]) => {
kit.start(app, __sveltekit_dev.element, {
node_ids: [0, 4],
data,
form: null,
error: null
});
});
}
</script> I am not sure if this is possible with an universal fetch. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I am new to Svelte(Kit) and I understand that when carrying out a fetch "during server-side rendering, the response will be captured and inlined into the rendered HTML"
Coming from Nuxt3, I am used to the "pick" function I can apply when fetching data, which results in picking only the parts of the response I want to have (you pass the object keys to pick and your response object will only have the key-value pairs you specified by the passed keys).
Since I do not want to have big responses inlined in the HTML all the time, I came across
hooks.server.js
. It would work to return anew Response(JSON.stringify({stuff I picked from: "the actual response"}))
per request (determined via if statements on the url.pathname) and I am wondering if that is the "correct SvelteKit way" to achieve my desired behaviour.Thanks :)
Beta Was this translation helpful? Give feedback.
All reactions