Add the TermsFeed Privacy Consent cookie banner to a single-page application by pasting its snippet into the <head> of the HTML shell, the one HTML file every route loads. The code covers React and Vue with Vite 5 or later, Angular 17 or later, and SvelteKit 2 with Svelte 5, and setup takes about 10 minutes. Next.js has its own guide, because its root layout replaces the HTML shell: follow Install TermsFeed Privacy Consent on Next.js for a Next.js project.

Privacy Consent needs no npm package, no import, and no initialization call. The shell loads the script once per visit, and it keeps running while the router swaps views.

Why the script stays out of the bundle and the components

The script stays out of the bundle and the components, because the shell loads it before the application code has downloaded. The consent banner then appears without waiting for your bundle. TermsFeed does not update a copy in your build. A component that loads the script risks a second copy in the same page load, which prints [ee][205bde55] or [ee][1040992c] in the Console tab and stops.

The same rule excludes the scripts array of angular.json, which bundles a file into the build, and <svelte:head>, which adds the element from a component.

Paste the snippet into the HTML shell of each framework

Each framework keeps one HTML file that it serves for every route. Copy the snippet from the Install page of your consent banner, which holds your own token in place of YOUR_TOKEN, and paste it in the file this table names. Make the consent banner before this step with Create Your First TermsFeed Privacy Consent Banner.

Framework HTML shell Where the snippet goes
React with Vite index.html at the project root Before </head>
Vue with Vite index.html at the project root Before </head>
Angular src/index.html Before </head>
SvelteKit src/app.html Above %sveltekit.head%

React with Vite: index.html

