proper usage of page store
This commit is contained in:
@@ -1,26 +1,23 @@
|
||||
<script>
|
||||
<script context="module">
|
||||
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;
|
||||
}
|
||||
|
||||
function host(url) {
|
||||
try {
|
||||
let parsed = new URL(url);
|
||||
return parsed.host === pageHost;
|
||||
let u = new URL(url);
|
||||
return u.host;
|
||||
}
|
||||
catch {
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<script>
|
||||
export let href;
|
||||
</script>
|
||||
|
||||
<!-- we pass href explicitly so a11y doesn't scream at us -->
|
||||
{#if isLocal(href)}
|
||||
{#if href.startsWith('/') || host(href) === $page.host}
|
||||
<a sveltekit:prefetch {href} {...$$props}>
|
||||
<slot></slot>
|
||||
</a>
|
||||
|
||||
Reference in New Issue
Block a user