refactored HEAD fetch

This commit is contained in:
Djeeberjr 2022-05-13 11:53:55 +02:00
parent 2ae7923efc
commit c1b4225b34

View File

@ -1,16 +1,16 @@
<script lang="ts"> <script lang="ts">
import { onMount } from "svelte"; import { onMount } from "svelte";
import getComponentForShare from "./contentType"; import getComponentForShare from "./contentType";
const regex = /filename="(.*)"/gm;
const slug = window.location.pathname.split("/").pop(); const slug = window.location.pathname.split("/").pop();
let component = null; let component = null;
let contentType: string; let contentType: string;
let filename: string; let filename: string;
onMount(async () => { async function getFileHeadAndComponent() {
const regex = /filename="(.*)"/gm;
const res = await fetch(`/s/${slug}`,{method:"HEAD"}); const res = await fetch(`/s/${slug}`,{method:"HEAD"});
contentType = res.headers.get("Content-Type"); contentType = res.headers.get("Content-Type");
let match; let match;
@ -22,25 +22,19 @@
filename = match[1]; filename = match[1];
} }
document.title = filename; return getComponentForShare(contentType,filename);
}
component = getComponentForShare(contentType,filename);
});
</script> </script>
<div class="dl"> <div class="dl">
<a href="/s/{slug}">Download</a> <a href="/s/{slug}">Download</a>
</div> </div>
<main> <main>
{#if component === null} {#await getFileHeadAndComponent()}
Loading data...
{:else}
{#await component}
Loading data... Loading data...
{:then v } {:then v }
<svelte:component this={v.default} slug={slug} contentType={contentType} filename={filename} /> <svelte:component this={v.default} slug={slug} contentType={contentType} filename={filename} />
{/await} {/await}
{/if}
</main> </main>
<style> <style>