File tree Expand file tree Collapse file tree 2 files changed +27
-2
lines changed
packages/react-query-next-experimental/src Expand file tree Collapse file tree 2 files changed +27
-2
lines changed Original file line number Diff line number Diff line change 22
33import { useServerInsertedHTML } from 'next/navigation'
44import * as React from 'react'
5+ import { htmlEscapeJsonString } from './htmlescape'
56
67const serializedSymbol = Symbol ( 'serialized' )
78
@@ -83,7 +84,7 @@ export function createHydrationStreamProvider<TShape>() {
8384 } ) {
8485 // unique id for the cache provider
8586 const id = `__RQ${ React . useId ( ) } `
86- const idJSON = JSON . stringify ( id )
87+ const idJSON = htmlEscapeJsonString ( JSON . stringify ( id ) )
8788
8889 const [ transformer ] = React . useState (
8990 ( ) =>
@@ -124,7 +125,7 @@ export function createHydrationStreamProvider<TShape>() {
124125
125126 const html : Array < string > = [
126127 `window[${ idJSON } ] = window[${ idJSON } ] || [];` ,
127- `window[${ idJSON } ].push(${ serializedCacheArgs } );` ,
128+ `window[${ idJSON } ].push(${ htmlEscapeJsonString ( serializedCacheArgs ) } );` ,
128129 ]
129130 return (
130131 < script
Original file line number Diff line number Diff line change 1+ // --------------------------------------------------------------------------------
2+ //
3+ // copied from
4+ // https://github.com/vercel/next.js/blob/6bc07792a4462a4bf921a72ab30dc4ab2c4e1bda/packages/next/src/server/htmlescape.ts
5+ // License: https://github.com/vercel/next.js/blob/6bc07792a4462a4bf921a72ab30dc4ab2c4e1bda/packages/next/license.md
6+ //
7+ // --------------------------------------------------------------------------------
8+
9+ // This utility is based on https://github.com/zertosh/htmlescape
10+ // License: https://github.com/zertosh/htmlescape/blob/0527ca7156a524d256101bb310a9f970f63078ad/LICENSE
11+
12+ const ESCAPE_LOOKUP : { [ match : string ] : string } = {
13+ "&" : "\\u0026" ,
14+ ">" : "\\u003e" ,
15+ "<" : "\\u003c" ,
16+ "\u2028" : "\\u2028" ,
17+ "\u2029" : "\\u2029" ,
18+ } ;
19+
20+ export const ESCAPE_REGEX = / [ & > < \u2028 \u2029 ] / g;
21+
22+ export function htmlEscapeJsonString ( str : string ) : string {
23+ return str . replace ( ESCAPE_REGEX , ( match ) => ESCAPE_LOOKUP [ match ] ) ;
24+ }
You can’t perform that action at this time.
0 commit comments