vike 0.4.236-commit-08fbc10 → 0.4.236-commit-cd565e9

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.
Files changed (82) hide show
  1. package/dist/cjs/client/shared/createGetGlobalContextClient.js +20 -10
  2. package/dist/cjs/client/shared/utils.js +1 -0
  3. package/dist/cjs/node/runtime/globalContext.js +111 -26
  4. package/dist/cjs/node/runtime/html/injectAssets/getViteDevScript.js +3 -1
  5. package/dist/cjs/node/runtime/renderPage/getPageAssets.js +3 -1
  6. package/dist/cjs/node/runtime/utils.js +3 -0
  7. package/dist/cjs/node/vite/index.js +2 -0
  8. package/dist/cjs/node/vite/plugins/pluginBuild/handleAssetsManifest.js +19 -11
  9. package/dist/cjs/node/vite/plugins/pluginBuild/pluginAutoFullBuild.js +2 -2
  10. package/dist/cjs/node/vite/plugins/pluginBuild/pluginBuildConfig.js +0 -3
  11. package/dist/cjs/node/vite/plugins/pluginCommon.js +1 -0
  12. package/dist/cjs/node/vite/plugins/pluginDev/determineOptimizeDeps.js +10 -8
  13. package/dist/cjs/node/vite/plugins/pluginEnvVars.js +3 -3
  14. package/dist/cjs/node/vite/plugins/pluginExtractExportNames.js +2 -2
  15. package/dist/cjs/node/vite/plugins/pluginNonRunnableDev.js +51 -0
  16. package/dist/cjs/node/vite/plugins/pluginReplaceConstants.js +1 -1
  17. package/dist/cjs/node/vite/plugins/pluginVirtualFiles/getVirtualFilePageConfigsEager.js +4 -0
  18. package/dist/cjs/node/vite/shared/applyRegExWithMagicString.js +1 -0
  19. package/dist/cjs/node/vite/shared/getMagicString.js +18 -0
  20. package/dist/cjs/node/vite/shared/getManifestFilePathRelative.js +10 -0
  21. package/dist/cjs/shared/createGlobalContextShared.js +5 -1
  22. package/dist/cjs/shared/getProxyForPublicUsage.js +2 -2
  23. package/dist/cjs/utils/PROJECT_VERSION.js +1 -1
  24. package/dist/cjs/utils/assertSetup.js +2 -0
  25. package/dist/cjs/utils/debug.js +1 -0
  26. package/dist/cjs/utils/getViteRPC.js +79 -0
  27. package/dist/cjs/utils/isRunnableDevEnvironment.js +9 -0
  28. package/dist/esm/client/runtime-client-routing/createPageContextClientSide.d.ts +1 -1
  29. package/dist/esm/client/runtime-client-routing/getPageContextFromHooks.d.ts +2 -2
  30. package/dist/esm/client/runtime-client-routing/globalContext.d.ts +1 -1
  31. package/dist/esm/client/runtime-client-routing/history.d.ts +3 -5
  32. package/dist/esm/client/runtime-client-routing/history.js +41 -14
  33. package/dist/esm/client/runtime-client-routing/initClientRouter.js +2 -3
  34. package/dist/esm/client/runtime-client-routing/renderPageClientSide.d.ts +1 -1
  35. package/dist/esm/client/runtime-client-routing/renderPageClientSide.js +2 -2
  36. package/dist/esm/client/runtime-client-routing/utils.d.ts +1 -0
  37. package/dist/esm/client/runtime-client-routing/utils.js +1 -0
  38. package/dist/esm/client/runtime-server-routing/createPageContextClientSide.d.ts +1 -1
  39. package/dist/esm/client/runtime-server-routing/globalContext.d.ts +1 -1
  40. package/dist/esm/client/shared/createGetGlobalContextClient.d.ts +1 -1
  41. package/dist/esm/client/shared/createGetGlobalContextClient.js +20 -10
  42. package/dist/esm/client/shared/utils.d.ts +1 -0
  43. package/dist/esm/client/shared/utils.js +1 -0
  44. package/dist/esm/node/prerender/runPrerender.d.ts +40 -132
  45. package/dist/esm/node/runtime/globalContext.d.ts +34 -54
  46. package/dist/esm/node/runtime/globalContext.js +112 -27
  47. package/dist/esm/node/runtime/html/injectAssets/getViteDevScript.js +4 -2
  48. package/dist/esm/node/runtime/renderPage/createPageContextServerSide.d.ts +10 -33
  49. package/dist/esm/node/runtime/renderPage/getPageAssets.js +4 -2
  50. package/dist/esm/node/runtime/renderPage/renderPageAlreadyRouted.d.ts +20 -66
  51. package/dist/esm/node/runtime/renderPage.d.ts +10 -33
  52. package/dist/esm/node/runtime/utils.d.ts +3 -0
  53. package/dist/esm/node/runtime/utils.js +3 -0
  54. package/dist/esm/node/vite/index.js +2 -0
  55. package/dist/esm/node/vite/plugins/pluginBuild/handleAssetsManifest.d.ts +1 -1
  56. package/dist/esm/node/vite/plugins/pluginBuild/handleAssetsManifest.js +19 -11
  57. package/dist/esm/node/vite/plugins/pluginBuild/pluginAutoFullBuild.js +2 -2
  58. package/dist/esm/node/vite/plugins/pluginBuild/pluginBuildConfig.d.ts +0 -2
  59. package/dist/esm/node/vite/plugins/pluginBuild/pluginBuildConfig.js +0 -2
  60. package/dist/esm/node/vite/plugins/pluginCommon.d.ts +3 -0
  61. package/dist/esm/node/vite/plugins/pluginCommon.js +1 -0
  62. package/dist/esm/node/vite/plugins/pluginDev/determineOptimizeDeps.js +10 -8
  63. package/dist/esm/node/vite/plugins/pluginNonRunnableDev.d.ts +26 -0
  64. package/dist/esm/node/vite/plugins/pluginNonRunnableDev.js +49 -0
  65. package/dist/esm/node/vite/plugins/pluginVirtualFiles/getVirtualFilePageConfigsEager.js +4 -0
  66. package/dist/esm/node/vite/shared/applyRegExWithMagicString.js +1 -0
  67. package/dist/esm/node/vite/shared/getMagicString.d.ts +9 -0
  68. package/dist/esm/node/vite/shared/getMagicString.js +13 -0
  69. package/dist/esm/node/vite/shared/getManifestFilePathRelative.d.ts +2 -0
  70. package/dist/esm/node/vite/shared/getManifestFilePathRelative.js +8 -0
  71. package/dist/esm/shared/createGlobalContextShared.d.ts +3 -3
  72. package/dist/esm/shared/createGlobalContextShared.js +5 -1
  73. package/dist/esm/utils/PROJECT_VERSION.d.ts +1 -1
  74. package/dist/esm/utils/PROJECT_VERSION.js +1 -1
  75. package/dist/esm/utils/assertSetup.js +2 -0
  76. package/dist/esm/utils/debug.d.ts +1 -1
  77. package/dist/esm/utils/debug.js +1 -0
  78. package/dist/esm/utils/getViteRPC.d.ts +6 -0
  79. package/dist/esm/utils/getViteRPC.js +77 -0
  80. package/dist/esm/utils/isRunnableDevEnvironment.d.ts +3 -0
  81. package/dist/esm/utils/isRunnableDevEnvironment.js +7 -0
  82. package/package.json +11 -2
