Skip to content

Commit

Permalink
feat: allow specifying nonce in global config (#448)
Browse files Browse the repository at this point in the history
This commit updates the signature of the `PartytownConfig` interface to include
the `nonce` property. The `nonce` property can now be specified globally and will be
subsequently set on `script` elements created by Partytown.
  • Loading branch information
arturovt authored Aug 28, 2023
1 parent ddf7344 commit 5d71e43
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/lib/main/snippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ export function snippet(
for (i = 0; i < scripts!.length; i++) {
script = doc.createElement('script');
script.innerHTML = scripts![i].innerHTML;
// We don't need to set a `nonce` on sandbox script since it is loaded via
// the `src` attribute. However, we do need to set a `nonce` on the current
// script because it contains an inline script. This action ensures that the
// script can still be executed even when inline scripts are blocked
// (assuming `unsafe-inline` is disabled and `nonce-*` is used instead).
script.nonce = config!.nonce;
doc.head.appendChild(script);
}

Expand Down
13 changes: 12 additions & 1 deletion src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,12 @@ export interface WebWorkerEnvironment {
$body$: HTMLElement;
$location$: Location;
$visibilityState$?: string;
$createNode$: (nodeName: string, instanceId: InstanceId, namespace?: string, prevInstance?: WorkerNode) => WorkerNode;
$createNode$: (
nodeName: string,
instanceId: InstanceId,
namespace?: string,
prevInstance?: WorkerNode
) => WorkerNode;
$currentScriptId$?: InstanceId;
$isInitialized$?: number;
$isLoading$?: number;
Expand Down Expand Up @@ -489,6 +494,12 @@ export interface PartytownConfig {
* Path to the service worker file. Defaults to `partytown-sw.js`.
*/
swPath?: string;
/**
* The nonce property may be set on script elements created by Partytown.
* This should be set only when dealing with content security policies
* and when the use of `unsafe-inline` is disabled (using `nonce-*` instead).
*/
nonce?: string;
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/react/snippet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import { partytownSnippet } from '@builder.io/partytown/integration';
*
* @public
*/
export interface PartytownProps extends PartytownConfig {
nonce?: string;
}
export interface PartytownProps extends PartytownConfig {}

/**
* The React `<Partytown/>` component should be placed within the `<head>`
Expand Down

1 comment on commit 5d71e43

@vercel
Copy link

@vercel vercel bot commented on 5d71e43 Aug 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.