Skip to content

web search done right

dynamically import highlight-js. makes sure we don't send the somewhat big library to the frontend unless we actually need to render code.

Mikkel Denker3fa73e42e40f
diff --git a/frontend/src/app.d.ts b/frontend/src/app.d.ts
index bc139e0f..3277a498 100644
--- a/frontend/src/app.d.ts
+++ b/frontend/src/app.d.ts
@@ -17,7 +17,6 @@ declare global {
/** Show the divider between the header and the content */
divider?: boolean;
};
- highlightjs?: { HighlightAuto: typeof import('svelte-highlight').HighlightAuto };
};
}
// interface Platform {}
diff --git a/frontend/src/lib/components/Code.svelte b/frontend/src/lib/components/Code.svelte
index d0c3ef14..fcec5e8c 100644
--- a/frontend/src/lib/components/Code.svelte
+++ b/frontend/src/lib/components/Code.svelte
@@ -1,5 +1,5 @@
<script lang="ts">
- import { HighlightAuto } from 'svelte-highlight';
+ import DynamicHighlightAuto from './DynamicHighlightAuto.svelte';
export let code: string;
export let transparentBackground = false;
@@ -9,18 +9,18 @@
{#if isInline}
<span
- class="code rounded-md {transparentBackground ? '' : 'bg-base-200'} text-neutral-focus inline-block mx-1"
+ class="code rounded-md {transparentBackground
+ ? ''
+ : 'bg-base-200'} text-neutral-focus mx-1 inline-block"
>
<span class="overflow-auto px-2">
<code>{code}</code>
</span>
</span>
{:else}
- <div
- class="code rounded-lg {transparentBackground ? '' : 'bg-base-200'} text-neutral-focus my-2"
- >
+ <div class="code rounded-lg {transparentBackground ? '' : 'bg-base-200'} text-neutral-focus my-2">
<div class="overflow-auto px-3 py-2">
- <HighlightAuto {code} />
+ <DynamicHighlightAuto {code} />
</div>
</div>
{/if}
diff --git a/frontend/src/lib/components/DynamicHighlightAuto.svelte b/frontend/src/lib/components/DynamicHighlightAuto.svelte
new file mode 100644
index 00000000..fcd55167
--- /dev/null
+++ b/frontend/src/lib/components/DynamicHighlightAuto.svelte
@@ -0,0 +1,12 @@
+<script lang="ts">
+ import { onMount } from 'svelte';
+
+ export let code: string;
+
+ let component: any; // don't know what the type should be before import?
+ onMount(async () => {
+ component = (await import('svelte-highlight')).HighlightAuto;
+ });
+</script>
+
+<svelte:component this={component} {code} />
diff --git a/frontend/src/lib/globals.ts b/frontend/src/lib/globals.ts
index 73ac21a3..1b3fe73d 100644
--- a/frontend/src/lib/globals.ts
+++ b/frontend/src/lib/globals.ts
@@ -1,10 +1,5 @@
-import { browser } from '$app/environment';
-
export const globals = async (
globals?: App.PageData['globals'],
): Promise<App.PageData['globals']> => ({
- highlightjs: browser
- ? void 0
- : { HighlightAuto: (await import('svelte-highlight')).HighlightAuto },
...globals,
});
diff --git a/frontend/src/routes/search/Result.svelte b/frontend/src/routes/search/Result.svelte
index fc7ed9d1..ce9de8c8 100644
--- a/frontend/src/routes/search/Result.svelte
+++ b/frontend/src/routes/search/Result.svelte
@@ -13,8 +13,6 @@
import { improvements } from '$lib/improvements';
import TextSnippet from '$lib/components/TextSnippet.svelte';
import StackOverflowSnippet from './StackOverflowSnippet.svelte';
- import Article from '$lib/components/Article.svelte';
- import BiglogoBeta from '$lib/images/BiglogoBeta.svelte';
export let webpage: DisplayedWebpage;
export let resultIndex: number;