fix links for local static files

This commit is contained in:
Joseph Montanaro 2024-11-16 12:37:47 -05:00
parent 9b82175daa
commit 5b21f3b3a7

View File

@ -10,20 +10,35 @@
return null;
}
}
function ext(url) {
}
</script>
<script>
export let href;
export let rel = '';
export let rel = null;
let url = null;
try {
url = new URL(href);
}
catch {}
let isLocal = false;
if (href.startsWith('/') || url?.host === $page.url.host) {
isLocal = true;
}
// if href is not a valid url, assume that it's a relative link
const path = url?.pathname || href;
// set rel="external" on links to static files (i.e. local links with a dot in them)
if (isLocal && path.search(/\.\w+$/) > -1) {
rel = 'external';
}
</script>
{#if href.startsWith('/') || host(href) === $page.host}
<a data-sveltekit-preload-data="hover" {href} {rel}>
<a data-sveltekit-preload-data={isLocal ? 'hover' : null} {href} {rel}>
<slot></slot>
</a>
{:else}
<a {href}>
<slot></slot>
</a>
{/if}