sveltejs / kit

web development, streamlined
https://kit.svelte.dev
MIT License
18.05k stars 1.82k forks source link

svelte fetch causes api results to be cached #12351

Open sacrosanctic opened 1 week ago

sacrosanctic commented 1 week ago

Describe the bug

Using svelte fetch causes the same data to be returned.

// +page.ts
export const load = async ({ fetch }) => {
    const values: string[] = [];

    for (let i = 0; i < 5; i++) {
        const res = await fetch('https://api.chucknorris.io/jokes/random');
        const { value } = await res.json();
        values.push(value);
    }

    return { values };
};
<script lang="ts">
    export let data;
    const values = data.values;
</script>

{#each values as value, index}
    <h1>{index}: {value}</h1>
{/each}

Reproduction

npx degit https://github.com/sacrosanctic/bug-with-svelte-fetch
npm i
npm run dev

routes/a/+page.svelte should demonstrate the bug

Logs

No response

System Info

.

Severity

annoyance

Additional Information

https://discord.com/channels/457912077277855764/1251255888598274138

related conversation on discord

xactlyblue commented 1 week ago

Hey, thats my bug haha! I'm still pretty new to web development and stumbled onto this totally by mistake (I didn't read the documentation enough and forgot the .server part of the file name) so I'm not quite sure the impact of this, but it sure is interesting.