Svelte / SvelteKit Integration
Svelte component for the Ekoo widget with lifecycle management
data-ekooReqdata-ekoo-product-idReqdata-ekoo-localeautodata-ekoo-variantdata-ekoo-review-iddata-ekoo-on-eventdata-ekoo-directionnormaldata-ekoo-scale1data-ekoo-animationpulsedata-ekoo-animation-durationcontinuousdata-ekoo-always-openfalsedata-ekoo-show-imagetruedata-ekoo-not-fully-clickablefalsedata-ekoo-autoplayfalsedata-ekoo-show-transcriptfalsedata-ekoo-show-speed-buttonfalsedata-ekoo-closed-state-main-textdata-ekoo-closed-state-secondary-textdata-ekoo-modeautodata-shadow-modeopenwindow.EKOO_FORCE_SPA = truewindow.ekooShadowMode = "open"1. EkooWidget Component
Create a Svelte component that dynamically loads the script and uses onMount / onDestroy to manage the lifecycle.
1<script lang="ts">2 import { onMount, onDestroy } from 'svelte'34 export let websiteId: string5 export let productId: string6 export let locale: string = 'fr'78 const SCRIPT_URL = 'https://app.ekoo.co/widgets/widget-4.0.0-standalone.js'910 onMount(() => {11 let script = document.querySelector<HTMLScriptElement>(12 `script[src="${SCRIPT_URL}"]`13 )1415 if (!script) {16 script = document.createElement('script')17 script.src = SCRIPT_URL18 script.defer = true19 document.head.appendChild(script)2021 script.addEventListener('load', () => {22 window.ekooLoad?.()23 })24 } else {25 window.ekooLoad?.()26 }27 })2829 onDestroy(() => {30 window.ekooUnload?.()31 })32</script>3334<ekoo-widget35 data-ekoo={websiteId}36 data-ekoo-product-id={productId}37 data-ekoo-locale={locale}38></ekoo-widget>2. Usage in a Page
1<script lang="ts">2 import EkooWidget from '$lib/components/EkooWidget.svelte'3 import type { PageData } from './$types'45 export let data: PageData6</script>78<h1>{data.product.name}</h1>910<EkooWidget11 websiteId="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"12 productId={data.product.id}13 locale="fr"14/>3. SvelteKit Navigation
SvelteKit performs client-side navigations (soft navigation). When the user moves from one product to another, the component may not be destroyed and recreated. Use afterNavigate to reload the widget after each navigation:
1<script lang="ts">2 import { onMount, onDestroy } from 'svelte'3 import { afterNavigate } from '$app/navigation'45 export let websiteId: string6 export let productId: string7 export let locale: string = 'fr'89 const SCRIPT_URL = 'https://app.ekoo.co/widgets/widget-4.0.0-standalone.js'1011 function loadScript() {12 let script = document.querySelector<HTMLScriptElement>(13 `script[src="${SCRIPT_URL}"]`14 )15 if (!script) {16 script = document.createElement('script')17 script.src = SCRIPT_URL18 script.defer = true19 document.head.appendChild(script)20 script.addEventListener('load', () => window.ekooLoad?.())21 } else {22 window.ekooLoad?.()23 }24 }2526 onMount(() => loadScript())27 onDestroy(() => window.ekooUnload?.())2829 // Reload the widget after each SvelteKit navigation30 afterNavigate(() => {31 window.ekooReload?.()32 })33</script>3435<ekoo-widget36 data-ekoo={websiteId}37 data-ekoo-product-id={productId}38 data-ekoo-locale={locale}39></ekoo-widget>Script loading strategy
The widget-4.0.0-standalone.js script should only be loaded once. The component checks whether the script is already present in the DOM before adding it. On subsequent navigations, only ekooLoad() or ekooReload() is called.
Re-initialization
If the widget shows visual inconsistencies after several product changes, call window.ekooReload() to force a complete re-initialization.