prefetch links
This commit is contained in:
31
src/lib/Link.svelte
Normal file
31
src/lib/Link.svelte
Normal file
@@ -0,0 +1,31 @@
|
||||
<script>
|
||||
import { page } from '$app/stores';
|
||||
export let href;
|
||||
let pageHost;
|
||||
page.subscribe(p => pageHost = p.host); // apparently this works?
|
||||
|
||||
function isLocal(url) {
|
||||
if (url.startsWith('/')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
let parsed = new URL(url);
|
||||
return parsed.host === pageHost;
|
||||
}
|
||||
catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<!-- we pass href explicitly so a11y doesn't scream at us -->
|
||||
{#if isLocal(href)}
|
||||
<a sveltekit:prefetch {href} {...$$props}>
|
||||
<slot></slot>
|
||||
</a>
|
||||
{:else}
|
||||
<a {href} {...$$props}>
|
||||
<slot></slot>
|
||||
</a>
|
||||
{/if}
|
||||
Reference in New Issue
Block a user