For the complete documentation index, see llms.txt. This page is also available as Markdown.

🔒Content-Security-Policy

Add security to you application

react-dsfr supports strict Content-Security-Policy headers.

Nonce

Add your Content-Security-Policy either by configuring your server, or with the meta tag. Remember that a nonce MUST be generated per requests. Add the nonce to react-dsfr

To get the nonce, you have to enable SSR with Vite and either inject the value in process.env or in a additional custom meta tag in your index.html. You can also infer it by using the content of the Content-Security-Policy meta tag if you configured the header this way. For more information about SSR in Vite see the following page.

Vite SSR

This documentation is for Next projects using the App router.

You are in this case if you have a app/ directory at the root of your project.

The following assumes that you configured your CSP accordingly to the Next.js recommendation.

First configure the nonce in the <DsfrHead /> tag in your root layout:

The X-Nonce header is forwarded by the middleware.ts as suggested by Next.js.

If you use the NextAppDirEmotionCacheProvider, don't forget to add the nonce to it accordingly to what Emotion and MUI needs: <NextAppDirEmotionCacheProvider options={{ "key": "css", nonce, prepend: true }}>

Then you have to tell react-dsfr to read and forward the nonce injected to all other scripts and styles by adding doCheckNonce: true; to the startReactDsfr() function:

Next.js with old Pages Router is not supported. As per Next.js suggests, we recommended migrating to App Router.

App Router Migration

Once your injected the nonce into your app, add the following code:

Your framework isn't supported? Let's get in touch!

Trusted Types Policy

Trusted Types are supported out-of-the box.

When configuring your CSP you only have to add our policy names to your list:

We register two policies with only the createHTML hook. Policy names are react-dsfr and react-dsfr-asap

Custom policy name

You can configure a custom policy name if you need to by adding the trustedTypesPolicyName options to the startReactDsfr() function.

In Next.js App Router, trustedTypesPolicyName must also be set to the <DsfrHead /> component.

When a custom name is set, the suffix -asap is used for the second policy. Don't forget to add both to your header configuration.

Example:

If you set trustedTypesPolicyName: "my-app"

You header must be configured like so: trusted-types my-app my-app-asap;

Last updated

Was this helpful?