vike 0.4.236-commit-e752d12 → 0.4.236-commit-79f1ef0
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/createGetGlobalContextClient.js +20 -10
- package/dist/cjs/client/shared/utils.js +1 -0
- package/dist/cjs/node/runtime/globalContext.js +104 -20
- package/dist/cjs/node/runtime/html/injectAssets/getViteDevScript.js +3 -1
- package/dist/cjs/node/runtime/renderPage/getPageAssets/retrieveAssetsDev.js +4 -0
- package/dist/cjs/node/runtime/renderPage/getPageAssets.js +3 -1
- package/dist/cjs/node/runtime/utils.js +4 -0
- package/dist/cjs/node/shared/utils.js +1 -0
- package/dist/cjs/node/shared/virtualFiles/virtualFileEntry.js +16 -14
- package/dist/cjs/node/shared/virtualFiles/virtualFilePageConfigLazy.js +10 -4
- package/dist/cjs/node/vite/index.js +2 -0
- package/dist/cjs/node/vite/plugins/pluginBuild/handleAssetsManifest.js +19 -11
- package/dist/cjs/node/vite/plugins/pluginBuild/pluginAutoFullBuild.js +2 -2
- package/dist/cjs/node/vite/plugins/pluginBuild/pluginBuildApp.js +28 -13
- package/dist/cjs/node/vite/plugins/pluginBuild/pluginBuildConfig.js +0 -3
- package/dist/cjs/node/vite/plugins/pluginBuild/pluginDistFileNames.js +12 -7
- package/dist/cjs/node/vite/plugins/pluginBuild/pluginModuleBanner.js +2 -2
- package/dist/cjs/node/vite/plugins/pluginCommon.js +1 -0
- package/dist/cjs/node/vite/plugins/pluginDev/determineOptimizeDeps.js +102 -58
- package/dist/cjs/node/vite/plugins/pluginEnvVars.js +3 -3
- package/dist/cjs/node/vite/plugins/pluginExtractAssets.js +3 -4
- package/dist/cjs/node/vite/plugins/pluginExtractExportNames.js +2 -2
- package/dist/cjs/node/vite/plugins/pluginNonRunnableDev.js +51 -0
- package/dist/cjs/node/vite/plugins/pluginReplaceConstants.js +1 -1
- package/dist/cjs/node/vite/plugins/pluginVirtualFiles/getVirtualFilePageConfigLazy.js +1 -0
- package/dist/cjs/node/vite/plugins/pluginVirtualFiles/getVirtualFilePageConfigsEager.js +4 -0
- package/dist/cjs/node/vite/plugins/pluginVirtualFiles.js +4 -5
- package/dist/cjs/node/vite/shared/applyRegExWithMagicString.js +1 -0
- package/dist/cjs/node/vite/shared/getMagicString.js +18 -0
- package/dist/cjs/node/vite/shared/getManifestFilePathRelative.js +10 -0
- package/dist/cjs/node/vite/utils.js +1 -0
- package/dist/cjs/shared/createGlobalContextShared.js +5 -1
- package/dist/cjs/shared/getPageFiles/parseVirtualFileExports.js +6 -0
- package/dist/cjs/shared/getProxyForPublicUsage.js +2 -2
- package/dist/cjs/utils/PROJECT_VERSION.js +1 -1
- package/dist/cjs/utils/assertSetup.js +2 -0
- package/dist/cjs/utils/debug.js +1 -0
- package/dist/cjs/utils/getViteRPC.js +79 -0
- package/dist/cjs/utils/isFilePathAbsoluteFilesystem.js +1 -0
- package/dist/cjs/utils/isRunnableDevEnvironment.js +9 -0
- package/dist/cjs/utils/virtualFileId.js +39 -0
- package/dist/esm/client/runtime-client-routing/createPageContextClientSide.d.ts +1 -1
- package/dist/esm/client/runtime-client-routing/getPageContextFromHooks.d.ts +2 -2
- package/dist/esm/client/runtime-client-routing/globalContext.d.ts +1 -1
- package/dist/esm/client/runtime-client-routing/history.d.ts +4 -2
- package/dist/esm/client/runtime-client-routing/history.js +13 -35
- package/dist/esm/client/runtime-client-routing/initClientRouter.js +3 -2
- package/dist/esm/client/runtime-client-routing/renderPageClientSide.d.ts +1 -1
- package/dist/esm/client/runtime-client-routing/renderPageClientSide.js +43 -30
- package/dist/esm/client/runtime-server-routing/createPageContextClientSide.d.ts +1 -1
- package/dist/esm/client/runtime-server-routing/globalContext.d.ts +1 -1
- package/dist/esm/client/shared/createGetGlobalContextClient.d.ts +1 -1
- package/dist/esm/client/shared/createGetGlobalContextClient.js +20 -10
- package/dist/esm/client/shared/utils.d.ts +1 -0
- package/dist/esm/client/shared/utils.js +1 -0
- package/dist/esm/node/prerender/runPrerender.d.ts +40 -132
- package/dist/esm/node/runtime/globalContext.d.ts +32 -52
- package/dist/esm/node/runtime/globalContext.js +105 -21
- package/dist/esm/node/runtime/html/injectAssets/getViteDevScript.js +4 -2
- package/dist/esm/node/runtime/renderPage/createPageContextServerSide.d.ts +10 -33
- package/dist/esm/node/runtime/renderPage/getPageAssets/retrieveAssetsDev.js +4 -0
- package/dist/esm/node/runtime/renderPage/getPageAssets.js +4 -2
- package/dist/esm/node/runtime/renderPage/renderPageAlreadyRouted.d.ts +20 -66
- package/dist/esm/node/runtime/renderPage.d.ts +10 -33
- package/dist/esm/node/runtime/utils.d.ts +4 -0
- package/dist/esm/node/runtime/utils.js +4 -0
- package/dist/esm/node/shared/utils.d.ts +1 -0
- package/dist/esm/node/shared/utils.js +1 -0
- package/dist/esm/node/shared/virtualFiles/virtualFileEntry.js +16 -14
- package/dist/esm/node/shared/virtualFiles/virtualFilePageConfigLazy.js +11 -5
- package/dist/esm/node/vite/index.js +2 -0
- package/dist/esm/node/vite/plugins/pluginBuild/handleAssetsManifest.d.ts +1 -1
- package/dist/esm/node/vite/plugins/pluginBuild/handleAssetsManifest.js +19 -11
- package/dist/esm/node/vite/plugins/pluginBuild/pluginAutoFullBuild.js +2 -2
- package/dist/esm/node/vite/plugins/pluginBuild/pluginBuildApp.js +28 -13
- package/dist/esm/node/vite/plugins/pluginBuild/pluginBuildConfig.d.ts +0 -2
- package/dist/esm/node/vite/plugins/pluginBuild/pluginBuildConfig.js +0 -2
- package/dist/esm/node/vite/plugins/pluginBuild/pluginDistFileNames.js +12 -7
- package/dist/esm/node/vite/plugins/pluginBuild/pluginModuleBanner.js +2 -2
- package/dist/esm/node/vite/plugins/pluginCommon.d.ts +3 -0
- package/dist/esm/node/vite/plugins/pluginCommon.js +1 -0
- package/dist/esm/node/vite/plugins/pluginDev/determineOptimizeDeps.js +103 -59
- package/dist/esm/node/vite/plugins/pluginExtractAssets.js +3 -4
- package/dist/esm/node/vite/plugins/pluginNonRunnableDev.d.ts +26 -0
- package/dist/esm/node/vite/plugins/pluginNonRunnableDev.js +49 -0
- package/dist/esm/node/vite/plugins/pluginVirtualFiles/getVirtualFilePageConfigLazy.js +1 -0
- package/dist/esm/node/vite/plugins/pluginVirtualFiles/getVirtualFilePageConfigsEager.js +4 -0
- package/dist/esm/node/vite/plugins/pluginVirtualFiles.js +3 -4
- package/dist/esm/node/vite/shared/applyRegExWithMagicString.js +1 -0
- package/dist/esm/node/vite/shared/getMagicString.d.ts +9 -0
- package/dist/esm/node/vite/shared/getMagicString.js +13 -0
- package/dist/esm/node/vite/shared/getManifestFilePathRelative.d.ts +2 -0
- package/dist/esm/node/vite/shared/getManifestFilePathRelative.js +8 -0
- package/dist/esm/node/vite/utils.d.ts +1 -0
- package/dist/esm/node/vite/utils.js +1 -0
- package/dist/esm/shared/createGlobalContextShared.d.ts +3 -3
- package/dist/esm/shared/createGlobalContextShared.js +5 -1
- package/dist/esm/shared/getPageFiles/parseVirtualFileExports.js +6 -0
- package/dist/esm/utils/PROJECT_VERSION.d.ts +1 -1
- package/dist/esm/utils/PROJECT_VERSION.js +1 -1
- package/dist/esm/utils/assertSetup.js +2 -0
- package/dist/esm/utils/debug.d.ts +1 -1
- package/dist/esm/utils/debug.js +1 -0
- package/dist/esm/utils/getViteRPC.d.ts +6 -0
- package/dist/esm/utils/getViteRPC.js +77 -0
- package/dist/esm/utils/isFilePathAbsoluteFilesystem.d.ts +7 -0
- package/dist/esm/utils/isFilePathAbsoluteFilesystem.js +1 -0
- package/dist/esm/utils/isRunnableDevEnvironment.d.ts +3 -0
- package/dist/esm/utils/isRunnableDevEnvironment.js +7 -0
- package/dist/esm/utils/virtualFileId.d.ts +6 -0
- package/dist/esm/{node/shared/virtualFiles.js → utils/virtualFileId.js} +15 -17
- package/package.json +12 -3
- package/dist/cjs/node/shared/virtualFiles.js +0 -41
- package/dist/esm/node/shared/virtualFiles.d.ts +0 -8
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getViteRPC = getViteRPC;
|
|
4
|
+
exports.createViteRPC = createViteRPC;
|
|
5
|
+
const assert_js_1 = require("./assert.js");
|
|
6
|
+
const genPromise_js_1 = require("./genPromise.js");
|
|
7
|
+
const getRandomId_js_1 = require("./getRandomId.js");
|
|
8
|
+
const getGlobalObject_js_1 = require("./getGlobalObject.js");
|
|
9
|
+
const debug_js_1 = require("./debug.js");
|
|
10
|
+
const assertIsNotBrowser_js_1 = require("./assertIsNotBrowser.js");
|
|
11
|
+
(0, assertIsNotBrowser_js_1.assertIsNotBrowser)();
|
|
12
|
+
const globalObject = (0, getGlobalObject_js_1.getGlobalObject)('utils/getViteRPC.ts', {
|
|
13
|
+
rpc: null,
|
|
14
|
+
});
|
|
15
|
+
const debug = (0, debug_js_1.createDebugger)('vike:vite-rpc');
|
|
16
|
+
function getViteRPC() {
|
|
17
|
+
globalObject.rpc ?? (globalObject.rpc = createRpcClient());
|
|
18
|
+
return globalObject.rpc;
|
|
19
|
+
}
|
|
20
|
+
function createRpcClient() {
|
|
21
|
+
// @ts-ignore CJS build doesn't support import.meta — TO-DO/eventually: let's remove this ts-ignore after we removed the CJS build
|
|
22
|
+
const hot = (undefined);
|
|
23
|
+
(0, assert_js_1.assert)(hot);
|
|
24
|
+
const listeners = [];
|
|
25
|
+
hot.on(`vike:rpc:response`, (dataResponse) => {
|
|
26
|
+
if (debug.isActivated)
|
|
27
|
+
debug('Response received', dataResponse);
|
|
28
|
+
const { callId, functionReturn } = dataResponse;
|
|
29
|
+
listeners.forEach((l) => {
|
|
30
|
+
if (callId !== l.callId)
|
|
31
|
+
return;
|
|
32
|
+
l.cb(functionReturn);
|
|
33
|
+
listeners.splice(listeners.indexOf(l), 1);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
const rpc = new Proxy({}, {
|
|
37
|
+
get(_, functionName) {
|
|
38
|
+
return async (...functionArgs) => {
|
|
39
|
+
// @ts-ignore CJS build doesn't support import.meta — TO-DO/eventually: let's remove this ts-ignore after we removed the CJS build
|
|
40
|
+
const hot = (undefined);
|
|
41
|
+
(0, assert_js_1.assert)(hot);
|
|
42
|
+
const callId = (0, getRandomId_js_1.getRandomId)();
|
|
43
|
+
const { promise, resolve } = (0, genPromise_js_1.genPromise)({ timeout: 3 * 1000 });
|
|
44
|
+
listeners.push({
|
|
45
|
+
callId,
|
|
46
|
+
cb: (functionReturn) => {
|
|
47
|
+
resolve(functionReturn);
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
const dataRequest = { callId, functionName, functionArgs };
|
|
51
|
+
if (debug.isActivated)
|
|
52
|
+
debug('Request sent', dataRequest);
|
|
53
|
+
// Vite's type is wrong: (undefined).send() does seem to return a promise
|
|
54
|
+
await hot.send('vike:rpc:request', dataRequest);
|
|
55
|
+
const functionReturn = await promise;
|
|
56
|
+
return functionReturn;
|
|
57
|
+
};
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
return rpc;
|
|
61
|
+
}
|
|
62
|
+
function createViteRPC(viteDevServer, getRpcFunctions) {
|
|
63
|
+
const rpcFunctions = getRpcFunctions(viteDevServer);
|
|
64
|
+
const { environments } = viteDevServer;
|
|
65
|
+
for (const envName in environments) {
|
|
66
|
+
debug('Listening to environment', envName);
|
|
67
|
+
const env = environments[envName];
|
|
68
|
+
env.hot.on('vike:rpc:request', async (dataRequest) => {
|
|
69
|
+
if (debug.isActivated)
|
|
70
|
+
debug('Request received', dataRequest);
|
|
71
|
+
const { callId, functionName, functionArgs } = dataRequest;
|
|
72
|
+
const functionReturn = await rpcFunctions[functionName](...functionArgs);
|
|
73
|
+
const dataResponse = { callId, functionReturn };
|
|
74
|
+
if (debug.isActivated)
|
|
75
|
+
debug('Response sent', dataResponse);
|
|
76
|
+
env.hot.send('vike:rpc:response', dataResponse);
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.assertFilePathAbsoluteFilesystem = assertFilePathAbsoluteFilesystem;
|
|
7
|
+
exports.isFilePathAbsoluteFilesystem = isFilePathAbsoluteFilesystem;
|
|
7
8
|
exports.isFilePathAbsolute = isFilePathAbsolute;
|
|
8
9
|
const node_path_1 = __importDefault(require("node:path"));
|
|
9
10
|
const assert_js_1 = require("./assert.js");
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isRunnableDevEnvironment = isRunnableDevEnvironment;
|
|
4
|
+
// We use this instead of `import { isRunnableDevEnvironment } from 'vite'` because:
|
|
5
|
+
// - Vite's isRunnableDevEnvironment() isn't reliable in monorepos where multiple Vite instances can be used simultaneously (it uses `instanceof RunnableDevEnvironment` which fails)
|
|
6
|
+
// - We can use this in Vike's server production runtime without having to load the 'vite' package
|
|
7
|
+
function isRunnableDevEnvironment(environment) {
|
|
8
|
+
return !!environment && 'runner' in environment && !!environment.runner;
|
|
9
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.isVirtualFileId = isVirtualFileId;
|
|
7
|
+
exports.addVirtualFileIdPrefix = addVirtualFileIdPrefix;
|
|
8
|
+
exports.removeVirtualFileIdPrefix = removeVirtualFileIdPrefix;
|
|
9
|
+
const picocolors_1 = __importDefault(require("@brillout/picocolors"));
|
|
10
|
+
const assert_js_1 = require("./assert.js");
|
|
11
|
+
const assertIsNotBrowser_js_1 = require("./assertIsNotBrowser.js");
|
|
12
|
+
(0, assertIsNotBrowser_js_1.assertIsNotBrowser)();
|
|
13
|
+
const idBase = 'virtual:vike:';
|
|
14
|
+
// https://vitejs.dev/guide/api-plugin.html#virtual-modules-convention
|
|
15
|
+
const prefix = '\0';
|
|
16
|
+
function isVirtualFileId(id) {
|
|
17
|
+
if (id.startsWith(idBase))
|
|
18
|
+
return true;
|
|
19
|
+
if (id.startsWith(prefix + idBase))
|
|
20
|
+
return true;
|
|
21
|
+
// https://github.com/vikejs/vike/issues/1985
|
|
22
|
+
(0, assert_js_1.assertUsage)(!id.includes(idBase), `Encountered a module ID ${picocolors_1.default.cyan(id)} that is unexpected. Are you using a tool that modifies the ID of modules? For example, the baseUrl setting in tsconfig.json cannot be used.`);
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
function addVirtualFileIdPrefix(id) {
|
|
26
|
+
(0, assert_js_1.assert)(isVirtualFileId(id));
|
|
27
|
+
if (!id.startsWith(prefix)) {
|
|
28
|
+
id = prefix + id;
|
|
29
|
+
}
|
|
30
|
+
(0, assert_js_1.assert)(id.startsWith(prefix));
|
|
31
|
+
return id;
|
|
32
|
+
}
|
|
33
|
+
function removeVirtualFileIdPrefix(id) {
|
|
34
|
+
if (id.startsWith(prefix)) {
|
|
35
|
+
id = id.slice(prefix.length);
|
|
36
|
+
}
|
|
37
|
+
(0, assert_js_1.assert)(!id.startsWith(prefix));
|
|
38
|
+
return id;
|
|
39
|
+
}
|
|
@@ -30,7 +30,7 @@ declare function createPageContextClientSide(urlOriginal: string): Promise<{
|
|
|
30
30
|
} & {
|
|
31
31
|
_pageRoutes: import("../../__internal/index.js").PageRoutes;
|
|
32
32
|
_onBeforeRouteHook: import("../../shared/hooks/getHook.js").Hook | null;
|
|
33
|
-
}
|
|
33
|
+
} & Record<string, any>;
|
|
34
34
|
_pageFilesAll: import("../../shared/getPageFiles.js").PageFile[];
|
|
35
35
|
_urlHandler: null;
|
|
36
36
|
_urlRewrite: null | string;
|
|
@@ -50,7 +50,7 @@ declare function getPageContextFromHooks_isHydration(pageContext: PageContextSer
|
|
|
50
50
|
} & {
|
|
51
51
|
_pageRoutes: import("../../__internal/index.js").PageRoutes;
|
|
52
52
|
_onBeforeRouteHook: import("../../shared/hooks/getHook.js").Hook | null;
|
|
53
|
-
}
|
|
53
|
+
} & Record<string, any>;
|
|
54
54
|
_pageFilesAll: PageFile[];
|
|
55
55
|
_urlHandler: null;
|
|
56
56
|
_urlRewrite: null | string;
|
|
@@ -264,7 +264,7 @@ declare function getPageContextFromClientHooks(pageContext: {
|
|
|
264
264
|
} & {
|
|
265
265
|
_pageRoutes: import("../../__internal/index.js").PageRoutes;
|
|
266
266
|
_onBeforeRouteHook: import("../../shared/hooks/getHook.js").Hook | null;
|
|
267
|
-
}
|
|
267
|
+
} & Record<string, any>;
|
|
268
268
|
_pageFilesAll: PageFile[];
|
|
269
269
|
_urlHandler: null;
|
|
270
270
|
_urlRewrite: null | string;
|
|
@@ -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 = {
|
|
@@ -18,6 +19,7 @@ type ScrollPosition = {
|
|
|
18
19
|
declare function saveScrollPosition(): void;
|
|
19
20
|
declare function pushHistoryState(url: string, overwriteLastHistoryEntry: boolean): void;
|
|
20
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
8
|
import { assert, assertUsage, getGlobalObject, isObject } from './utils.js';
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
previous: undefined,
|
|
11
|
-
});
|
|
12
|
-
initHistory(); // we redundantly call initHistory() to ensure it's called early
|
|
13
|
-
globalObject.previous = getHistoryInfo();
|
|
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,11 +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;
|
|
110
|
-
// Ensure Vike's monkey patch is the first.
|
|
111
|
-
assert(window.history.pushState === History.prototype.pushState);
|
|
103
|
+
;
|
|
112
104
|
['pushState', 'replaceState'].forEach((funcName) => {
|
|
113
105
|
const funcOriginal = window.history[funcName].bind(window.history);
|
|
114
106
|
window.history[funcName] = (stateOriginal = {}, ...rest) => {
|
|
@@ -122,14 +114,11 @@ function monkeyPatchHistoryAPI() {
|
|
|
122
114
|
triggeredBy: 'user',
|
|
123
115
|
...stateOriginal,
|
|
124
116
|
};
|
|
125
|
-
|
|
126
|
-
funcOriginal(stateEnhanced, ...rest);
|
|
127
|
-
assertIsVikeEnhanced(getState());
|
|
117
|
+
assert(isVikeEnhanced(stateEnhanced));
|
|
118
|
+
const ret = funcOriginal(stateEnhanced, ...rest);
|
|
128
119
|
globalObject.previous = getHistoryInfo();
|
|
120
|
+
return ret;
|
|
129
121
|
};
|
|
130
|
-
window.history[funcName]._isVikeMonkeyPatch = true;
|
|
131
|
-
// Ensure assert() above isn't a false positive
|
|
132
|
-
assert(window.history.pushState !== History.prototype.pushState);
|
|
133
122
|
});
|
|
134
123
|
}
|
|
135
124
|
function isVikeEnhanced(state) {
|
|
@@ -147,16 +136,6 @@ function isVikeEnhanced(state) {
|
|
|
147
136
|
}
|
|
148
137
|
return false;
|
|
149
138
|
}
|
|
150
|
-
function assertIsVikeEnhanced(state) {
|
|
151
|
-
if (isVikeEnhanced(state))
|
|
152
|
-
return;
|
|
153
|
-
assert(false, {
|
|
154
|
-
state,
|
|
155
|
-
// TO-DO/eventually: remove _isVikeMonkeyPatch debug info to save KBs
|
|
156
|
-
pushStateIsVikeMonkeyPatch: window.history.pushState._isVikeMonkeyPatch,
|
|
157
|
-
replaceStateIsVikeMonkeyPatch: window.history.replaceState._isVikeMonkeyPatch,
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
139
|
function getHistoryInfo() {
|
|
161
140
|
return {
|
|
162
141
|
url: getCurrentUrl(),
|
|
@@ -168,12 +147,11 @@ function onPopStateBegin() {
|
|
|
168
147
|
const isHistoryStateEnhanced = window.history.state !== null;
|
|
169
148
|
if (!isHistoryStateEnhanced)
|
|
170
149
|
enhanceHistoryState();
|
|
171
|
-
|
|
150
|
+
assert(isVikeEnhanced(window.history.state));
|
|
172
151
|
const current = getHistoryInfo();
|
|
173
152
|
globalObject.previous = current;
|
|
174
153
|
return { isHistoryStateEnhanced, previous, current };
|
|
175
154
|
}
|
|
176
|
-
function
|
|
177
|
-
|
|
178
|
-
enhanceHistoryState(); // enhance very first window.history.state which is `null`
|
|
155
|
+
function initHistoryState() {
|
|
156
|
+
enhanceHistoryState();
|
|
179
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();
|
|
@@ -63,7 +63,7 @@ declare function getPageContextBegin(isForErrorPage: boolean, { urlOriginal, isB
|
|
|
63
63
|
} & {
|
|
64
64
|
_pageRoutes: import("../../shared/route/loadPageRoutes.js").PageRoutes;
|
|
65
65
|
_onBeforeRouteHook: import("../../shared/hooks/getHook.js").Hook | null;
|
|
66
|
-
}
|
|
66
|
+
} & Record<string, any>;
|
|
67
67
|
_pageFilesAll: import("../../shared/getPageFiles.js").PageFile[];
|
|
68
68
|
_urlHandler: null;
|
|
69
69
|
_urlRewrite: null | string;
|
|
@@ -84,7 +84,7 @@ async function renderPageClientSide(renderArgs) {
|
|
|
84
84
|
return;
|
|
85
85
|
}
|
|
86
86
|
}
|
|
87
|
-
//
|
|
87
|
+
// Get pageContext serialized in <script id="vike_pageContext" type="application/json">
|
|
88
88
|
if (isFirstRender) {
|
|
89
89
|
const pageContextSerialized = getPageContextFromHooks_serialized();
|
|
90
90
|
// TO-DO/eventually: create helper assertPageContextFromHook()
|
|
@@ -93,7 +93,9 @@ async function renderPageClientSide(renderArgs) {
|
|
|
93
93
|
// TO-DO/pageContext-prefetch: remove or change, because this only makes sense for a pre-rendered page
|
|
94
94
|
populatePageContextPrefetchCache(pageContext, { pageContextFromServerHooks: pageContextSerialized });
|
|
95
95
|
}
|
|
96
|
-
|
|
96
|
+
// Route
|
|
97
|
+
// - We must also run it upon hydration to call the onBeforeRoute() hook, which is needed for i18n URL locale extraction.
|
|
98
|
+
{
|
|
97
99
|
let pageContextFromRoute;
|
|
98
100
|
try {
|
|
99
101
|
pageContextFromRoute = await route(pageContext);
|
|
@@ -104,38 +106,49 @@ async function renderPageClientSide(renderArgs) {
|
|
|
104
106
|
}
|
|
105
107
|
if (isRenderOutdated())
|
|
106
108
|
return;
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
//
|
|
111
|
-
//
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
await renderErrorPage({ is404: true })
|
|
116
|
-
*/
|
|
117
|
-
redirectHard(urlOriginal);
|
|
118
|
-
return;
|
|
109
|
+
// TO-DO/eventually: create helper assertPageContextFromHook()
|
|
110
|
+
assert(!('urlOriginal' in pageContextFromRoute));
|
|
111
|
+
if (isFirstRender) {
|
|
112
|
+
// Set pageContext properties set by onBeforeRoute()
|
|
113
|
+
// - But we skip pageId and routeParams because routing may have been aborted by a server-side `throw render()`
|
|
114
|
+
const { pageId, routeParams, ...pageContextFromRouteRest } = pageContextFromRoute;
|
|
115
|
+
objectAssign(pageContext, pageContextFromRouteRest);
|
|
116
|
+
assert(hasProp(pageContext, 'routeParams', 'string{}')); // Help TS
|
|
119
117
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
if (isRenderOutdated())
|
|
123
|
-
return;
|
|
124
|
-
if (!isClientRoutable) {
|
|
125
|
-
redirectHard(urlOriginal);
|
|
126
|
-
return;
|
|
118
|
+
else {
|
|
119
|
+
objectAssign(pageContext, pageContextFromRoute);
|
|
127
120
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
121
|
+
if (!isFirstRender) {
|
|
122
|
+
if (!pageContextFromRoute.pageId) {
|
|
123
|
+
/*
|
|
124
|
+
// We don't use the client router to render the 404 page:
|
|
125
|
+
// - So that the +redirects setting (https://vike.dev/redirects) can be applied.
|
|
126
|
+
// - This is the main argument.
|
|
127
|
+
// - See also failed CI: https://github.com/vikejs/vike/pull/1871
|
|
128
|
+
// - So that server-side error tracking can track 404 links?
|
|
129
|
+
// - We do use the client router for rendering the error page, so I don't think this is much of an argument.
|
|
130
|
+
await renderErrorPage({ is404: true })
|
|
131
|
+
*/
|
|
132
|
+
redirectHard(urlOriginal);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
const isClientRoutable = await isClientSideRoutable(pageContextFromRoute.pageId, pageContext);
|
|
136
|
+
if (isRenderOutdated())
|
|
137
|
+
return;
|
|
138
|
+
if (!isClientRoutable) {
|
|
139
|
+
redirectHard(urlOriginal);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
const isSamePage = pageContextFromRoute.pageId &&
|
|
143
|
+
previousPageContext?.pageId &&
|
|
144
|
+
pageContextFromRoute.pageId === previousPageContext.pageId;
|
|
145
|
+
if (doNotRenderIfSamePage && isSamePage) {
|
|
146
|
+
// Skip's Vike's rendering; let the user handle the navigation
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
134
149
|
}
|
|
135
|
-
// TO-DO/eventually: create helper assertPageContextFromHook()
|
|
136
|
-
assert(!('urlOriginal' in pageContextFromRoute));
|
|
137
|
-
objectAssign(pageContext, pageContextFromRoute);
|
|
138
150
|
}
|
|
151
|
+
assert(hasProp(pageContext, 'pageId', 'string')); // Help TS
|
|
139
152
|
const res = await loadPageConfigsLazyClientSideAndExecHook(pageContext, isFirstRender, isRenderOutdated);
|
|
140
153
|
/* Already called inside loadPageConfigsLazyClientSideAndExecHook()
|
|
141
154
|
if (isRenderOutdated()) return
|
|
@@ -26,7 +26,7 @@ declare function createPageContextClientSide(): Promise<{
|
|
|
26
26
|
};
|
|
27
27
|
} & {
|
|
28
28
|
isClientSide: true;
|
|
29
|
-
} & object
|
|
29
|
+
} & object & Record<string, any>;
|
|
30
30
|
_pageFilesAll: import("../../shared/getPageFiles.js").PageFile[];
|
|
31
31
|
isBackwardNavigation: null;
|
|
32
32
|
_hasPageContextFromServer: true;
|
|
@@ -30,7 +30,7 @@ declare function createGetGlobalContextClient<GlobalContextAddendum extends obje
|
|
|
30
30
|
* We recommend using `import.meta.env.SSR` instead, see https://vike.dev/globalContext
|
|
31
31
|
*/
|
|
32
32
|
isClientSide: true;
|
|
33
|
-
} & Awaited<GlobalContextAddendum>>;
|
|
33
|
+
} & Awaited<GlobalContextAddendum> & Record<string, any>>;
|
|
34
34
|
type NeverExported = never;
|
|
35
35
|
declare function getGlobalContext(): Promise<NeverExported>;
|
|
36
36
|
declare function getGlobalContextSync(): NeverExported;
|
|
@@ -5,12 +5,12 @@ export { getGlobalContextSync };
|
|
|
5
5
|
export { createGetGlobalContextClient };
|
|
6
6
|
import { createGlobalContextShared, getGlobalContextSyncErrMsg, } from '../../shared/createGlobalContextShared.js';
|
|
7
7
|
import { getGlobalContextSerializedInHtml } from './getJsonSerializedInHtml.js';
|
|
8
|
-
import { assert, assertUsage, genPromise, getGlobalObject, objectAssign } from './utils.js';
|
|
8
|
+
import { assert, assertUsage, genPromise, getGlobalObject, objectAssign, checkType } from './utils.js';
|
|
9
9
|
const globalObject = getGlobalObject('createGetGlobalContextClient.ts', (() => {
|
|
10
|
-
const { promise:
|
|
10
|
+
const { promise: globalContextInitialPromise, resolve: globalContextInitialPromiseResolve } = genPromise();
|
|
11
11
|
return {
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
globalContextInitialPromise,
|
|
13
|
+
globalContextInitialPromiseResolve,
|
|
14
14
|
};
|
|
15
15
|
})());
|
|
16
16
|
function createGetGlobalContextClient(virtualFileExports, isClientRouting, addGlobalContext) {
|
|
@@ -21,13 +21,17 @@ function createGetGlobalContextClient(virtualFileExports, isClientRouting, addGl
|
|
|
21
21
|
return getGlobalContext;
|
|
22
22
|
async function getGlobalContext() {
|
|
23
23
|
// Cache
|
|
24
|
-
if (globalObject.
|
|
24
|
+
if (globalObject.virtualFileExports &&
|
|
25
25
|
// Don't break HMR
|
|
26
|
-
globalObject.
|
|
27
|
-
|
|
26
|
+
globalObject.virtualFileExports === virtualFileExports) {
|
|
27
|
+
const globalContext = await globalObject.globalContextPromise;
|
|
28
|
+
return globalContext;
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
globalObject.virtualFileExports = virtualFileExports;
|
|
28
32
|
}
|
|
29
33
|
// Create
|
|
30
|
-
const
|
|
34
|
+
const globalContextPromise = createGlobalContextShared(virtualFileExports, globalObject, undefined, async (globalContext) => {
|
|
31
35
|
const globalContextAddendum = {
|
|
32
36
|
/**
|
|
33
37
|
* Whether the environment is client-side or server-side / pre-rendering.
|
|
@@ -40,18 +44,24 @@ function createGetGlobalContextClient(virtualFileExports, isClientRouting, addGl
|
|
|
40
44
|
objectAssign(globalContextAddendum, await addGlobalContext?.(globalContext));
|
|
41
45
|
return globalContextAddendum;
|
|
42
46
|
});
|
|
43
|
-
|
|
44
|
-
|
|
47
|
+
globalObject.globalContextPromise = globalContextPromise;
|
|
48
|
+
const globalContext = await globalContextPromise;
|
|
49
|
+
assert(globalObject.globalContext === globalContext);
|
|
50
|
+
globalObject.globalContextInitialPromiseResolve();
|
|
45
51
|
// Return
|
|
46
52
|
return globalContext;
|
|
47
53
|
}
|
|
48
54
|
}
|
|
49
55
|
async function getGlobalContext() {
|
|
56
|
+
await globalObject.globalContextInitialPromise;
|
|
50
57
|
const globalContext = await globalObject.globalContextPromise;
|
|
58
|
+
assert(globalContext);
|
|
59
|
+
checkType(globalContext);
|
|
51
60
|
return globalContext;
|
|
52
61
|
}
|
|
53
62
|
function getGlobalContextSync() {
|
|
54
63
|
const { globalContext } = globalObject;
|
|
55
64
|
assertUsage(globalContext, getGlobalContextSyncErrMsg);
|
|
65
|
+
checkType(globalContext);
|
|
56
66
|
return globalContext;
|
|
57
67
|
}
|