blog/src/lib/Link.svelte
2021-10-19 14:30:48 -07:00

29 lines
508 B
Svelte

<script context="module">
import { page } from '$app/stores';
function host(url) {
try {
let u = new URL(url);
return u.host;
}
catch {
return null;
}
}
</script>
<script>
export let href; // we don't care about other attributes
</script>
{#if href.startsWith('/') || host(href) === $page.host}
<a sveltekit:prefetch {href}>
<slot></slot>
</a>
{:else}
<a {href}>
<slot></slot>
</a>
{/if}