Vite plugin for generating Content Security Policy (CSP) headers in single-page applications. Current stable version 4.0.1, requires Vite ^8.0.0. Supports automatic hashing of inline scripts/styles, SRI integrity, nonces for SSR, and policy validation. Key differentiators include first-class SPA support, dev-mode CSP enforcement, and framework integration for Vue Router and React. Previously released under csp-toolkit; actively maintained with frequent releases.
npm install vite-plugin-csp-guardVerified import paths — ran on the pinned version, not inferred.
Basic setup for vite-plugin-csp-guard with a CSP policy allowing self and specific external sources.
Upgrade Vite to 8.x or use version 3.x of this plugin.
Ensure Vite 7 is installed, or downgrade to version 2.x.
Set `dev: { run: true }` in the plugin options.Migrate to new import: `import csp from 'vite-plugin-csp-guard'`.
Always pass an object like `{ 'script-src': ["'self'"] }`.Configure SSR in Vite and use `nonce` option.
Use ES module import syntax: `import csp from 'vite-plugin-csp-guard'`
Use `import csp from 'vite-plugin-csp-guard'` without curly braces.
Add appropriate sources to your policy (e.g., 'self', specific domains).