@@ -26,7 +26,7 @@ function pluginReplaceConstants() {
26
26
  (0, utils_js_1.assertPosixPath)(config.root);
27
27
  if (!id.startsWith(config.root))
28
28
  return;
29
- if (!code.includes('import.meta.env.'))
29
+ if (!code.includes('({}).'))
30
30
  return;
31
31
  const isBuild = config.command === 'build';
32
32
  (0, utils_js_1.assert)(isBuild);
@@ -21,6 +21,10 @@ function getCode(pageConfigs, pageConfigGlobal, isForClientSide, isDev, id, isCl
21
21
  lines.push('export const pageConfigGlobalSerialized = {');
22
22
  lines.push(getCodePageConfigGlobalSerialized(pageConfigGlobal, isForClientSide, isClientRouting, isDev, importStatements, filesEnv));
23
23
  lines.push('};');
24
+ if (!isForClientSide && isDev) {
25
+ // https://vite.dev/guide/api-environment-frameworks.html
26
+ lines.push('if ((undefined)) (undefined).accept();');
27
+ }
24
28
  const code = [...importStatements, ...lines].join('\n');
25
29
  (0, debug_js_1.debug)(id, isForClientSide ? 'CLIENT-SIDE' : 'SERVER-SIDE', code);
26
30
  return code;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.applyRegExpWithMagicString = applyRegExpWithMagicString;
4
+ // TODO/now: replace with .replaceAll() ?
4
5
  function applyRegExpWithMagicString(magicString, regExp, replacement) {
5
6
  const envStatementRegEx = typeof regExp === 'string' ? new RegExp(regExp, 'g') : regExp;
6
7
  let match;
@@ -0,0 +1,18 @@
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.getMagicString = getMagicString;
7
+ const magic_string_1 = __importDefault(require("magic-string"));
8
+ // TODO/now use everywhere
9
+ function getMagicString(code, id) {
10
+ const magicString = new magic_string_1.default(code);
11
+ const getMagicStringResult = () => {
12
+ return {
13
+ code: magicString.toString(),
14
+ map: magicString.generateMap({ hires: true, source: id }),
15
+ };
16
+ };
17
+ return { magicString, getMagicStringResult };
18
+ }
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getManifestFilePathRelative = getManifestFilePathRelative;
4
+ const utils_js_1 = require("../utils.js");
5
+ function getManifestFilePathRelative(manifestConfig) {
6
+ (0, utils_js_1.assert)(['string', 'boolean'].includes(typeof manifestConfig));
7
+ (0, utils_js_1.assert)(manifestConfig !== false);
8
+ const manifestFileRelative = typeof manifestConfig === 'string' ? manifestConfig : '.vite/manifest.json';
9
+ return manifestFileRelative;
10
+ }
@@ -12,7 +12,7 @@ const getGlobalContextSyncErrMsg = "The global context isn't set yet, call getGl
12
12
  exports.getGlobalContextSyncErrMsg = getGlobalContextSyncErrMsg;
13
13
  async function createGlobalContextShared(virtualFileExports, globalObject, addGlobalContext,
14
14
  // TO-DO/next-major-release: we'll be able to remove addGlobalContextTmp after loadPageRoutes() is sync (it will be sync after we remove the old design)
15
- addGlobalContextTmp) {
15
+ addGlobalContextTmp, addGlobalContextAsync) {
16
16
  const globalContext = createGlobalContextBase(virtualFileExports);
17
17
  let isNewGlobalContext;
18
18
  if (!globalObject.globalContext) {
@@ -34,6 +34,10 @@ addGlobalContextTmp) {
34
34
  const globalContextAdded = await addGlobalContextTmp?.(globalContext);
35
35
  (0, utils_js_1.objectAssign)(globalContext, globalContextAdded);
36
36
  }
37
+ {
38
+ const globalContextAddedAsync = await addGlobalContextAsync?.(globalContext);
39
+ (0, utils_js_1.objectAssign)(globalContext, globalContextAddedAsync);
40
+ }
37
41
  const onCreateGlobalContextHooks = (0, getHook_js_1.getHookFromPageConfigGlobalCumulative)(globalContext._pageConfigGlobal, 'onCreateGlobalContext');
38
42
  let hooksCalled = false;
39
43
  if (!hooksAreEqual(globalObject.onCreateGlobalContextHooks ?? [], onCreateGlobalContextHooks)) {
@@ -43,8 +43,8 @@ function onInternalProp(propStr, objName) {
43
43
  // - We must skip it in the client-side because of the reactivity mechanism of UI frameworks like Solid.
44
44
  // - TO-DO/eventually: use import.meta.CLIENT instead of isBrowser()
45
45
  // - Where import.meta.CLIENT is defined by Vike
46
- // - Using import.meta.env.CLIENT (note `.env.`) doesn't seem possible: https://github.com/brillout/playground_node_import.meta.env
47
- // - If Rolldown Vite + Rolldowns always transpiles node_modules/ then we can simply use import.meta.env.SSR
46
+ // - Using ({}).CLIENT (note `.env.`) doesn't seem possible: https://github.com/brillout/playground_node_({})
47
+ // - If Rolldown Vite + Rolldowns always transpiles node_modules/ then we can simply use ({}).SSR
48
48
  if ((0, utils_js_1.isBrowser)())
49
49
  return;
50
50
  // TO-DO/soon/proxy: remove this and only warn on built-in access instead
@@ -2,4 +2,4 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PROJECT_VERSION = void 0;
4
4
  // Automatically updated by @brillout/release-me
5
- exports.PROJECT_VERSION = '0.4.236-commit-08fbc10';
5
+ exports.PROJECT_VERSION = '0.4.236-commit-cd565e9';
@@ -29,6 +29,8 @@ function assertIsNotProductionRuntime() {
29
29
  setup.shouldNotBeProduction = true;
30
30
  }
31
31
  function onSetupRuntime() {
32
+ if (true)
33
+ return; // TODO/now refactor this
32
34
  if (debug.isActivated)
33
35
  debug('assertSetup()', new Error().stack);
34
36
  if (isTest())
@@ -35,6 +35,7 @@ const flags = [
35
35
  'vike:setup',
36
36
  'vike:stream',
37
37
  'vike:virtualFiles',
38
+ 'vike:vite-rpc',
38
39
  ];
39
40
  const flagsSkipWildcard = ['vike:log'];
40
41
  const flagRegex = /\bvike:[a-zA-Z-]+/g;
@@ -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
+ }
@@ -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
+ }
@@ -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;
@@ -27,4 +27,4 @@ declare const getGlobalContextClientInternal: () => Promise<{
27
27
  } & {
28
28
  _pageRoutes: import("../../shared/route/loadPageRoutes.js").PageRoutes;
29
29
  _onBeforeRouteHook: import("../../shared/hooks/getHook.js").Hook | null;
30
- }>;
30
+ } & Record<string, any>>;
@@ -2,8 +2,7 @@ export { pushHistoryState };
2
2
  export { replaceHistoryStateOriginal };
3
3
  export { onPopStateBegin };
4
4
  export { saveScrollPosition };
5
- export { initHistoryState };
6
- export { monkeyPatchHistoryAPI };
5
+ export { initHistory };
7
6
  export type { HistoryInfo };
8
7
  export type { ScrollPosition };
9
8
  type StateEnhanced = {
@@ -18,8 +17,7 @@ type ScrollPosition = {
18
17
  };
19
18
  declare function saveScrollPosition(): void;
20
19
  declare function pushHistoryState(url: string, overwriteLastHistoryEntry: boolean): void;
21
- declare function replaceHistoryStateOriginal(state: unknown, url: string): void;
22
- declare function monkeyPatchHistoryAPI(): void;
20
+ declare function replaceHistoryStateOriginal(state: unknown, url: Parameters<typeof window.history.replaceState>[2]): void;
23
21
  type HistoryInfo = {
24
22
  url: `/${string}`;
25
23
  state: StateEnhanced;
@@ -29,4 +27,4 @@ declare function onPopStateBegin(): {
29
27
  previous: HistoryInfo;
30
28
  current: HistoryInfo;
31
29
  };
32
- declare function initHistoryState(): void;
30
+ declare function initHistory(): void;
@@ -2,12 +2,15 @@ export { pushHistoryState };
2
2
  export { replaceHistoryStateOriginal };
3
3
  export { onPopStateBegin };
4
4
  export { saveScrollPosition };
5
- export { initHistoryState };
6
- export { monkeyPatchHistoryAPI };
5
+ export { initHistory };
7
6
  import { getCurrentUrl } from '../shared/getCurrentUrl.js';
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() });
7
+ import { assert, assertUsage, getGlobalObject, isObject, deepEqual } from './utils.js';
8
+ const globalObject = getGlobalObject('history.ts', {
9
+ monkeyPatched: false,
10
+ previous: undefined,
11
+ });
12
+ initHistory(); // we redundantly call initHistory() to ensure it's called early
13
+ globalObject.previous = getHistoryInfo();
11
14
  // `window.history.state === null` when:
12
15
  // - The very first render
13
16
  // - Click on `<a href="#some-hash" />`
@@ -41,7 +44,7 @@ function enhance(stateNotEnhanced) {
41
44
  _isVikeEnhanced: true,
42
45
  };
43
46
  }
44
- assert(isVikeEnhanced(stateVikeEnhanced));
47
+ assertIsVikeEnhanced(stateVikeEnhanced);
45
48
  return stateVikeEnhanced;
46
49
  }
47
50
  function getState() {
@@ -51,7 +54,7 @@ function getState() {
51
54
  // - Therefore, we have to monkey patch history.pushState() and history.replaceState()
52
55
  // - Therefore, we need the assert() below to ensure history.state has been enhanced by Vike
53
56
  // - If users stumble upon this assert() then let's make it a assertUsage()
54
- assert(isVikeEnhanced(state), { state });
57
+ assertIsVikeEnhanced(state);
55
58
  return state;
56
59
  }
57
60
  function getStateNotEnhanced() {
@@ -90,6 +93,7 @@ function pushHistoryState(url, overwriteLastHistoryEntry) {
90
93
  function replaceHistoryState(state, url) {
91
94
  const url_ = url ?? null; // Passing `undefined` chokes older Edge versions.
92
95
  window.history.replaceState(state, '', url_);
96
+ assertIsVikeEnhanced(getState());
93
97
  }
94
98
  function replaceHistoryStateOriginal(state, url) {
95
99
  // Bypass all monkey patches.
@@ -100,7 +104,9 @@ function replaceHistoryStateOriginal(state, url) {
100
104
  // - history.pushState()
101
105
  // - history.replaceState()
102
106
  function monkeyPatchHistoryAPI() {
103
- ;
107
+ if (globalObject.monkeyPatched)
108
+ return;
109
+ globalObject.monkeyPatched = true;
104
110
  ['pushState', 'replaceState'].forEach((funcName) => {
105
111
  const funcOriginal = window.history[funcName].bind(window.history);
106
112
  window.history[funcName] = (stateOriginal = {}, ...rest) => {
@@ -114,11 +120,21 @@ function monkeyPatchHistoryAPI() {
114
120
  triggeredBy: 'user',
115
121
  ...stateOriginal,
116
122
  };
117
- assert(isVikeEnhanced(stateEnhanced));
118
- const ret = funcOriginal(stateEnhanced, ...rest);
123
+ assertIsVikeEnhanced(stateEnhanced);
124
+ funcOriginal(stateEnhanced, ...rest);
125
+ assertIsVikeEnhanced(getState());
119
126
  globalObject.previous = getHistoryInfo();
120
- return ret;
127
+ // Workaround https://github.com/vikejs/vike/issues/2504#issuecomment-3149764736
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
+ });
121
136
  };
137
+ window.history[funcName]._isVikeMonkeyPatch = true;
122
138
  });
123
139
  }
124
140
  function isVikeEnhanced(state) {
@@ -136,6 +152,16 @@ function isVikeEnhanced(state) {
136
152
  }
137
153
  return false;
138
154
  }
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
+ }
139
165
  function getHistoryInfo() {
140
166
  return {
141
167
  url: getCurrentUrl(),
@@ -147,11 +173,12 @@ function onPopStateBegin() {
147
173
  const isHistoryStateEnhanced = window.history.state !== null;
148
174
  if (!isHistoryStateEnhanced)
149
175
  enhanceHistoryState();
150
- assert(isVikeEnhanced(window.history.state));
176
+ assertIsVikeEnhanced(window.history.state);
151
177
  const current = getHistoryInfo();
152
178
  globalObject.previous = current;
153
179
  return { isHistoryStateEnhanced, previous, current };
154
180
  }
155
- function initHistoryState() {
156
- enhanceHistoryState();
181
+ function initHistory() {
182
+ monkeyPatchHistoryAPI(); // the earlier we call it the better (Vike can workaround erroneous library monkey patches if Vike is the last one in the monkey patch chain)
183
+ enhanceHistoryState(); // enhance very first window.history.state which is `null`
157
184
  }
@@ -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 { initHistoryState, monkeyPatchHistoryAPI } from './history.js';
9
+ import { initHistory } from './history.js';
10
10
  async function initClientRouter() {
11
11
  // Init navigation history and scroll restoration
12
12
  initHistoryAndScroll();
@@ -29,8 +29,7 @@ async function renderFirstPage() {
29
29
  }
30
30
  function initHistoryAndScroll() {
31
31
  scrollRestoration_init();
32
- monkeyPatchHistoryAPI();
33
- initHistoryState(); // we redundantly call initHistoryState() to ensure it's called early
32
+ initHistory(); // we redundantly call initHistory() to ensure it's called early
34
33
  autoSaveScrollPosition();
35
34
  // Handle back-/forward navigation
36
35
  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;
@@ -111,8 +111,8 @@ async function renderPageClientSide(renderArgs) {
111
111
  if (isFirstRender) {
112
112
  // Set pageContext properties set by onBeforeRoute()
113
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);
114
+ const { pageId, routeParams, ...rest } = pageContextFromRoute;
115
+ objectAssign(pageContext, rest);
116
116
  assert(hasProp(pageContext, 'routeParams', 'string{}')); // Help TS
117
117
  }
118
118
  else {
@@ -22,3 +22,4 @@ export * from '../../utils/PROJECT_VERSION.js';
22
22
  export * from '../../utils/genPromise.js';
23
23
  export * from '../../utils/catchInfiniteLoop.js';
24
24
  export * from '../../utils/changeEnumerable.js';
25
+ export * from '../../utils/deepEqual.js';
@@ -26,3 +26,4 @@ export * from '../../utils/PROJECT_VERSION.js';
26
26
  export * from '../../utils/genPromise.js';
27
27
  export * from '../../utils/catchInfiniteLoop.js';
28
28
  export * from '../../utils/changeEnumerable.js';
29
+ export * from '../../utils/deepEqual.js';
@@ -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;
@@ -24,4 +24,4 @@ declare const getGlobalContextClientInternal: () => Promise<{
24
24
  };
25
25
  } & {
26
26
  isClientSide: true;
27
- } & object>;
27
+ } & object & Record<string, any>>;
@@ -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: globalContextPromise, resolve: globalContextPromiseResolve } = genPromise();
10
+ const { promise: globalContextInitialPromise, resolve: globalContextInitialPromiseResolve } = genPromise();
11
11
  return {
12
- globalContextPromise,
13
- globalContextPromiseResolve,
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.globalContext &&
24
+ if (globalObject.virtualFileExports &&
25
25
  // Don't break HMR
26
- globalObject.globalContext._virtualFileExports !== virtualFileExports) {
27
- return globalObject.globalContext;
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 globalContext = await createGlobalContextShared(virtualFileExports, globalObject, undefined, async (globalContext) => {
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
- assert(globalObject.globalContext);
44
- globalObject.globalContextPromiseResolve(globalObject.globalContext);
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
  }
@@ -4,3 +4,4 @@ export * from '../../utils/getGlobalObject.js';
4
4
  export * from '../../utils/objectAssign.js';
5
5
  export * from '../../utils/hasProp.js';
6
6
  export * from '../../utils/genPromise.js';
7
+ export * from '../../utils/checkType.js';
@@ -4,3 +4,4 @@ export * from '../../utils/getGlobalObject.js';
4
4
  export * from '../../utils/objectAssign.js';
5
5
  export * from '../../utils/hasProp.js';
6
6
  export * from '../../utils/genPromise.js';
7
+ export * from '../../utils/checkType.js';