Skip to content

Commit 6e42088

Browse files
resolve the SSR error related to sanitization in the base Search component.
1 parent 774c0f0 commit 6e42088

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/lib/components/ui/Search.svelte

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
<script lang="ts">
22
import { clsx } from "clsx";
33
import { browser } from "$app/environment";
4-
import IconSearch from "$lib/icons/IconSearch.svelte";
5-
import DOMPurify from "dompurify";
64
import { onMount } from "svelte";
5+
import IconSearch from "$lib/icons/IconSearch.svelte";
6+
7+
// SSR-safe HTML sanitizer: no-op on server
8+
let sanitize = $state<(html: string) => string>((html) => html);
9+
10+
// Hook up DOMPurify sanitize in browser
11+
onMount(async () => {
12+
if (browser) {
13+
const DOMPurify = (await import("dompurify")).default;
14+
sanitize = DOMPurify.sanitize;
15+
}
16+
});
717
818
// Define the props based on GOV.UK documentation
919
type Props = {
@@ -157,7 +167,7 @@
157167

158168
{#snippet LabelContent()}
159169
<label for={defaultId} class={derivedLabelClasses}>
160-
{@html DOMPurify.sanitize(label_text)}
170+
{@html sanitize(label_text)}
161171
</label>
162172
{/snippet}
163173

0 commit comments

Comments
 (0)