Content Security Policy

What is CSP?

Content Security Policy (CSP) is an added layer of security that helps to detect and mitigate certain types of attacks, including Cross-Site Scripting (XSS) and data injection attacks. These attacks are used for everything from data theft, to site defacement, to malware distribution.

Steps to integrate

Add a route plugin

To add the middleware plugin to your application, simply add a file named [email protected] to the routes folder. This file will be loaded with every request, allowing you to add headers, modify the response, and more.

Add the plugin
└── routes/
    ├── [email protected]         # The plugin which runs on every request (route middleware)
    ├── contact/
       └── index.mdx         #
    ├── about/
       └──          #
    ├── index.mdx             #
    └── layout.tsx            # This layout is used for all pages


This template provides very permissive backward-compatible defaults. It is highly recommended that you customize it to better suit your specific use case. As this is an advanced topic, you should take a closer look at MDN or to get a better understanding of CSP. Please note that in dev mode the Vite scripts have no nonce and will report. For this reason, the example will not add csp in dev mode.

import type { RequestHandler } from "";
import { isDev } from "";
export const onRequest: RequestHandler = event => {
  if (isDev) return; // Will not return CSP headers in dev mode
  const nonce =; // Your custom nonce logic here
  event.sharedMap.set("@nonce", nonce);
  const csp = [
    `default-src 'self' 'unsafe-inline'`,
    `font-src 'self'`,
    `img-src 'self' 'unsafe-inline' data:`,
    `script-src 'self' 'unsafe-inline' https: 'nonce-${nonce}' 'strict-dynamic'`,
    `style-src 'self' 'unsafe-inline'`,
    `frame-src 'self' 'nonce-${nonce}'`,
    `object-src 'none'`,
    `base-uri 'self'`,
  event.headers.set("Content-Security-Policy", csp.join("; "));

Add it to the service worker as well

import { component$, useServerData } from "";
import {
} from "";
import { RouterHead } from "./components/router-head/router-head";
import "./global.css";
export default component$(() => {
  const nonce = useServerData<string | undefined>("nonce");
  return (
        <meta charSet="utf-8" />
        <link rel="manifest" href="/manifest.json" />
        <RouterHead />
      <body lang="en">
        <RouterOutlet />
        <ServiceWorkerRegister nonce={nonce} />

Custom scripts

If you have custom script tags that you need to add the nonce to, you can use the useServerData hook to get the nonce from the server and add it to your script tags.

export default component$(() => {
  const nonce = useServerData<string | undefined>("nonce");
  return (
      <script nonce={nonce}>alert("Hello world")</script>

Validate your CSP

There is a great tool to validate your CSP:


Thanks to all the contributors who have helped make this documentation better!

  • tzdesign
  • jordanw66
  • mrhoodz
  • the-zimmermann
  • hamatoyogi