Learn how to integrate EasyFindAi into your Svelte application
Create a new Svelte component to load the EasyFindAi chatbot script.
<!-- EasyFindAiChatbot.svelte --><script>import { onMount, onDestroy } from 'svelte';export let apiKey = 'YOUR_API_KEY';let scriptElement;onMount(() => {// Create script elementscriptElement = document.createElement('script');scriptElement.src = 'https://easyfindai.com/api/chatbot-embed?apiKey=YOUR_API_KEY';// Append to documentdocument.body.appendChild(scriptElement);});onDestroy(() => {// Clean up when component is destroyedif (scriptElement && document.body.contains(scriptElement)) {document.body.removeChild(scriptElement);}});</script>
Import and use the component in your main layout or App component so it's available throughout your site.
<!-- App.svelte or your main layout component --><script>import EasyFindAiChatbot from './EasyFindAiChatbot.svelte';// Other imports...</script><main><!-- Your app content --><slot></slot><!-- Add the chatbot component --><EasyFindAiChatbot apiKey="YOUR_API_KEY" /></main>
If you're using SvelteKit with server-side rendering, make sure to only load the script on the client side:
<!-- EasyFindAiChatbot.svelte for SvelteKit --><script>import { onMount, onDestroy } from 'svelte';import { browser } from '$app/environment';export let apiKey = 'YOUR_API_KEY';let scriptElement;onMount(() => {// Only run in the browser, not during SSRif (browser) {// Create script elementscriptElement = document.createElement('script');scriptElement.src = 'https://easyfindai.com/api/chatbot-embed?apiKey=YOUR_API_KEY';// Append to documentdocument.body.appendChild(scriptElement);}});onDestroy(() => {// Clean up when component is destroyedif (browser && scriptElement && document.body.contains(scriptElement)) {document.body.removeChild(scriptElement);}});</script>
Note: The browser
import from $app/environment
is a SvelteKit-specific feature that helps determine if code is running in the browser or during server-side rendering.
That's it!
The chatbot will now be available on all pages of your Svelte application. No additional configuration is needed!