3

I'm using SvelteKit to build a static website with the static adapter for server rendering. I've noticed that in the generated HTML files, there's a script that fetches the same data that's already included in the page. For example, if I have a page listing blog posts and I fetch the posts data in the server-side load function, SvelteKit still includes a script to fetch the same data when rendering the HTML.

In my +page.ts file I am doing the following:

import type { Post } from '$lib/types';

export async function load({ fetch }) {
    const response = await fetch('../api/posts');
    const posts: Post[] = await response.json();
    return { posts };
}
export const prerender = true;

I'm confused about the purpose of this redundant script and whether it's necessary for static sites where all the data is pre-rendered. Is there a way to configure SvelteKit or the static adapter to prevent this redundant data fetching script from being included in the HTML output?

<script type="application/json" data-sveltekit-fetched data-url="/api/posts">
    {"status:200, ... all the data ... 
</script>

Any insights or suggestions on how to address this issue would be greatly appreciated. Thanks in advance!

1
  • 1
    load in +page.ts runs on server and client, only +page.server.ts is restricted to the server. Commented May 9, 2024 at 9:46

1 Answer 1

2

My suspicion is that this is part of client-side routing/rendering.
You could try turning that off:

// +page.ts / +layout.ts
export const csr = false;

Docs

You will get harder page transitions:

Links are handled by the browser with a full-page navigation.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a million. You are totally right. Unfortunately, setting csr = false breaks all page transitions, and this is not my goal. However, I've found that removing the script from the built HTML actually works fine for CSR. It seems that this script is not needed for CSR at all. I'm still puzzled as to why SvelteKit includes both the script in the HTML and the "static" endpoint api/posts for CSR, as the information appears to be redundant. On content-heavy pages, loading a huge amount of files and data to the server is not ideal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.