Add the snippet to the <head> of index.html, beside the tags Vite generated. Vite leaves a script with an external https:// address as it is and does not bundle it.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" type="image/svg+xml" href="/vite.svg" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>My App</title>
    <!-- Privacy Consent by TermsFeed (https://www.termsfeed.com) -->
    <script type="text/javascript" src="https://cdn.termsfeedtag.com/plugins/pc/v1/YOUR_TOKEN/plugin.js" async></script>
    <!-- End Privacy Consent by TermsFeed (https://www.termsfeed.com) -->
  </head>
  <body>
    <div id="root"></div>
    <script type="module" src="/src/main.tsx"></script>
  </body>
</html>

Vue with Vite: index.html

Add the snippet to the <head> of index.html, which a project from create-vue keeps at the project root. The mount element is #app instead of #root. A Nuxt project has no index.html, so it adds the same element through the app.head.script option of nuxt.config.ts.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" href="/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>My App</title>
    <!-- Privacy Consent by TermsFeed (https://www.termsfeed.com) -->
    <script type="text/javascript" src="https://cdn.termsfeedtag.com/plugins/pc/v1/YOUR_TOKEN/plugin.js" async></script>
    <!-- End Privacy Consent by TermsFeed (https://www.termsfeed.com) -->
  </head>
  <body>
    <div id="app"></div>
    <script type="module" src="/src/main.ts"></script>
  </body>
</html>

Angular: src/index.html

Add the snippet to the <head> of src/index.html, which the Angular CLI copies into every build. Server-side rendering with @angular/ssr uses the same file.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>MyApp</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <!-- Privacy Consent by TermsFeed (https://www.termsfeed.com) -->
  <script type="text/javascript" src="https://cdn.termsfeedtag.com/plugins/pc/v1/YOUR_TOKEN/plugin.js" async></script>
  <!-- End Privacy Consent by TermsFeed (https://www.termsfeed.com) -->
</head>
<body>
  <app-root></app-root>
</body>
</html>

SvelteKit: src/app.html

Add the snippet to the <head> of src/app.html, above the %sveltekit.head% placeholder. SvelteKit renders every page into this template.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" href="%sveltekit.assets%/favicon.png" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <!-- Privacy Consent by TermsFeed (https://www.termsfeed.com) -->
    <script type="text/javascript" src="https://cdn.termsfeedtag.com/plugins/pc/v1/YOUR_TOKEN/plugin.js" async></script>
    <!-- End Privacy Consent by TermsFeed (https://www.termsfeed.com) -->
    %sveltekit.head%
  </head>
  <body data-sveltekit-preload-data="hover">
    <div style="display: contents">%sveltekit.body%</div>
  </body>
</html>

Google tags added from their templates in the TermsFeed app need no Consent Mode code in the shell. With your own Google Consent Mode v2 code, the order is the block, then any Google tag or container, then the snippet. Copy the block from Set Up Google Consent Mode v2.

Open the Preferences Center from a component

Call window.termsfeed_privacy_consent.v1.openPrivacyPreferences() from a click handler to reopen the choices. The method does nothing before the script is ready, and it then prints [ee][478058ef] in the Console tab. In a TypeScript project, declare the global once, in a file under src.

// src/termsfeed-privacy-consent.d.ts
export {};

declare global {
  interface Window {
    termsfeed_privacy_consent?: {
      openPrivacyPreferences: () => void;
    };
  }
}

A React component calls the method from onClick. The optional chaining keeps a very early click from throwing an error.

// src/components/PrivacyPreferencesButton.tsx
export function PrivacyPreferencesButton() {
  return (
    <button
      type="button"
      onClick={() => window.termsfeed_privacy_consent?.v1?.openPrivacyPreferences()}
    >
      Privacy Preferences
    </button>
  );
}

A Vue single-file component calls it from a function in <script setup>.

<!-- src/components/PrivacyPreferencesButton.vue -->
<script setup lang="ts">
function openPrivacyPreferences(): void {
  window.termsfeed_privacy_consent?.v1?.openPrivacyPreferences();
}
</script>

<template>
  <button type="button" @click="openPrivacyPreferences">Privacy Preferences</button>
</template>

An Angular standalone component calls it from a method.

// src/app/privacy-preferences-button.component.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-privacy-preferences-button',
  standalone: true,
  template: `<button type="button" (click)="open()">Privacy Preferences</button>`,
})
export class PrivacyPreferencesButtonComponent {
  open(): void {
    window.termsfeed_privacy_consent?.v1?.openPrivacyPreferences();
  }
}

A Svelte 5 component uses the onclick attribute.

<!-- src/lib/PrivacyPreferencesButton.svelte -->
<script lang="ts">
  function openPrivacyPreferences(): void {
    window.termsfeed_privacy_consent?.v1?.openPrivacyPreferences();
  }
</script>

<button type="button" onclick={openPrivacyPreferences}>Privacy Preferences</button>

A plain element with the class termsfeed-pc1-open-privacy-preferences needs no handler of your own, and it works when a component renders it after Privacy Consent has started. Compare the options in Let Visitors Reopen Preferences.

Why embeds mounted after the first page load stay empty

Embeds mounted after Privacy Consent starts stay empty, because Privacy Consent finds replaceable tags once and does not offer a method to look again. A replaceable tag that a component renders later, on another route or inside a modal, does not get an embed placeholder and never loads. It gets both at the next full page load, when the tag is on the page as Privacy Consent starts.

Render replaceable tags in markup that exists when the page first loads. A client-rendered application mounts its first view from JavaScript, so the most reliable place is the HTML shell or a server-rendered template. Choose the pattern for other cases with Use TermsFeed Privacy Consent in a Single-Page Application.

How route changes behave in a single-page application

A route change swaps the view without loading a new document, so Privacy Consent does not start again. The consent banner stays on screen until the visitor answers, and the vendor tags that ran on the first view keep running. A declined answer takes full effect at the next full page load, and a route change is not one.

An analytics tool counts a page view on a full page load, so each route change needs its own page view event. Send it with Use TermsFeed Privacy Consent in a Single-Page Application.

Build the application and serve the build, then test it in a private window. Run npm run build and npm run preview for Vite and SvelteKit projects, or ng build and a static server for Angular, and open the http://localhost address the tool prints. The Strict consent logic, the starting setting, asks each visitor wherever the visitor is. Standard in Settings does not ask a visitor in a US state, so pick a test location from Cookie Banner by Visitor Location.

  1. Open Chrome DevTools, select the Network tab, type termsfeedtag in the filter box, and reload. 7 requests appear with status 200, ending with settings.json twice.
  2. Navigate to another route through the router. No new termsfeedtag request appears, which shows that the shell loaded the script once.
  3. Select the Console tab. No line after [termsfeed] holds [ee] or [ww], and no uncaught TypeError appears.
  4. Filter the Network tab for events.push and click I Agree. A new request with status 201 appears.
  5. Click your Privacy Preferences button on a route the first load did not render. The Preferences Center opens.

Test I Decline in a new private window, after closing every other one, since a saved answer keeps the consent banner hidden. Follow the full list in Check That Your TermsFeed Privacy Consent Installation Works.

Common mistakes in React, Vue, Angular, and SvelteKit

Most problems in single-page applications come from loading the script from application code instead of the shell.

  • The script loads from a useEffect, onMounted, or ngOnInit hook. The consent banner appears late, and every vendor waits for it. Move the script to the HTML shell.
  • The script sits in the scripts array of angular.json. TermsFeed does not update the copy the build bundles. Put the snippet in src/index.html.
  • An analytics package stays in the bundle. A package such as react-ga4 or vue-gtag sends data before any answer. Remove it and add the tool as a vendor.
  • A replaceable tag renders after Privacy Consent starts. The embed never loads on that page view. Render the tag in the first markup.
  • A nonce-based Content Security Policy covers the shell. Privacy Consent inserts its files and vendor tags without your nonce. Allow them with the Content Security Policy reference.