How to Verify SeaText Script Loads Correctly in a Single-Page Application
To verify SeaText in an SPA, check the Network tab for the script, confirm no console errors, and call SeaText.reinit() or SeaText.update() on route changes. Keep the script tag only in the initial HTML;...
Direct Answer
To verify SeaText in a single-page application, call SeaText.reinit() or SeaText.update() when the route changes. Also confirm that the SeaText script tag appears only in the initial HTML. It should not be injected again on navigation.
This is the core of a correct SPA setup. If the script tag is present once, and the route hook calls the method after each route change, SeaText can keep working as users move through your app.
Why SPA Integration Differs from Traditional Sites
Multi-page sites load a new HTML document on every click. The browser finds the SeaText script again and runs it from scratch. SPAs load once and swap content in the browser. They never ask for a second copy of that HTML document.
SeaText uses an async attribute on its script tag. That means the script loads without blocking page render. SeaText also stores an ID in localStorage. That ID helps SeaText recognize the same visitor across routes.
Because the script is already running, you must tell SeaText when the visible content changed. A client-side route change does not reset the JavaScript environment. The router removes the old view and renders a new one. SeaText needs a signal to inspect that new view. That signal is SeaText.reinit() or SeaText.update().
If you add the script tag to every route instead, you may create multiple copies. The old copy can keep running. The new copy can start a new session. That can reset variants, translations, or personalization. Keep the script tag in the static entry file.
Quick Verification Steps
- Locate the entry point. Find where your SPA mounts. This is usually
index.html,main.js,main.tsx, orApp.vue. - Confirm the snippet is present. The SeaText snippet should appear once inside the
bodytag of that entry file. Do not put it in a component template that re-renders on route change. - Build and serve the app. Run your normal dev command such as
npm start,npm run serve, orng serve. - Inspect the Network tab. Reload the page. Verify that the SeaText script request returns HTTP 200 and that the response is JavaScript.
- Check the Console tab. Look for errors that mention
SeaText,seatext, or the script domain. A clean console means the script parsed and executed. - Trigger a SeaText feature. Change a language or show a variant. Confirm that the feature appears on the current route.
- Navigate to another route. Use your app router to change views. Open the Network tab again. The SeaText script should not request a second time.
These steps verify the initial load. They do not yet prove that SeaText re-initializes after navigation.
Verifying Route Changes
A clean initial load is only half the test. You also need to prove that SeaText reacts when the route changes. Use a route listener or a router hook to call SeaText.reinit() after the new route renders.
Here is a generic pattern. The event name will depend on your router. The important part is the call to the SeaText method.
window.addEventListener('navigation', function () {
if (window.SeaText && typeof window.SeaText.reinit === 'function') {
window.SeaText.reinit();
}
});
Replace navigation with the event your router exposes. If your integration uses update(), replace the method name.
Expected Results for Each Step
Use these checks after you add the listener:
- Open the app. Expected: the SeaText script loads once in the Network tab.
- Navigate to a new route. Expected: your listener fires and calls
SeaText.reinit()orSeaText.update()once. - Watch the Network tab again. Expected: no new request for the SeaText script file.
- Check the Console. Expected: no errors from SeaText.
- Trigger a SeaText feature. Expected: the feature applies on the new route.
If the script reloads, move the snippet back to the static entry file. If the feature does not apply, check that the route hook runs after the DOM updates.
Framework-Specific Route Hooks
Every router has a different place to hook route changes. The goal is the same: after the new route renders, call SeaText.reinit() or SeaText.update().
React Router
Use useLocation() and useEffect(). Put the observer inside your Router component.
import { useLocation } from 'react-router-dom';
import { useEffect } from 'react';
function SeaTextRouteObserver() {
const location = useLocation();
useEffect(() => {
if (window.SeaText && typeof window.SeaText.reinit === 'function') {
window.SeaText.reinit();
}
}, [location]);
return null;
}
Expected: when the pathname changes, the effect runs and SeaText re-initializes on the new page.
Next.js App Router
Use usePathname() in a client component. Render that component in your root layout so it stays mounted.
'use client';
import { usePathname } from 'next/navigation';
import { useEffect } from 'react';
export default function SeaTextRouteObserver() {
const pathname = usePathname();
useEffect(() => {
if (window.SeaText && typeof window.SeaText.reinit === 'function') {
window.SeaText.reinit();
}
}, [pathname]);
return null;
}
Expected: every App Router navigation changes pathname and triggers the effect.
Next.js Pages Router
Use the useRouter() hook and the routeChangeComplete event. Mount the observer in _app.js or _app.tsx.
import { useRouter } from 'next/router';
import { useEffect } from 'react';
export default function SeaTextRouteObserver() {
const router = useRouter();
useEffect(() => {
const handleRouteChange = () => {
if (window.SeaText && typeof window.SeaText.reinit === 'function') {
window.SeaText.reinit();
}
};
router.events.on('routeChangeComplete', handleRouteChange);
return () => router.events.off('routeChangeComplete', handleRouteChange);
}, [router]);
return null;
}
Expected: the handler fires after each completed route change and calls SeaText once.
Vue Router
Use the router afterEach hook in your router setup file.
router.afterEach(() => {
if (window.SeaText && typeof window.SeaText.reinit === 'function') {
window.SeaText.reinit();
}
});
Expected: after every navigation, the hook runs. The SeaText script tag stays in index.html.
Angular Router
Filter the NavigationEnd event in AppComponent or a root service.
import { Router, NavigationEnd } from '@angular/router';
import { filter } from 'rxjs/operators';
export class AppComponent {
constructor(private router: Router) {
this.router.events
.pipe(filter((event) => event instanceof NavigationEnd))
.subscribe(() => {
(window as any).SeaText?.reinit?.();
});
}
}
Expected: SeaText re-initializes after each completed Angular navigation.
If your installed version exposes update() instead of reinit(), replace the method name in these examples. Check with the vendor if you are unsure.
Common Issues and How to Fix Them
- Script loads but features do not appear. Check that
localStorageis accessible. Some privacy modes or sandboxed iframes block it. Also confirm that your route hook callsSeaText.reinit()orSeaText.update(). - Script requests on every route change. The snippet is probably inside a component that re-mounts. Move it to the static
index.html. - Cross-origin errors in the Console. If your SPA serves content from multiple domains, allow the SeaText script domain in your Content Security Policy and CORS headers.
- 404 on script request. The snippet URL may be outdated. Copy the latest snippet from your SeaText dashboard.
- Re-init runs more than once. Add the route observer in one place only. If you mount it in several components, you may call the method multiple times.
Limitations and When This Advice Does Not Apply
This guide covers client-side script loading and route re-initialization. It does not validate SeaText's AI rewriting, translation, or A/B testing logic. Those need separate functional tests.
The steps assume a standard SPA with one static entry file. If your SPA uses a micro-frontend architecture where each fragment injects its own scripts, the single-snippet rule may not hold. Consult SeaText support for that topology.
If your app uses server-side rendering, check that the snippet is not duplicated in server output. The route hooks still belong in the client.
Headless testing tools need to wait for script execution. A simple page load may not be enough. Wait for the SeaText method to be available before you assert results.
FAQ
Do I need to call SeaText.reinit() on every route change?
Yes, for client-side route changes. The route swap changes the visible DOM, so SeaText needs to re-scan the page. Do not reload the SeaText script during navigation. Keep the script tag in the initial HTML and call SeaText.reinit() or SeaText.update() from the router hook.
When should I use SeaText.update() instead of SeaText.reinit()?
Use the method your integration provides. As a general pattern, reinit() fits a full route change, while update() fits a smaller content change on the same page. If your build exposes only one method, use that one. Check with the vendor if you are unsure.
Can I load the SeaText script dynamically via JavaScript?
Not recommended. The snippet is designed for static placement in the initial HTML. Dynamic injection can cause race conditions, double-loading, or Content Security Policy failures.
What if my SPA uses a strict Content Security Policy?
Add the SeaText script domain to the script-src directive. Because the script communicates with SeaText endpoints, also allow connect-src. Then repeat the Network and Console checks.
How do I verify this in a production build?
Run npm run build and serve the output. Open DevTools, check the Network and Console tabs, navigate between routes, and confirm that the SeaText script does not reload.
Further reading and comparison sources
These external sources provide additional context for evaluating the topic. Their inclusion is not an endorsement.
Learn more
Visit the website for more information.