Create the widget component
Create a new client component file at Replace
components/TranslationWidget.tsx. This component dynamically loads the CDN script and initializes the widget once the document is ready:"YOUR_PUBLIC_KEY" with your public key from the JigsawStack dashboard.The "use client" directive is required because this component uses useEffect and accesses document and window, which are only available in the browser.How it works
The component creates a<script> element programmatically and appends it to document.body when the component mounts. Once the script loads, it checks whether the document is already fully loaded:
- If
document.readyState === "complete", it callsinitWidget()immediately. - Otherwise, it waits for the
windowloadevent before initializing.
useEffect cleanup function removes both the event listener and the script element when the component unmounts, preventing memory leaks.
TypeScript support
Thedeclare global block in the component extends the Window interface so TypeScript recognizes window.TranslationWidget. You can also move this declaration into a dedicated types.d.ts file at the root of your project to share it across your codebase.
For more details, see TypeScript Support.
Configuration options
See Configuration Options for the full list of parameters you can pass towindow.TranslationWidget.
Looking for the Pages Router guide? See Next.js Pages Router.