Skip to content

Commit

Permalink
feat(react-query-next-experimental): introduce nonce prop (#7575)
Browse files Browse the repository at this point in the history
allow the inline script to be executed when CSP is enforced by including nonce to the script

Co-authored-by: Dominik Dorfmeister <[email protected]>
  • Loading branch information
hey-kevin and TkDodo authored Jun 21, 2024
1 parent 89e1fbe commit 1c7739e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ export interface HydrationStreamProviderProps<TShape> {
* onFlush is called on the server when the cache is flushed
*/
onFlush?: () => Array<TShape>
/**
* A nonce that'll allow the inline script to be executed when Content Security Policy is enforced
*/
nonce?: string
}

export function createHydrationStreamProvider<TShape>() {
Expand Down Expand Up @@ -82,6 +86,10 @@ export function createHydrationStreamProvider<TShape>() {
* onFlush is called on the server when the cache is flushed
*/
onFlush?: () => Array<TShape>
/**
* A nonce that'll allow the inline script to be executed when Content Security Policy is enforced
*/
nonce?: string
}) {
// unique id for the cache provider
const id = `__RQ${React.useId()}`
Expand Down Expand Up @@ -131,6 +139,7 @@ export function createHydrationStreamProvider<TShape>() {
return (
<script
key={count.current++}
nonce={props.nonce}
dangerouslySetInnerHTML={{
__html: html.join(''),
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const stream = createHydrationStreamProvider<DehydratedState>()
export function ReactQueryStreamedHydration(props: {
children: React.ReactNode
queryClient?: QueryClient
nonce?: string
options?: {
hydrate?: HydrateOptions
dehydrate?: DehydrateOptions
Expand Down Expand Up @@ -87,6 +88,7 @@ export function ReactQueryStreamedHydration(props: {
}}
// Handle BigInts etc using superjson
transformer={props.transformer}
nonce={props.nonce}
>
{props.children}
</stream.Provider>
Expand Down

0 comments on commit 1c7739e

Please sign in to comment.