vike-react 0.6.25 → 0.6.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -5,6 +5,7 @@ import { useStreamOptional } from 'react-streaming';
|
|
|
5
5
|
import { objectKeys } from '../../utils/objectKeys.js';
|
|
6
6
|
import { includes } from '../../utils/includes.js';
|
|
7
7
|
import { assert } from '../../utils/assert.js';
|
|
8
|
+
import { escapeJavaScriptExpression } from '../../utils/escapeJavaScriptExpression.js';
|
|
8
9
|
import { configsCumulative } from './configsCumulative.js';
|
|
9
10
|
import { configsClientSide } from './configsClientSide.js';
|
|
10
11
|
/**
|
|
@@ -59,9 +60,10 @@ function setPageContextConfigViaHook(config, pageContext) {
|
|
|
59
60
|
function apply(config, stream, pageContext) {
|
|
60
61
|
const { title } = config;
|
|
61
62
|
if (title) {
|
|
62
|
-
// No need to escape the injected
|
|
63
|
+
// No need to escape the injected nonce attribute — see https://github.com/vikejs/vike/blob/36201ddad5f5b527b244b24d548014ec86c204e4/packages/vike/src/server/runtime/renderPageServer/csp.ts#L45
|
|
63
64
|
const nonceAttr = pageContext.cspNonce ? ` nonce="${pageContext.cspNonce}"` : '';
|
|
64
|
-
const
|
|
65
|
+
const titleJs = escapeJavaScriptExpression(JSON.stringify(title));
|
|
66
|
+
const htmlSnippet = `<script${nonceAttr}>document.title = ${titleJs}</script>`;
|
|
65
67
|
stream.injectToStream(htmlSnippet);
|
|
66
68
|
}
|
|
67
69
|
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Escapes a JavaScript expression (e.g. the output of `JSON.stringify()`) so that it can be safely embedded inside an inline `<script>`.
|
|
3
|
+
*
|
|
4
|
+
* https://github.com/vikejs/vike/issues/3463
|
|
5
|
+
*/
|
|
6
|
+
export declare function escapeJavaScriptExpression(js: string): string;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Escapes a JavaScript expression (e.g. the output of `JSON.stringify()`) so that it can be safely embedded inside an inline `<script>`.
|
|
3
|
+
*
|
|
4
|
+
* https://github.com/vikejs/vike/issues/3463
|
|
5
|
+
*/
|
|
6
|
+
export function escapeJavaScriptExpression(js) {
|
|
7
|
+
return (js
|
|
8
|
+
// `<` is escaped so that the HTML parser never encounters `</script>` nor `<!--` inside the `<script>` — the JavaScript parser decodes `\u003c` back to `<`. (The input must be a JavaScript expression whose `<` only ever occurs inside string literals, such as `JSON.stringify()` output.)
|
|
9
|
+
.replace(/</g, '\\u003c')
|
|
10
|
+
// U+2028/U+2029 are escaped because a raw U+2028/U+2029 inside a JavaScript string literal is a syntax error in pre-ES2019 browsers.
|
|
11
|
+
.replace(/\u2028/g, '\\u2028')
|
|
12
|
+
.replace(/\u2029/g, '\\u2029'));
|
|
13
|
+
}
|