Skip to main content

Calling fetch(url) inside a load function registers url as a dependency. Sometimes it's not appropriate to use fetch, in which case you can specify a dependency manually with the depends(url) function.

Since any string that begins with an [a-z]+: pattern is a valid URL, we can create custom invalidation keys like data:now.

Update src/routes/+layout.js to return a value directly rather than making a fetch call, and add the depends:

src/routes/+layout.js
export async function load({ depends }) {
	depends('data:now');

	return {
		now: Date.now()
	};
}

Now, update the invalidate call in src/routes/[...timezone]/+page.svelte:

src/routes/[...timezone]/+page.svelte
<script>
	import { onMount } from 'svelte';
	import { invalidate } from '$app/navigation';

	export let data;

	onMount(() => {
		const interval = setInterval(() => {
			invalidate('data:now');
		}, 1000);

		return () => {
			clearInterval(interval);
		};
	});
</script>

Next: invalidateAll



> tutorial@0.0.1 dev
> ./node_modules/vite/bin/vite.js dev


Forced re-optimization of dependencies

VITE v4.3.9 ready in 2801 ms

Local: http://localhost:5173/
Network: use --host to expose
press h to show help
12:56:28 AM [vite] Failed to load source map for /@vite/client.
12:56:28 AM [vite] Failed to load source map for /node_modules/vite/dist/client/env.mjs.