vike 0.4.236-commit-cd565e9 → 0.4.236-commit-3c05c99
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.
- package/dist/cjs/client/shared/getJsonSerializedInHtml.js +10 -1
- package/dist/cjs/node/runtime/html/injectAssets/getHtmlTags.js +2 -2
- package/dist/cjs/node/runtime/html/serializeContext.js +20 -20
- package/dist/cjs/node/runtime/renderPage/renderPageAlreadyRouted.js +2 -2
- package/dist/cjs/node/runtime/renderPage.js +2 -2
- package/dist/cjs/utils/PROJECT_VERSION.js +1 -1
- package/dist/esm/client/runtime-client-routing/history.d.ts +5 -3
- package/dist/esm/client/runtime-client-routing/history.js +14 -41
- package/dist/esm/client/runtime-client-routing/initClientRouter.js +3 -2
- package/dist/esm/client/runtime-client-routing/utils.d.ts +0 -1
- package/dist/esm/client/runtime-client-routing/utils.js +0 -1
- package/dist/esm/client/shared/getJsonSerializedInHtml.js +10 -1
- package/dist/esm/node/runtime/html/injectAssets/getHtmlTags.js +2 -2
- package/dist/esm/node/runtime/html/serializeContext.d.ts +3 -3
- package/dist/esm/node/runtime/html/serializeContext.js +20 -20
- package/dist/esm/node/runtime/renderPage/renderPageAlreadyRouted.js +2 -2
- package/dist/esm/node/runtime/renderPage.js +2 -2
- package/dist/esm/utils/PROJECT_VERSION.d.ts +1 -1
- package/dist/esm/utils/PROJECT_VERSION.js +1 -1
- package/package.json +2 -2
|
@@ -26,6 +26,15 @@ function findAndParseJson(id) {
|
|
|
26
26
|
`Couldn't find #${id} (which Vike automatically injects in the HTML): make sure it exists (i.e. don't remove it and make sure your HTML isn't malformed)`);
|
|
27
27
|
const jsonStr = elem.textContent;
|
|
28
28
|
(0, utils_js_1.assert)(jsonStr);
|
|
29
|
-
const json = (0, parse_1.parse)(jsonStr
|
|
29
|
+
const json = (0, parse_1.parse)(jsonStr, {
|
|
30
|
+
// Prevent Google from crawling URLs in JSON:
|
|
31
|
+
// - https://github.com/vikejs/vike/pull/2603
|
|
32
|
+
// - https://github.com/brillout/json-serializer/blob/38edbb9945de4938da1e65d6285ce1dd123a45ef/test/main.spec.ts#L44-L95
|
|
33
|
+
reviver(_key, value) {
|
|
34
|
+
if (typeof value === 'string') {
|
|
35
|
+
return { replacement: value.replaceAll('\\/', '/'), resolved: false };
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
});
|
|
30
39
|
return json;
|
|
31
40
|
}
|
|
@@ -187,7 +187,7 @@ function mergeScriptEntries(pageAssets, viteDevScript) {
|
|
|
187
187
|
return scriptEntry;
|
|
188
188
|
}
|
|
189
189
|
function getPageContextJsonScriptTag(pageContext) {
|
|
190
|
-
const pageContextClientSerialized = (0, sanitizeJson_js_1.sanitizeJson)((0, serializeContext_js_1.getPageContextClientSerialized)(pageContext));
|
|
190
|
+
const pageContextClientSerialized = (0, sanitizeJson_js_1.sanitizeJson)((0, serializeContext_js_1.getPageContextClientSerialized)(pageContext, true));
|
|
191
191
|
const htmlTag = `<script id="${htmlElementIds_js_1.htmlElementId_pageContext}" type="application/json">${pageContextClientSerialized}</script>`;
|
|
192
192
|
// Used by contra.com https://github.com/gajus
|
|
193
193
|
// @ts-expect-error
|
|
@@ -195,7 +195,7 @@ function getPageContextJsonScriptTag(pageContext) {
|
|
|
195
195
|
return htmlTag;
|
|
196
196
|
}
|
|
197
197
|
function getGlobalContextJsonScriptTag(pageContext) {
|
|
198
|
-
const globalContextClientSerialized = (0, sanitizeJson_js_1.sanitizeJson)((0, serializeContext_js_1.getGlobalContextClientSerialized)(pageContext));
|
|
198
|
+
const globalContextClientSerialized = (0, sanitizeJson_js_1.sanitizeJson)((0, serializeContext_js_1.getGlobalContextClientSerialized)(pageContext, true));
|
|
199
199
|
const htmlTag = `<script id="${htmlElementIds_js_1.htmlElementId_globalContext}" type="application/json">${globalContextClientSerialized}</script>`;
|
|
200
200
|
return htmlTag;
|
|
201
201
|
}
|
|
@@ -30,7 +30,7 @@ const passToClientBuiltInPageContext = [
|
|
|
30
30
|
'data', // for data() hook
|
|
31
31
|
];
|
|
32
32
|
const pageToClientBuiltInPageContextError = ['pageProps', 'is404', isServerSideError_js_1.isServerSideError];
|
|
33
|
-
function getPageContextClientSerialized(pageContext) {
|
|
33
|
+
function getPageContextClientSerialized(pageContext, isHtmlJsonScript) {
|
|
34
34
|
const passToClientPageContext = getPassToClientPageContext(pageContext);
|
|
35
35
|
const getObj = (passToClientEntry) => {
|
|
36
36
|
if (passToClientEntry.once)
|
|
@@ -43,10 +43,10 @@ function getPageContextClientSerialized(pageContext) {
|
|
|
43
43
|
if (pageContextClientProps.some((prop) => (0, propKeys_js_1.getPropVal)(pageContext._pageContextInit, prop))) {
|
|
44
44
|
pageContextClient[pageContextInitIsPassedToClient_js_1.pageContextInitIsPassedToClient] = true;
|
|
45
45
|
}
|
|
46
|
-
const pageContextClientSerialized = serializeObject(pageContextClient, passToClientPageContext, getObj);
|
|
46
|
+
const pageContextClientSerialized = serializeObject(pageContextClient, passToClientPageContext, getObj, isHtmlJsonScript);
|
|
47
47
|
return pageContextClientSerialized;
|
|
48
48
|
}
|
|
49
|
-
function getGlobalContextClientSerialized(pageContext) {
|
|
49
|
+
function getGlobalContextClientSerialized(pageContext, isHtmlJsonScript) {
|
|
50
50
|
const passToClient = pageContext._passToClient;
|
|
51
51
|
const globalContext = pageContext._globalContext;
|
|
52
52
|
const getObj = ({ prop, once }) => {
|
|
@@ -63,13 +63,13 @@ function getGlobalContextClientSerialized(pageContext) {
|
|
|
63
63
|
};
|
|
64
64
|
const res = applyPassToClient(passToClient, getObj);
|
|
65
65
|
const globalContextClient = res.objClient;
|
|
66
|
-
const globalContextClientSerialized = serializeObject(globalContextClient, passToClient, getObj);
|
|
66
|
+
const globalContextClientSerialized = serializeObject(globalContextClient, passToClient, getObj, isHtmlJsonScript);
|
|
67
67
|
return globalContextClientSerialized;
|
|
68
68
|
}
|
|
69
|
-
function serializeObject(obj, passToClient, getObj) {
|
|
69
|
+
function serializeObject(obj, passToClient, getObj, isHtmlJsonScript) {
|
|
70
70
|
let serialized;
|
|
71
71
|
try {
|
|
72
|
-
serialized = serializeValue(obj);
|
|
72
|
+
serialized = serializeValue(obj, isHtmlJsonScript);
|
|
73
73
|
}
|
|
74
74
|
catch (err) {
|
|
75
75
|
const h = (s) => picocolors_1.default.cyan(s);
|
|
@@ -86,7 +86,7 @@ function serializeObject(obj, passToClient, getObj) {
|
|
|
86
86
|
(0, utils_js_1.assert)(objName);
|
|
87
87
|
const varName = `${objName}${(0, propKeys_js_1.getPropKeys)(prop).map(utils_js_1.getPropAccessNotation).join('')}`;
|
|
88
88
|
try {
|
|
89
|
-
serializeValue(value, varName);
|
|
89
|
+
serializeValue(value, isHtmlJsonScript, varName);
|
|
90
90
|
}
|
|
91
91
|
catch (err) {
|
|
92
92
|
propsNonSerializable.push(prop);
|
|
@@ -124,7 +124,7 @@ function serializeObject(obj, passToClient, getObj) {
|
|
|
124
124
|
obj[(0, propKeys_js_1.getPropKeys)(prop)[0]] = NOT_SERIALIZABLE_js_1.NOT_SERIALIZABLE;
|
|
125
125
|
});
|
|
126
126
|
try {
|
|
127
|
-
serialized = serializeValue(obj);
|
|
127
|
+
serialized = serializeValue(obj, isHtmlJsonScript);
|
|
128
128
|
}
|
|
129
129
|
catch (err) {
|
|
130
130
|
(0, utils_js_1.assert)(false);
|
|
@@ -132,20 +132,20 @@ function serializeObject(obj, passToClient, getObj) {
|
|
|
132
132
|
}
|
|
133
133
|
return serialized;
|
|
134
134
|
}
|
|
135
|
-
function serializeValue(value, varName) {
|
|
135
|
+
function serializeValue(value, isHtmlJsonScript, varName) {
|
|
136
136
|
return (0, stringify_1.stringify)(value, {
|
|
137
137
|
forbidReactElements: true,
|
|
138
138
|
valueName: varName,
|
|
139
139
|
// Prevent Google from crawling URLs in JSON:
|
|
140
|
-
//
|
|
141
|
-
//
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
140
|
+
// - https://github.com/vikejs/vike/pull/2603
|
|
141
|
+
// - https://github.com/brillout/json-serializer/blob/38edbb9945de4938da1e65d6285ce1dd123a45ef/test/main.spec.ts#L44-L95
|
|
142
|
+
replacer: !isHtmlJsonScript
|
|
143
|
+
? undefined
|
|
144
|
+
: (_key, value) => {
|
|
145
|
+
if (typeof value === 'string') {
|
|
146
|
+
return { replacement: value.replaceAll('/', '\\/'), resolved: false };
|
|
147
|
+
}
|
|
148
|
+
},
|
|
149
149
|
});
|
|
150
150
|
}
|
|
151
151
|
function getPassToClientPageContext(pageContext) {
|
|
@@ -158,7 +158,7 @@ function getPassToClientPageContext(pageContext) {
|
|
|
158
158
|
passToClient = (0, utils_js_1.unique)(passToClient);
|
|
159
159
|
return passToClient;
|
|
160
160
|
}
|
|
161
|
-
function getPageContextClientSerializedAbort(pageContext) {
|
|
161
|
+
function getPageContextClientSerializedAbort(pageContext, isHtmlJsonScript) {
|
|
162
162
|
(0, utils_js_1.assert)(pageContext._urlRedirect || pageContext._urlRewrite || pageContext.abortStatusCode);
|
|
163
163
|
(0, utils_js_1.assert)(pageContext._abortCall);
|
|
164
164
|
(0, utils_js_1.assert)(pageContext._abortCaller);
|
|
@@ -191,7 +191,7 @@ function getPageContextClientSerializedAbort(pageContext) {
|
|
|
191
191
|
onlyOnce: false,
|
|
192
192
|
});
|
|
193
193
|
}
|
|
194
|
-
return serializeValue(pageContext);
|
|
194
|
+
return serializeValue(pageContext, isHtmlJsonScript);
|
|
195
195
|
}
|
|
196
196
|
function applyPassToClient(passToClient, getObj) {
|
|
197
197
|
const objClient = {};
|
|
@@ -50,7 +50,7 @@ async function renderPageAlreadyRouted(pageContext) {
|
|
|
50
50
|
if (isError) {
|
|
51
51
|
(0, utils_js_1.objectAssign)(pageContext, { [isServerSideError_js_1.isServerSideError]: true });
|
|
52
52
|
}
|
|
53
|
-
const pageContextSerialized = (0, serializeContext_js_1.getPageContextClientSerialized)(pageContext);
|
|
53
|
+
const pageContextSerialized = (0, serializeContext_js_1.getPageContextClientSerialized)(pageContext, false);
|
|
54
54
|
const httpResponse = await (0, createHttpResponse_js_1.createHttpResponsePageContextJson)(pageContextSerialized);
|
|
55
55
|
(0, utils_js_1.objectAssign)(pageContext, { httpResponse });
|
|
56
56
|
return pageContext;
|
|
@@ -80,7 +80,7 @@ async function prerenderPage(pageContext) {
|
|
|
80
80
|
return { documentHtml, pageContextSerialized: null, pageContext };
|
|
81
81
|
}
|
|
82
82
|
else {
|
|
83
|
-
const pageContextSerialized = (0, serializeContext_js_1.getPageContextClientSerialized)(pageContext);
|
|
83
|
+
const pageContextSerialized = (0, serializeContext_js_1.getPageContextClientSerialized)(pageContext, false);
|
|
84
84
|
return { documentHtml, pageContextSerialized, pageContext };
|
|
85
85
|
}
|
|
86
86
|
}
|
|
@@ -424,10 +424,10 @@ pageContextNominalPageBegin, httpRequestId, pageContextErrorPageInit, globalCont
|
|
|
424
424
|
(0, utils_js_1.objectAssign)(pageContext, pageContextErrorPageInit, true);
|
|
425
425
|
(0, utils_js_1.augmentType)(pageContext, await (0, loadPageConfigsLazyServerSide_js_1.loadPageConfigsLazyServerSideAndExecHook)(pageContext));
|
|
426
426
|
// We include pageContextInit: we don't only serialize pageContextAbort because the error page may need to access pageContextInit
|
|
427
|
-
pageContextSerialized = (0, serializeContext_js_1.getPageContextClientSerialized)(pageContext);
|
|
427
|
+
pageContextSerialized = (0, serializeContext_js_1.getPageContextClientSerialized)(pageContext, false);
|
|
428
428
|
}
|
|
429
429
|
else {
|
|
430
|
-
pageContextSerialized = (0, serializeContext_js_1.getPageContextClientSerializedAbort)(pageContextAbort);
|
|
430
|
+
pageContextSerialized = (0, serializeContext_js_1.getPageContextClientSerializedAbort)(pageContextAbort, false);
|
|
431
431
|
}
|
|
432
432
|
const httpResponse = await (0, createHttpResponse_js_1.createHttpResponsePageContextJson)(pageContextSerialized);
|
|
433
433
|
const pageContextReturn = { httpResponse };
|
|
@@ -2,7 +2,8 @@ export { pushHistoryState };
|
|
|
2
2
|
export { replaceHistoryStateOriginal };
|
|
3
3
|
export { onPopStateBegin };
|
|
4
4
|
export { saveScrollPosition };
|
|
5
|
-
export {
|
|
5
|
+
export { initHistoryState };
|
|
6
|
+
export { monkeyPatchHistoryAPI };
|
|
6
7
|
export type { HistoryInfo };
|
|
7
8
|
export type { ScrollPosition };
|
|
8
9
|
type StateEnhanced = {
|
|
@@ -17,7 +18,8 @@ type ScrollPosition = {
|
|
|
17
18
|
};
|
|
18
19
|
declare function saveScrollPosition(): void;
|
|
19
20
|
declare function pushHistoryState(url: string, overwriteLastHistoryEntry: boolean): void;
|
|
20
|
-
declare function replaceHistoryStateOriginal(state: unknown, url:
|
|
21
|
+
declare function replaceHistoryStateOriginal(state: unknown, url: string): void;
|
|
22
|
+
declare function monkeyPatchHistoryAPI(): void;
|
|
21
23
|
type HistoryInfo = {
|
|
22
24
|
url: `/${string}`;
|
|
23
25
|
state: StateEnhanced;
|
|
@@ -27,4 +29,4 @@ declare function onPopStateBegin(): {
|
|
|
27
29
|
previous: HistoryInfo;
|
|
28
30
|
current: HistoryInfo;
|
|
29
31
|
};
|
|
30
|
-
declare function
|
|
32
|
+
declare function initHistoryState(): void;
|
|
@@ -2,15 +2,12 @@ export { pushHistoryState };
|
|
|
2
2
|
export { replaceHistoryStateOriginal };
|
|
3
3
|
export { onPopStateBegin };
|
|
4
4
|
export { saveScrollPosition };
|
|
5
|
-
export {
|
|
5
|
+
export { initHistoryState };
|
|
6
|
+
export { monkeyPatchHistoryAPI };
|
|
6
7
|
import { getCurrentUrl } from '../shared/getCurrentUrl.js';
|
|
7
|
-
import { assert, assertUsage, getGlobalObject, isObject
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
previous: undefined,
|
|
11
|
-
});
|
|
12
|
-
initHistory(); // we redundantly call initHistory() to ensure it's called early
|
|
13
|
-
globalObject.previous = getHistoryInfo();
|
|
8
|
+
import { assert, assertUsage, getGlobalObject, isObject } from './utils.js';
|
|
9
|
+
initHistoryState(); // we redundantly call initHistoryState() to ensure it's called early
|
|
10
|
+
const globalObject = getGlobalObject('runtime-client-routing/history.ts', { previous: getHistoryInfo() });
|
|
14
11
|
// `window.history.state === null` when:
|
|
15
12
|
// - The very first render
|
|
16
13
|
// - Click on `<a href="#some-hash" />`
|
|
@@ -44,7 +41,7 @@ function enhance(stateNotEnhanced) {
|
|
|
44
41
|
_isVikeEnhanced: true,
|
|
45
42
|
};
|
|
46
43
|
}
|
|
47
|
-
|
|
44
|
+
assert(isVikeEnhanced(stateVikeEnhanced));
|
|
48
45
|
return stateVikeEnhanced;
|
|
49
46
|
}
|
|
50
47
|
function getState() {
|
|
@@ -54,7 +51,7 @@ function getState() {
|
|
|
54
51
|
// - Therefore, we have to monkey patch history.pushState() and history.replaceState()
|
|
55
52
|
// - Therefore, we need the assert() below to ensure history.state has been enhanced by Vike
|
|
56
53
|
// - If users stumble upon this assert() then let's make it a assertUsage()
|
|
57
|
-
|
|
54
|
+
assert(isVikeEnhanced(state), { state });
|
|
58
55
|
return state;
|
|
59
56
|
}
|
|
60
57
|
function getStateNotEnhanced() {
|
|
@@ -93,7 +90,6 @@ function pushHistoryState(url, overwriteLastHistoryEntry) {
|
|
|
93
90
|
function replaceHistoryState(state, url) {
|
|
94
91
|
const url_ = url ?? null; // Passing `undefined` chokes older Edge versions.
|
|
95
92
|
window.history.replaceState(state, '', url_);
|
|
96
|
-
assertIsVikeEnhanced(getState());
|
|
97
93
|
}
|
|
98
94
|
function replaceHistoryStateOriginal(state, url) {
|
|
99
95
|
// Bypass all monkey patches.
|
|
@@ -104,9 +100,7 @@ function replaceHistoryStateOriginal(state, url) {
|
|
|
104
100
|
// - history.pushState()
|
|
105
101
|
// - history.replaceState()
|
|
106
102
|
function monkeyPatchHistoryAPI() {
|
|
107
|
-
|
|
108
|
-
return;
|
|
109
|
-
globalObject.monkeyPatched = true;
|
|
103
|
+
;
|
|
110
104
|
['pushState', 'replaceState'].forEach((funcName) => {
|
|
111
105
|
const funcOriginal = window.history[funcName].bind(window.history);
|
|
112
106
|
window.history[funcName] = (stateOriginal = {}, ...rest) => {
|
|
@@ -120,21 +114,11 @@ function monkeyPatchHistoryAPI() {
|
|
|
120
114
|
triggeredBy: 'user',
|
|
121
115
|
...stateOriginal,
|
|
122
116
|
};
|
|
123
|
-
|
|
124
|
-
funcOriginal(stateEnhanced, ...rest);
|
|
125
|
-
assertIsVikeEnhanced(getState());
|
|
117
|
+
assert(isVikeEnhanced(stateEnhanced));
|
|
118
|
+
const ret = funcOriginal(stateEnhanced, ...rest);
|
|
126
119
|
globalObject.previous = getHistoryInfo();
|
|
127
|
-
|
|
128
|
-
assert(deepEqual(window.history.state, stateEnhanced));
|
|
129
|
-
queueMicrotask(() => {
|
|
130
|
-
if (deepEqual(window.history.state, stateEnhanced))
|
|
131
|
-
return;
|
|
132
|
-
Object.assign(stateEnhanced, window.history.state);
|
|
133
|
-
replaceHistoryStateOriginal(stateEnhanced, rest[1]);
|
|
134
|
-
assert(deepEqual(window.history.state, stateEnhanced));
|
|
135
|
-
});
|
|
120
|
+
return ret;
|
|
136
121
|
};
|
|
137
|
-
window.history[funcName]._isVikeMonkeyPatch = true;
|
|
138
122
|
});
|
|
139
123
|
}
|
|
140
124
|
function isVikeEnhanced(state) {
|
|
@@ -152,16 +136,6 @@ function isVikeEnhanced(state) {
|
|
|
152
136
|
}
|
|
153
137
|
return false;
|
|
154
138
|
}
|
|
155
|
-
function assertIsVikeEnhanced(state) {
|
|
156
|
-
if (isVikeEnhanced(state))
|
|
157
|
-
return;
|
|
158
|
-
assert(false, {
|
|
159
|
-
state,
|
|
160
|
-
// TO-DO/eventually: remove _isVikeMonkeyPatch debug info to save KBs
|
|
161
|
-
pushStateIsVikeMonkeyPatch: window.history.pushState._isVikeMonkeyPatch,
|
|
162
|
-
replaceStateIsVikeMonkeyPatch: window.history.replaceState._isVikeMonkeyPatch,
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
139
|
function getHistoryInfo() {
|
|
166
140
|
return {
|
|
167
141
|
url: getCurrentUrl(),
|
|
@@ -173,12 +147,11 @@ function onPopStateBegin() {
|
|
|
173
147
|
const isHistoryStateEnhanced = window.history.state !== null;
|
|
174
148
|
if (!isHistoryStateEnhanced)
|
|
175
149
|
enhanceHistoryState();
|
|
176
|
-
|
|
150
|
+
assert(isVikeEnhanced(window.history.state));
|
|
177
151
|
const current = getHistoryInfo();
|
|
178
152
|
globalObject.previous = current;
|
|
179
153
|
return { isHistoryStateEnhanced, previous, current };
|
|
180
154
|
}
|
|
181
|
-
function
|
|
182
|
-
|
|
183
|
-
enhanceHistoryState(); // enhance very first window.history.state which is `null`
|
|
155
|
+
function initHistoryState() {
|
|
156
|
+
enhanceHistoryState();
|
|
184
157
|
}
|
|
@@ -6,7 +6,7 @@ import { initOnLinkClick } from './initOnLinkClick.js';
|
|
|
6
6
|
import { scrollRestoration_init } from './scrollRestoration.js';
|
|
7
7
|
import { autoSaveScrollPosition } from './setScrollPosition.js';
|
|
8
8
|
import { initLinkPrefetchHandlers } from './prefetch.js';
|
|
9
|
-
import {
|
|
9
|
+
import { initHistoryState, monkeyPatchHistoryAPI } from './history.js';
|
|
10
10
|
async function initClientRouter() {
|
|
11
11
|
// Init navigation history and scroll restoration
|
|
12
12
|
initHistoryAndScroll();
|
|
@@ -29,7 +29,8 @@ async function renderFirstPage() {
|
|
|
29
29
|
}
|
|
30
30
|
function initHistoryAndScroll() {
|
|
31
31
|
scrollRestoration_init();
|
|
32
|
-
|
|
32
|
+
monkeyPatchHistoryAPI();
|
|
33
|
+
initHistoryState(); // we redundantly call initHistoryState() to ensure it's called early
|
|
33
34
|
autoSaveScrollPosition();
|
|
34
35
|
// Handle back-/forward navigation
|
|
35
36
|
initOnPopState();
|
|
@@ -24,6 +24,15 @@ function findAndParseJson(id) {
|
|
|
24
24
|
`Couldn't find #${id} (which Vike automatically injects in the HTML): make sure it exists (i.e. don't remove it and make sure your HTML isn't malformed)`);
|
|
25
25
|
const jsonStr = elem.textContent;
|
|
26
26
|
assert(jsonStr);
|
|
27
|
-
const json = parse(jsonStr
|
|
27
|
+
const json = parse(jsonStr, {
|
|
28
|
+
// Prevent Google from crawling URLs in JSON:
|
|
29
|
+
// - https://github.com/vikejs/vike/pull/2603
|
|
30
|
+
// - https://github.com/brillout/json-serializer/blob/38edbb9945de4938da1e65d6285ce1dd123a45ef/test/main.spec.ts#L44-L95
|
|
31
|
+
reviver(_key, value) {
|
|
32
|
+
if (typeof value === 'string') {
|
|
33
|
+
return { replacement: value.replaceAll('\\/', '/'), resolved: false };
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
});
|
|
28
37
|
return json;
|
|
29
38
|
}
|
|
@@ -182,7 +182,7 @@ function mergeScriptEntries(pageAssets, viteDevScript) {
|
|
|
182
182
|
return scriptEntry;
|
|
183
183
|
}
|
|
184
184
|
function getPageContextJsonScriptTag(pageContext) {
|
|
185
|
-
const pageContextClientSerialized = sanitizeJson(getPageContextClientSerialized(pageContext));
|
|
185
|
+
const pageContextClientSerialized = sanitizeJson(getPageContextClientSerialized(pageContext, true));
|
|
186
186
|
const htmlTag = `<script id="${htmlElementId_pageContext}" type="application/json">${pageContextClientSerialized}</script>`;
|
|
187
187
|
// Used by contra.com https://github.com/gajus
|
|
188
188
|
// @ts-expect-error
|
|
@@ -190,7 +190,7 @@ function getPageContextJsonScriptTag(pageContext) {
|
|
|
190
190
|
return htmlTag;
|
|
191
191
|
}
|
|
192
192
|
function getGlobalContextJsonScriptTag(pageContext) {
|
|
193
|
-
const globalContextClientSerialized = sanitizeJson(getGlobalContextClientSerialized(pageContext));
|
|
193
|
+
const globalContextClientSerialized = sanitizeJson(getGlobalContextClientSerialized(pageContext, true));
|
|
194
194
|
const htmlTag = `<script id="${htmlElementId_globalContext}" type="application/json">${globalContextClientSerialized}</script>`;
|
|
195
195
|
return htmlTag;
|
|
196
196
|
}
|
|
@@ -15,8 +15,8 @@ type PageContextSerialization = {
|
|
|
15
15
|
_globalContext: GlobalContextServerInternal;
|
|
16
16
|
isClientSideNavigation: boolean;
|
|
17
17
|
};
|
|
18
|
-
declare function getPageContextClientSerialized(pageContext: PageContextSerialization): string;
|
|
19
|
-
declare function getGlobalContextClientSerialized(pageContext: PageContextSerialization): string;
|
|
18
|
+
declare function getPageContextClientSerialized(pageContext: PageContextSerialization, isHtmlJsonScript: boolean): string;
|
|
19
|
+
declare function getGlobalContextClientSerialized(pageContext: PageContextSerialization, isHtmlJsonScript: boolean): string;
|
|
20
20
|
type PassToClient = (string | {
|
|
21
21
|
prop: string;
|
|
22
22
|
once?: boolean;
|
|
@@ -27,4 +27,4 @@ declare function getPageContextClientSerializedAbort(pageContext: Record<string,
|
|
|
27
27
|
_urlRewrite: string;
|
|
28
28
|
} | {
|
|
29
29
|
abortStatusCode: number;
|
|
30
|
-
})): string;
|
|
30
|
+
}), isHtmlJsonScript: false): string;
|
|
@@ -25,7 +25,7 @@ const passToClientBuiltInPageContext = [
|
|
|
25
25
|
'data', // for data() hook
|
|
26
26
|
];
|
|
27
27
|
const pageToClientBuiltInPageContextError = ['pageProps', 'is404', isServerSideError];
|
|
28
|
-
function getPageContextClientSerialized(pageContext) {
|
|
28
|
+
function getPageContextClientSerialized(pageContext, isHtmlJsonScript) {
|
|
29
29
|
const passToClientPageContext = getPassToClientPageContext(pageContext);
|
|
30
30
|
const getObj = (passToClientEntry) => {
|
|
31
31
|
if (passToClientEntry.once)
|
|
@@ -38,10 +38,10 @@ function getPageContextClientSerialized(pageContext) {
|
|
|
38
38
|
if (pageContextClientProps.some((prop) => getPropVal(pageContext._pageContextInit, prop))) {
|
|
39
39
|
pageContextClient[pageContextInitIsPassedToClient] = true;
|
|
40
40
|
}
|
|
41
|
-
const pageContextClientSerialized = serializeObject(pageContextClient, passToClientPageContext, getObj);
|
|
41
|
+
const pageContextClientSerialized = serializeObject(pageContextClient, passToClientPageContext, getObj, isHtmlJsonScript);
|
|
42
42
|
return pageContextClientSerialized;
|
|
43
43
|
}
|
|
44
|
-
function getGlobalContextClientSerialized(pageContext) {
|
|
44
|
+
function getGlobalContextClientSerialized(pageContext, isHtmlJsonScript) {
|
|
45
45
|
const passToClient = pageContext._passToClient;
|
|
46
46
|
const globalContext = pageContext._globalContext;
|
|
47
47
|
const getObj = ({ prop, once }) => {
|
|
@@ -58,13 +58,13 @@ function getGlobalContextClientSerialized(pageContext) {
|
|
|
58
58
|
};
|
|
59
59
|
const res = applyPassToClient(passToClient, getObj);
|
|
60
60
|
const globalContextClient = res.objClient;
|
|
61
|
-
const globalContextClientSerialized = serializeObject(globalContextClient, passToClient, getObj);
|
|
61
|
+
const globalContextClientSerialized = serializeObject(globalContextClient, passToClient, getObj, isHtmlJsonScript);
|
|
62
62
|
return globalContextClientSerialized;
|
|
63
63
|
}
|
|
64
|
-
function serializeObject(obj, passToClient, getObj) {
|
|
64
|
+
function serializeObject(obj, passToClient, getObj, isHtmlJsonScript) {
|
|
65
65
|
let serialized;
|
|
66
66
|
try {
|
|
67
|
-
serialized = serializeValue(obj);
|
|
67
|
+
serialized = serializeValue(obj, isHtmlJsonScript);
|
|
68
68
|
}
|
|
69
69
|
catch (err) {
|
|
70
70
|
const h = (s) => pc.cyan(s);
|
|
@@ -81,7 +81,7 @@ function serializeObject(obj, passToClient, getObj) {
|
|
|
81
81
|
assert(objName);
|
|
82
82
|
const varName = `${objName}${getPropKeys(prop).map(getPropAccessNotation).join('')}`;
|
|
83
83
|
try {
|
|
84
|
-
serializeValue(value, varName);
|
|
84
|
+
serializeValue(value, isHtmlJsonScript, varName);
|
|
85
85
|
}
|
|
86
86
|
catch (err) {
|
|
87
87
|
propsNonSerializable.push(prop);
|
|
@@ -119,7 +119,7 @@ function serializeObject(obj, passToClient, getObj) {
|
|
|
119
119
|
obj[getPropKeys(prop)[0]] = NOT_SERIALIZABLE;
|
|
120
120
|
});
|
|
121
121
|
try {
|
|
122
|
-
serialized = serializeValue(obj);
|
|
122
|
+
serialized = serializeValue(obj, isHtmlJsonScript);
|
|
123
123
|
}
|
|
124
124
|
catch (err) {
|
|
125
125
|
assert(false);
|
|
@@ -127,20 +127,20 @@ function serializeObject(obj, passToClient, getObj) {
|
|
|
127
127
|
}
|
|
128
128
|
return serialized;
|
|
129
129
|
}
|
|
130
|
-
function serializeValue(value, varName) {
|
|
130
|
+
function serializeValue(value, isHtmlJsonScript, varName) {
|
|
131
131
|
return stringify(value, {
|
|
132
132
|
forbidReactElements: true,
|
|
133
133
|
valueName: varName,
|
|
134
134
|
// Prevent Google from crawling URLs in JSON:
|
|
135
|
-
//
|
|
136
|
-
//
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
135
|
+
// - https://github.com/vikejs/vike/pull/2603
|
|
136
|
+
// - https://github.com/brillout/json-serializer/blob/38edbb9945de4938da1e65d6285ce1dd123a45ef/test/main.spec.ts#L44-L95
|
|
137
|
+
replacer: !isHtmlJsonScript
|
|
138
|
+
? undefined
|
|
139
|
+
: (_key, value) => {
|
|
140
|
+
if (typeof value === 'string') {
|
|
141
|
+
return { replacement: value.replaceAll('/', '\\/'), resolved: false };
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
144
|
});
|
|
145
145
|
}
|
|
146
146
|
function getPassToClientPageContext(pageContext) {
|
|
@@ -153,7 +153,7 @@ function getPassToClientPageContext(pageContext) {
|
|
|
153
153
|
passToClient = unique(passToClient);
|
|
154
154
|
return passToClient;
|
|
155
155
|
}
|
|
156
|
-
function getPageContextClientSerializedAbort(pageContext) {
|
|
156
|
+
function getPageContextClientSerializedAbort(pageContext, isHtmlJsonScript) {
|
|
157
157
|
assert(pageContext._urlRedirect || pageContext._urlRewrite || pageContext.abortStatusCode);
|
|
158
158
|
assert(pageContext._abortCall);
|
|
159
159
|
assert(pageContext._abortCaller);
|
|
@@ -186,7 +186,7 @@ function getPageContextClientSerializedAbort(pageContext) {
|
|
|
186
186
|
onlyOnce: false,
|
|
187
187
|
});
|
|
188
188
|
}
|
|
189
|
-
return serializeValue(pageContext);
|
|
189
|
+
return serializeValue(pageContext, isHtmlJsonScript);
|
|
190
190
|
}
|
|
191
191
|
function applyPassToClient(passToClient, getObj) {
|
|
192
192
|
const objClient = {};
|
|
@@ -45,7 +45,7 @@ async function renderPageAlreadyRouted(pageContext) {
|
|
|
45
45
|
if (isError) {
|
|
46
46
|
objectAssign(pageContext, { [isServerSideError]: true });
|
|
47
47
|
}
|
|
48
|
-
const pageContextSerialized = getPageContextClientSerialized(pageContext);
|
|
48
|
+
const pageContextSerialized = getPageContextClientSerialized(pageContext, false);
|
|
49
49
|
const httpResponse = await createHttpResponsePageContextJson(pageContextSerialized);
|
|
50
50
|
objectAssign(pageContext, { httpResponse });
|
|
51
51
|
return pageContext;
|
|
@@ -75,7 +75,7 @@ async function prerenderPage(pageContext) {
|
|
|
75
75
|
return { documentHtml, pageContextSerialized: null, pageContext };
|
|
76
76
|
}
|
|
77
77
|
else {
|
|
78
|
-
const pageContextSerialized = getPageContextClientSerialized(pageContext);
|
|
78
|
+
const pageContextSerialized = getPageContextClientSerialized(pageContext, false);
|
|
79
79
|
return { documentHtml, pageContextSerialized, pageContext };
|
|
80
80
|
}
|
|
81
81
|
}
|
|
@@ -419,10 +419,10 @@ pageContextNominalPageBegin, httpRequestId, pageContextErrorPageInit, globalCont
|
|
|
419
419
|
objectAssign(pageContext, pageContextErrorPageInit, true);
|
|
420
420
|
augmentType(pageContext, await loadPageConfigsLazyServerSideAndExecHook(pageContext));
|
|
421
421
|
// We include pageContextInit: we don't only serialize pageContextAbort because the error page may need to access pageContextInit
|
|
422
|
-
pageContextSerialized = getPageContextClientSerialized(pageContext);
|
|
422
|
+
pageContextSerialized = getPageContextClientSerialized(pageContext, false);
|
|
423
423
|
}
|
|
424
424
|
else {
|
|
425
|
-
pageContextSerialized = getPageContextClientSerializedAbort(pageContextAbort);
|
|
425
|
+
pageContextSerialized = getPageContextClientSerializedAbort(pageContextAbort, false);
|
|
426
426
|
}
|
|
427
427
|
const httpResponse = await createHttpResponsePageContextJson(pageContextSerialized);
|
|
428
428
|
const pageContextReturn = { httpResponse };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const PROJECT_VERSION: "0.4.236-commit-
|
|
1
|
+
export declare const PROJECT_VERSION: "0.4.236-commit-3c05c99";
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// Automatically updated by @brillout/release-me
|
|
2
|
-
export const PROJECT_VERSION = '0.4.236-commit-
|
|
2
|
+
export const PROJECT_VERSION = '0.4.236-commit-3c05c99';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vike",
|
|
3
|
-
"version": "0.4.236-commit-
|
|
3
|
+
"version": "0.4.236-commit-3c05c99",
|
|
4
4
|
"repository": "https://github.com/vikejs/vike",
|
|
5
5
|
"exports": {
|
|
6
6
|
"./server": {
|
|
@@ -129,7 +129,7 @@
|
|
|
129
129
|
},
|
|
130
130
|
"dependencies": {
|
|
131
131
|
"@brillout/import": "^0.2.6",
|
|
132
|
-
"@brillout/json-serializer": "^0.5.
|
|
132
|
+
"@brillout/json-serializer": "^0.5.17",
|
|
133
133
|
"@brillout/picocolors": "^1.0.26",
|
|
134
134
|
"@brillout/require-shim": "^0.1.2",
|
|
135
135
|
"@brillout/vite-plugin-server-entry": "^0.7.12",
|