Skip to content

Commit f9e3325

Browse files
committed
docs: add example on using CSP nonce
1 parent 6f74e3a commit f9e3325

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,28 @@ ReactDOM.render(
4141
)
4242
```
4343

44+
### Content-Security Policy
45+
46+
If you have a [Content Security Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy) on your website, you might need to add some configuration for loading the Usercentrics script. The Web CMP v3 only supports loading the "_latest_" version from a static URL, so a [Subresource Integrity hash](https://developer.mozilla.org/en-US/docs/Web/Security/Defenses/Subresource_Integrity) cannot be calculated. However, a nonce attribute can be used instead. You can read more at:
47+
48+
- [nonce HTML global attribute | MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Global_attributes/nonce)
49+
- [Script Implementation - UsercentricsDocs](https://usercentrics.com/docs/web/features/v2/browser_ui/browser_ui/#adding-a-nonce-to-the-script-tag)
50+
- [Guides: Content Security Policy | Next.js](https://nextjs.org/docs/app/guides/content-security-policy#nonces)
51+
52+
#### Next.js App Router example
53+
54+
Assuming the `x-nonce` header is calculated in `proxy.ts` ([see example](https://nextjs.org/docs/app/guides/content-security-policy#adding-a-nonce-with-proxy)):
55+
56+
```tsx
57+
import { UsercentricsScript } from '@s-group/react-usercentrics'
58+
import { headers } from 'next/headers'
59+
60+
export default async function Layout() {
61+
const nonce = (await headers()).get('x-nonce')
62+
return <UsercentricsScript nonce={nonce} settingsId={USERCENTRICS_SETTINGS_ID} />
63+
}
64+
```
65+
4466
### Augmented type-checks for `ServiceId`
4567

4668
Service-specific code needs to target a specific `serviceId`, for example

0 commit comments

Comments
 (0)