vike 0.4.266 → 0.4.267-commit-7884d91

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 (159) hide show
  1. package/dist/client/runtime-client-routing/createPageContextClient.d.ts +4 -0
  2. package/dist/client/runtime-client-routing/getGlobalContextClientInternal.d.ts +4 -0
  3. package/dist/client/runtime-client-routing/getPageContextFromHooks.d.ts +22 -2
  4. package/dist/client/runtime-client-routing/getPageContextFromHooks.js +4 -5
  5. package/dist/client/runtime-client-routing/history.d.ts +19 -19
  6. package/dist/client/runtime-client-routing/history.js +89 -83
  7. package/dist/client/runtime-client-routing/renderPageClient.d.ts +472 -9
  8. package/dist/client/runtime-client-routing/renderPageClient.js +12 -11
  9. package/dist/client/runtime-client-routing/setScrollPosition.js +6 -5
  10. package/dist/client/runtime-client-routing/streamedValues.d.ts +5 -0
  11. package/dist/client/runtime-client-routing/streamedValues.js +23 -0
  12. package/dist/client/runtime-server-routing/createPageContextClient.d.ts +4 -0
  13. package/dist/client/runtime-server-routing/entry.js +1 -1
  14. package/dist/client/runtime-server-routing/getGlobalContextClientInternal.d.ts +4 -0
  15. package/dist/client/shared/getGlobalContextClientInternalShared.d.ts +4 -0
  16. package/dist/client/shared/getJsonSerializedInHtml.d.ts +2 -2
  17. package/dist/client/shared/getJsonSerializedInHtml.js +9 -6
  18. package/dist/client/shared/streamedValues/asyncIterable.d.ts +4 -0
  19. package/dist/client/shared/streamedValues/asyncIterable.js +26 -0
  20. package/dist/client/shared/streamedValues/lines.d.ts +11 -0
  21. package/dist/client/shared/streamedValues/lines.js +19 -0
  22. package/dist/client/shared/streamedValues/promise.d.ts +4 -0
  23. package/dist/client/shared/streamedValues/promise.js +19 -0
  24. package/dist/client/shared/streamedValues/readableStream.d.ts +4 -0
  25. package/dist/client/shared/streamedValues/readableStream.js +44 -0
  26. package/dist/client/shared/streamedValues/registry.d.ts +21 -0
  27. package/dist/client/shared/streamedValues/registry.js +40 -0
  28. package/dist/client/shared/streamedValues.d.ts +7 -0
  29. package/dist/client/shared/streamedValues.js +82 -0
  30. package/dist/node/api/resolveViteConfigUser.js +2 -2
  31. package/dist/node/prerender/runPrerender.d.ts +12 -4
  32. package/dist/node/prerender/runPrerender.js +71 -15
  33. package/dist/node/vite/assertEnvVite.js +2 -2
  34. package/dist/node/vite/index.js +2 -0
  35. package/dist/node/vite/plugins/build/handleAssetsManifest.js +8 -3
  36. package/dist/node/vite/plugins/build/pluginBuildApp.js +25 -26
  37. package/dist/node/vite/plugins/build/pluginBuildConfig.d.ts +2 -2
  38. package/dist/node/vite/plugins/build/pluginBuildConfig.js +24 -16
  39. package/dist/node/vite/plugins/build/pluginDistFileNames.js +70 -50
  40. package/dist/node/vite/plugins/build/pluginEnvironmentEntries.d.ts +6 -0
  41. package/dist/node/vite/plugins/build/pluginEnvironmentEntries.js +71 -0
  42. package/dist/node/vite/plugins/build/pluginSuppressRollupWarning.js +19 -17
  43. package/dist/node/vite/plugins/pluginCommon.js +9 -6
  44. package/dist/node/vite/plugins/pluginDev/logSkillHint.js +6 -4
  45. package/dist/node/vite/plugins/pluginDev/optimizeDeps.js +1 -1
  46. package/dist/node/vite/plugins/pluginReplaceConstantsGlobalThis.js +6 -4
  47. package/dist/node/vite/plugins/pluginUniversalDeploy.js +3 -2
  48. package/dist/node/vite/plugins/pluginVirtualFiles/generateVirtualFileGlobalEntry.d.ts +2 -1
  49. package/dist/node/vite/plugins/pluginVirtualFiles/generateVirtualFileGlobalEntry.js +29 -12
  50. package/dist/node/vite/plugins/pluginVirtualFiles/generateVirtualFileGlobalEntryWithOldDesign.js +3 -2
  51. package/dist/node/vite/plugins/pluginVirtualFiles/generateVirtualFilePageEntry.js +7 -7
  52. package/dist/node/vite/plugins/pluginVirtualFiles/getConfigValueSourcesRelevant.d.ts +7 -5
  53. package/dist/node/vite/plugins/pluginVirtualFiles/getConfigValueSourcesRelevant.js +4 -10
  54. package/dist/node/vite/plugins/pluginVirtualFiles.js +20 -0
  55. package/dist/node/vite/shared/addSsrMiddleware.js +13 -1
  56. package/dist/node/vite/shared/getAssetsDir.d.ts +3 -2
  57. package/dist/node/vite/shared/getAssetsDir.js +3 -3
  58. package/dist/node/vite/shared/isViteCli.d.ts +3 -3
  59. package/dist/node/vite/shared/isViteCli.js +80 -84
  60. package/dist/node/vite/shared/isViteServerSide.js +2 -7
  61. package/dist/node/vite/shared/resolveVikeConfigInternal/getPlusFiles.d.ts +2 -2
  62. package/dist/node/vite/shared/resolveVikeConfigInternal/getPlusFiles.js +3 -3
  63. package/dist/node/vite/shared/resolveVikeConfigInternal/loadFileAtConfigTime.d.ts +4 -4
  64. package/dist/node/vite/shared/resolveVikeConfigInternal/loadFileAtConfigTime.js +9 -9
  65. package/dist/node/vite/shared/resolveVikeConfigInternal/metaBuiltIn.d.ts +1 -1
  66. package/dist/node/vite/shared/resolveVikeConfigInternal/metaBuiltIn.js +12 -6
  67. package/dist/node/vite/shared/resolveVikeConfigInternal/pointerImports.d.ts +3 -1
  68. package/dist/node/vite/shared/resolveVikeConfigInternal/pointerImports.js +18 -46
  69. package/dist/node/vite/shared/resolveVikeConfigInternal/transpileAndExecuteFile.d.ts +3 -3
  70. package/dist/node/vite/shared/resolveVikeConfigInternal/transpileAndExecuteFile.js +333 -212
  71. package/dist/node/vite/shared/resolveVikeConfigInternal.d.ts +4 -1
  72. package/dist/node/vite/shared/resolveVikeConfigInternal.js +97 -26
  73. package/dist/server/runtime/globalContext.d.ts +20 -0
  74. package/dist/server/runtime/renderPageServer/analyzePage.js +3 -3
  75. package/dist/server/runtime/renderPageServer/createHttpResponse.d.ts +20 -7
  76. package/dist/server/runtime/renderPageServer/createHttpResponse.js +34 -15
  77. package/dist/server/runtime/renderPageServer/createPageContextServer.d.ts +4 -0
  78. package/dist/server/runtime/renderPageServer/csp.d.ts +2 -1
  79. package/dist/server/runtime/renderPageServer/csp.js +2 -1
  80. package/dist/server/runtime/renderPageServer/execHookOnError.js +1 -1
  81. package/dist/server/runtime/renderPageServer/execHookOnRenderHtml.d.ts +9 -0
  82. package/dist/server/runtime/renderPageServer/execHookOnRenderHtml.js +17 -5
  83. package/dist/server/runtime/renderPageServer/getHttpResponseBody.d.ts +2 -2
  84. package/dist/server/runtime/renderPageServer/getHttpResponseBody.js +17 -2
  85. package/dist/server/runtime/renderPageServer/getPageContextJson.d.ts +8 -0
  86. package/dist/server/runtime/renderPageServer/getPageContextJson.js +47 -0
  87. package/dist/server/runtime/renderPageServer/handleErrorWithoutErrorPage.d.ts +2 -0
  88. package/dist/server/runtime/renderPageServer/handleErrorWithoutErrorPage.js +1 -1
  89. package/dist/server/runtime/renderPageServer/headersResponse.d.ts +11 -2
  90. package/dist/server/runtime/renderPageServer/headersResponse.js +22 -2
  91. package/dist/server/runtime/renderPageServer/html/injectAssets/getHtmlTags.js +5 -4
  92. package/dist/server/runtime/renderPageServer/html/injectAssets.d.ts +3 -2
  93. package/dist/server/runtime/renderPageServer/html/injectAssets.js +7 -1
  94. package/dist/server/runtime/renderPageServer/html/renderHtml.js +4 -2
  95. package/dist/server/runtime/renderPageServer/html/serializeContext.d.ts +5 -1
  96. package/dist/server/runtime/renderPageServer/html/serializeContext.js +12 -9
  97. package/dist/server/runtime/renderPageServer/html/stream.d.ts +13 -6
  98. package/dist/server/runtime/renderPageServer/html/stream.js +121 -31
  99. package/dist/server/runtime/renderPageServer/html/streamedValuesHtml.d.ts +30 -0
  100. package/dist/server/runtime/renderPageServer/html/streamedValuesHtml.js +92 -0
  101. package/dist/server/runtime/renderPageServer/loadPageConfigsLazyServerSide.d.ts +4 -0
  102. package/dist/server/runtime/renderPageServer/loadPageConfigsLazyServerSide.js +5 -1
  103. package/dist/server/runtime/renderPageServer/loadPageContextEnvironments.d.ts +15 -0
  104. package/dist/server/runtime/renderPageServer/loadPageContextEnvironments.js +61 -0
  105. package/dist/server/runtime/renderPageServer/renderPageServerAfterRoute.d.ts +314 -1
  106. package/dist/server/runtime/renderPageServer/renderPageServerAfterRoute.js +21 -12
  107. package/dist/server/runtime/renderPageServer/streamedValues/asyncIterable.d.ts +4 -0
  108. package/dist/server/runtime/renderPageServer/streamedValues/asyncIterable.js +12 -0
  109. package/dist/server/runtime/renderPageServer/streamedValues/lines.d.ts +10 -0
  110. package/dist/server/runtime/renderPageServer/streamedValues/lines.js +49 -0
  111. package/dist/server/runtime/renderPageServer/streamedValues/promise.d.ts +4 -0
  112. package/dist/server/runtime/renderPageServer/streamedValues/promise.js +14 -0
  113. package/dist/server/runtime/renderPageServer/streamedValues/pump.d.ts +10 -0
  114. package/dist/server/runtime/renderPageServer/streamedValues/pump.js +67 -0
  115. package/dist/server/runtime/renderPageServer/streamedValues/readableStream.d.ts +4 -0
  116. package/dist/server/runtime/renderPageServer/streamedValues/readableStream.js +12 -0
  117. package/dist/server/runtime/renderPageServer/streamedValues/registry.d.ts +28 -0
  118. package/dist/server/runtime/renderPageServer/streamedValues/registry.js +33 -0
  119. package/dist/server/runtime/renderPageServer.d.ts +4 -0
  120. package/dist/server/runtime/renderPageServer.js +17 -8
  121. package/dist/shared-server-client/createGlobalContextShared.d.ts +8 -0
  122. package/dist/shared-server-client/createGlobalContextShared.js +2 -1
  123. package/dist/shared-server-client/getPageContextPublicShared.js +0 -2
  124. package/dist/shared-server-client/getPageFiles/parseVirtualFileExportsGlobalEntry.d.ts +8 -0
  125. package/dist/shared-server-client/getPageFiles/parseVirtualFileExportsGlobalEntry.js +17 -7
  126. package/dist/shared-server-client/hooks/execHook.d.ts +1 -1
  127. package/dist/shared-server-client/hooks/execHook.js +2 -2
  128. package/dist/shared-server-client/route/abort.d.ts +1 -1
  129. package/dist/shared-server-client/route/abort.js +4 -2
  130. package/dist/shared-server-client/streamedValues.d.ts +22 -0
  131. package/dist/shared-server-client/streamedValues.js +24 -0
  132. package/dist/shared-server-node/vike-environments.d.ts +4 -0
  133. package/dist/shared-server-node/vike-environments.js +10 -0
  134. package/dist/shared-server-node/virtualFileId.d.ts +7 -5
  135. package/dist/shared-server-node/virtualFileId.js +42 -42
  136. package/dist/types/Config.d.ts +9 -1
  137. package/dist/types/PageConfig.d.ts +13 -5
  138. package/dist/types/PageContext.d.ts +18 -1
  139. package/dist/utils/PROJECT_VERSION.d.ts +1 -1
  140. package/dist/utils/PROJECT_VERSION.js +1 -1
  141. package/dist/utils/assertNodeVersion.d.ts +2 -0
  142. package/dist/utils/assertNodeVersion.js +4 -1
  143. package/dist/utils/assertViteVersion.d.ts +1 -3
  144. package/dist/utils/assertViteVersion.js +6 -9
  145. package/dist/utils/debug.d.ts +1 -1
  146. package/dist/utils/debug.js +1 -1
  147. package/dist/utils/deepEqual.d.ts +2 -1
  148. package/dist/utils/deepEqual.js +21 -6
  149. package/dist/utils/getContentTypeFromUrl.d.ts +2 -0
  150. package/dist/utils/getContentTypeFromUrl.js +39 -0
  151. package/dist/utils/injectRollupInputs.d.ts +2 -2
  152. package/dist/utils/injectRollupInputs.js +2 -2
  153. package/dist/utils/isDev.js +2 -7
  154. package/dist/utils/parseUrl-extras.js +4 -0
  155. package/dist/utils/requireResolve.d.ts +0 -6
  156. package/dist/utils/requireResolve.js +0 -8
  157. package/package.json +9 -10
  158. package/dist/shared-server-client/addIs404ToPageProps.d.ts +0 -2
  159. package/dist/shared-server-client/addIs404ToPageProps.js +0 -17
@@ -39,6 +39,10 @@ declare function createPageContextClient(urlOriginal: string): Promise<{
39
39
  _pageFilesAll: import("../../shared-server-client/getPageFiles.js").PageFile[];
40
40
  _pageConfigs: import("../../types/PageConfig.js").PageConfigRuntime[];
41
41
  _pageConfigGlobal: import("../../types/PageConfig.js").PageConfigGlobalRuntime;
42
+ _environmentEntries: Record<string, {
43
+ pageConfigs: import("../../types/PageConfig.js").PageConfigRuntime[];
44
+ pageConfigGlobal: import("../../types/PageConfig.js").PageConfigGlobalRuntime;
45
+ } | null>;
42
46
  _allPageIds: string[];
43
47
  } & {
44
48
  isClientSide: true;
@@ -25,6 +25,10 @@ declare function getGlobalContextClientInternal(): Promise<{
25
25
  _pageFilesAll: import("../../shared-server-client/getPageFiles.js").PageFile[];
26
26
  _pageConfigs: import("../../types/PageConfig.js").PageConfigRuntime[];
27
27
  _pageConfigGlobal: import("../../types/PageConfig.js").PageConfigGlobalRuntime;
28
+ _environmentEntries: Record<string, {
29
+ pageConfigs: import("../../types/PageConfig.js").PageConfigRuntime[];
30
+ pageConfigGlobal: import("../../types/PageConfig.js").PageConfigGlobalRuntime;
31
+ } | null>;
28
32
  _allPageIds: string[];
29
33
  } & {
30
34
  isClientSide: true;
@@ -13,10 +13,10 @@ type PageContextSerialized = {
13
13
  pageId: string;
14
14
  _hasPageContextFromServer: true;
15
15
  };
16
- declare function getPageContextFromHooksServer_firstRender(): PageContextSerialized & {
16
+ declare function getPageContextFromHooksServer_firstRender(): Promise<PageContextSerialized & {
17
17
  routeParams: Record<string, string>;
18
18
  _hasPageContextFromServer: true;
19
- };
19
+ }>;
20
20
  declare function getPageContextFromHooksClient_firstRender(pageContext: PageContextSerialized & PageContextBegin & PageContextConfig & {
21
21
  _hasPageContextFromServer: true;
22
22
  } & PageContextPublicClient): Promise<((PageContextSerialized & ({
@@ -57,6 +57,10 @@ declare function getPageContextFromHooksClient_firstRender(pageContext: PageCont
57
57
  _pageFilesAll: PageFile[];
58
58
  _pageConfigs: import("../../types/PageConfig.js").PageConfigRuntime[];
59
59
  _pageConfigGlobal: import("../../types/PageConfig.js").PageConfigGlobalRuntime;
60
+ _environmentEntries: Record<string, {
61
+ pageConfigs: import("../../types/PageConfig.js").PageConfigRuntime[];
62
+ pageConfigGlobal: import("../../types/PageConfig.js").PageConfigGlobalRuntime;
63
+ } | null>;
60
64
  _allPageIds: string[];
61
65
  } & {
62
66
  isClientSide: true;
@@ -113,6 +117,10 @@ declare function getPageContextFromHooksClient_firstRender(pageContext: PageCont
113
117
  _pageFilesAll: PageFile[];
114
118
  _pageConfigs: import("../../types/PageConfig.js").PageConfigRuntime[];
115
119
  _pageConfigGlobal: import("../../types/PageConfig.js").PageConfigGlobalRuntime;
120
+ _environmentEntries: Record<string, {
121
+ pageConfigs: import("../../types/PageConfig.js").PageConfigRuntime[];
122
+ pageConfigGlobal: import("../../types/PageConfig.js").PageConfigGlobalRuntime;
123
+ } | null>;
116
124
  _allPageIds: string[];
117
125
  } & {
118
126
  isClientSide: true;
@@ -180,6 +188,8 @@ declare function getPageContextFromHooksClient_firstRender(pageContext: PageCont
180
188
  } | null;
181
189
  } & {
182
190
  urlOriginal: string;
191
+ } & {
192
+ Page: unknown;
183
193
  }) | null;
184
194
  pageContextsAborted: import("../../shared-server-client/route/abort.js").PageContextAborted[];
185
195
  } & (import("../../shared-server-client/route/abort.js").PageContextAbort | null))) & PageContextConfig & {
@@ -299,6 +309,10 @@ declare function getPageContextFromHooksClient(pageContext: {
299
309
  _pageFilesAll: PageFile[];
300
310
  _pageConfigs: import("../../types/PageConfig.js").PageConfigRuntime[];
301
311
  _pageConfigGlobal: import("../../types/PageConfig.js").PageConfigGlobalRuntime;
312
+ _environmentEntries: Record<string, {
313
+ pageConfigs: import("../../types/PageConfig.js").PageConfigRuntime[];
314
+ pageConfigGlobal: import("../../types/PageConfig.js").PageConfigGlobalRuntime;
315
+ } | null>;
302
316
  _allPageIds: string[];
303
317
  } & {
304
318
  isClientSide: true;
@@ -355,6 +369,10 @@ declare function getPageContextFromHooksClient(pageContext: {
355
369
  _pageFilesAll: PageFile[];
356
370
  _pageConfigs: import("../../types/PageConfig.js").PageConfigRuntime[];
357
371
  _pageConfigGlobal: import("../../types/PageConfig.js").PageConfigGlobalRuntime;
372
+ _environmentEntries: Record<string, {
373
+ pageConfigs: import("../../types/PageConfig.js").PageConfigRuntime[];
374
+ pageConfigGlobal: import("../../types/PageConfig.js").PageConfigGlobalRuntime;
375
+ } | null>;
358
376
  _allPageIds: string[];
359
377
  } & {
360
378
  isClientSide: true;
@@ -422,6 +440,8 @@ declare function getPageContextFromHooksClient(pageContext: {
422
440
  } | null;
423
441
  } & {
424
442
  urlOriginal: string;
443
+ } & {
444
+ Page: unknown;
425
445
  }) | null;
426
446
  pageContextsAborted: import("../../shared-server-client/route/abort.js").PageContextAborted[];
427
447
  } & (import("../../shared-server-client/route/abort.js").PageContextAbort | null)) & PageContextConfig)) & import("../../shared-server-client/createPageContextShared.js").PageContextCreated & Omit<Partial<PageContextConfig & {
@@ -9,8 +9,8 @@ import { hasProp } from '../../utils/hasProp.js';
9
9
  import { isObject } from '../../utils/isObject.js';
10
10
  import { objectAssign } from '../../utils/objectAssign.js';
11
11
  import { redirectHard } from '../../utils/redirectHard.js';
12
- import { parse } from '@brillout/json-serializer/parse';
13
12
  import { getPageContextSerializedInHtml } from '../shared/getJsonSerializedInHtml.js';
13
+ import { readPageContextJson } from './streamedValues.js';
14
14
  import { analyzePageServerSide } from '../../shared-server-client/getPageFiles/analyzePageServerSide.js';
15
15
  import { removeBuiltInOverrides } from './getPageContext/removeBuiltInOverrides.js';
16
16
  import { getPageContextRequestUrl } from '../../shared-server-client/getPageContextRequestUrl.js';
@@ -30,8 +30,8 @@ const globalObject = getGlobalObject('getPageContextFromHooks.ts', {});
30
30
  // - Possible implementation: new computed prop `clientOnlyHooks: string[]` (list of hook ids) and add `hookId` to serialized config values
31
31
  const clientHooks = ['guard', 'data', 'onBeforeRender'];
32
32
  // Get `pageContext` values from `<script id="vike_pageContext" type="application/json">`
33
- function getPageContextFromHooksServer_firstRender() {
34
- const pageContextSerialized = getPageContextSerializedInHtml();
33
+ async function getPageContextFromHooksServer_firstRender() {
34
+ const pageContextSerialized = await getPageContextSerializedInHtml();
35
35
  processPageContextFromServer(pageContextSerialized);
36
36
  objectAssign(pageContextSerialized, {
37
37
  _hasPageContextFromServer: true,
@@ -210,8 +210,7 @@ async function fetchPageContextFromServer(pageContext) {
210
210
  }
211
211
  assertUsage(isCorrect, `Wrong Content-Type for ${pageContextUrl}: it should be ${contentTypeCorrect} but it's ${contentType} instead. Make sure to properly use pageContext.httpResponse.headers, see https://vike.dev/renderPage`);
212
212
  }
213
- const responseText = await response.text();
214
- const pageContextFromServer = parse(responseText);
213
+ const pageContextFromServer = await readPageContextJson(response);
215
214
  assert(isObject(pageContextFromServer));
216
215
  if (isAbortPageContext(pageContextFromServer)) {
217
216
  throw AbortRender(pageContextFromServer);
@@ -1,11 +1,28 @@
1
- export { pushHistoryState };
2
- export { replaceHistoryStateOriginal };
1
+ export { changeUrl };
3
2
  export { onPopStateBegin };
4
3
  export { saveScrollPosition };
5
4
  export { initHistory };
5
+ export { historyApiReplaceStateOriginal };
6
6
  export type { HistoryInfo };
7
7
  export type { ScrollPosition };
8
8
  import '../assertEnvClient.js';
9
+ declare function changeUrl(url: string, overwriteLastHistoryEntry: boolean): void;
10
+ declare function historyApiReplaceStateOriginal(state: unknown, url?: Parameters<typeof window.history.replaceState>[2]): void;
11
+ declare function onPopStateBegin(): {
12
+ skip: true;
13
+ previous?: undefined;
14
+ current?: undefined;
15
+ } | {
16
+ skip?: undefined;
17
+ previous: HistoryInfo;
18
+ current: HistoryInfo;
19
+ };
20
+ type ScrollPosition = {
21
+ x: number;
22
+ y: number;
23
+ };
24
+ declare function saveScrollPosition(scrollPosition?: ScrollPosition): void;
25
+ declare function initHistory(): void;
9
26
  type StateEnhanced = {
10
27
  vike: {
11
28
  timestamp: number;
@@ -13,24 +30,7 @@ type StateEnhanced = {
13
30
  triggeredBy: 'user' | 'vike' | 'browser';
14
31
  };
15
32
  };
16
- type ScrollPosition = {
17
- x: number;
18
- y: number;
19
- };
20
- declare function saveScrollPosition(scrollPosition?: ScrollPosition): void;
21
- declare function pushHistoryState(url: string, overwriteLastHistoryEntry: boolean): void;
22
- declare function replaceHistoryStateOriginal(state: unknown, url?: Parameters<typeof window.history.replaceState>[2]): void;
23
33
  type HistoryInfo = {
24
34
  url: `/${string}`;
25
35
  state: StateEnhanced;
26
36
  };
27
- declare function onPopStateBegin(): {
28
- skip: true;
29
- previous?: undefined;
30
- current?: undefined;
31
- } | {
32
- skip?: undefined;
33
- previous: HistoryInfo;
34
- current: HistoryInfo;
35
- };
36
- declare function initHistory(): void;
@@ -1,8 +1,8 @@
1
- export { pushHistoryState };
2
- export { replaceHistoryStateOriginal };
1
+ export { changeUrl };
3
2
  export { onPopStateBegin };
4
3
  export { saveScrollPosition };
5
4
  export { initHistory };
5
+ export { historyApiReplaceStateOriginal };
6
6
  import { getCurrentUrl } from '../shared/getCurrentUrl.js';
7
7
  import { assert, assertUsage } from '../../utils/assert.js';
8
8
  import { getGlobalObject } from '../../utils/getGlobalObject.js';
@@ -15,49 +15,9 @@ const globalObject = getGlobalObject('history.ts', {
15
15
  });
16
16
  initHistory(); // we redundantly call initHistory() to ensure it's called early
17
17
  globalObject.previous = getHistoryInfo();
18
- // `window.history.state === null` when:
19
- // - The very first render
20
- // - Click on `<a href="#some-hash" />`
21
- // - `location.hash = 'some-hash'`
22
- function enhance() {
23
- if (isEnhanced(window.history.state))
24
- return;
25
- const stateEnhanced = {
26
- vike: {
27
- timestamp: getTimestamp(),
28
- scrollPosition: getScrollPosition(),
29
- triggeredBy: 'browser',
30
- },
31
- };
32
- replaceHistoryState(stateEnhanced);
33
- }
34
- function getState() {
35
- const state = window.history.state;
36
- // *Every* state added to the history needs to go through Vike.
37
- // - Otherwise Vike's `popstate` listener won't work. (Because, for example, if globalObject.previous is outdated => isHashNavigation faulty => client-side navigation is wrongfully skipped.)
38
- // - Therefore, we have to monkey patch history.pushState() and history.replaceState()
39
- // - Therefore, we need the assert() below to ensure history.state has been enhanced by Vike
40
- // - If users stumble upon this assert() then let's make it a assertUsage()
41
- assertIsEnhanced(state);
42
- return state;
43
- }
44
- function getScrollPosition() {
45
- const scrollPosition = { x: window.scrollX, y: window.scrollY };
46
- return scrollPosition;
47
- }
48
- function getTimestamp() {
49
- return new Date().getTime();
50
- }
51
- function saveScrollPosition(scrollPosition) {
52
- scrollPosition || (scrollPosition = getScrollPosition());
53
- // Don't overwrite history.state if it was set by a non-Vike history.pushState() call.
54
- // https://github.com/vikejs/vike/issues/2801#issuecomment-3490431479
55
- if (!isEnhanced(window.history.state))
18
+ function changeUrl(url, overwriteLastHistoryEntry) {
19
+ if (getCurrentUrl() === url)
56
20
  return;
57
- const state = getState();
58
- replaceHistoryState({ ...state, vike: { ...state.vike, scrollPosition } });
59
- }
60
- function pushHistoryState(url, overwriteLastHistoryEntry) {
61
21
  if (!overwriteLastHistoryEntry) {
62
22
  const state = {
63
23
  vike: {
@@ -67,24 +27,68 @@ function pushHistoryState(url, overwriteLastHistoryEntry) {
67
27
  triggeredBy: 'vike',
68
28
  },
69
29
  };
70
- // Calling the monkey patched history.pushState() (and not the original) so that other tools (e.g. user tracking) can listen to Vike's pushState() calls.
71
- // - https://github.com/vikejs/vike/issues/1582
72
- window.history.pushState(state, '', url);
30
+ historyApiPushState(state, url);
73
31
  }
74
32
  else {
75
- replaceHistoryState(getState(), url);
33
+ historyApiReplaceState(getState(), url);
76
34
  }
77
35
  }
78
- function replaceHistoryState(state, url) {
36
+ function historyApiPushState(state, url) {
37
+ // Calling the monkey patched history.pushState() (not the original) so that other tools (e.g. user tracking) can listen to Vike's pushState() calls.
38
+ // - https://github.com/vikejs/vike/issues/1582
39
+ window.history.pushState(state, '', url);
40
+ assertIsEnhanced(window.history.state);
41
+ }
42
+ function historyApiReplaceState(state, url) {
79
43
  const url_ = url ?? null; // Passing `undefined` chokes older Edge versions.
80
44
  window.history.replaceState(state, '', url_);
81
45
  assertIsEnhanced(window.history.state);
82
46
  }
83
- function replaceHistoryStateOriginal(state, url) {
47
+ function historyApiReplaceStateOriginal(state, url) {
84
48
  // Bypass all monkey patches.
85
49
  // - Useful, for example, to avoid other tools listening to history.replaceState() calls
86
50
  History.prototype.replaceState.bind(window.history)(state, '', url);
87
51
  }
52
+ function onPopStateBegin() {
53
+ const { previous } = globalObject;
54
+ const isStateEnhanced = isEnhanced(window.history.state);
55
+ // Either:
56
+ // - `window.history.pushState(null, '', '/some-path')` , or
57
+ // - hash navigation
58
+ // - Click on `<a href="#some-hash">`
59
+ // - Using the `location` API (only hash navigation)
60
+ // See comments a the top of the ./initOnPopState.ts file.
61
+ const isStatePristine = window.history.state === null;
62
+ if (!isStateEnhanced && !isStatePristine) {
63
+ // Going to a history entry not created by Vike — entering another "SPA realm" => hard reload
64
+ // https://github.com/vikejs/vike/issues/2801#issuecomment-3490431479
65
+ redirectHard(getCurrentUrl());
66
+ return { skip: true };
67
+ }
68
+ if (!isStateEnhanced)
69
+ enhanceState();
70
+ const current = getHistoryInfo();
71
+ globalObject.previous = current;
72
+ // Let the browser handle hash navigations.
73
+ // - Upon hash navigation: `isHistoryStatePristine===true` (see comment above).
74
+ if (isStatePristine) {
75
+ return { skip: true };
76
+ }
77
+ return { previous, current };
78
+ }
79
+ function saveScrollPosition(scrollPosition) {
80
+ scrollPosition || (scrollPosition = getScrollPosition());
81
+ // Don't overwrite history.state if it was set by a non-Vike history.pushState() call.
82
+ // https://github.com/vikejs/vike/issues/2801#issuecomment-3490431479
83
+ if (!isEnhanced(window.history.state))
84
+ return;
85
+ const state = getState();
86
+ historyApiReplaceState({ ...state, vike: { ...state.vike, scrollPosition } });
87
+ }
88
+ function initHistory() {
89
+ 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)
90
+ enhanceState(); // enhance very first window.history.state which is `null`
91
+ }
88
92
  // Monkey patch:
89
93
  // - history.pushState()
90
94
  // - history.replaceState()
@@ -114,11 +118,26 @@ function monkeyPatchHistoryAPI() {
114
118
  if (isEnhanced(window.history.state))
115
119
  return;
116
120
  Object.assign(state, window.history.state);
117
- replaceHistoryStateOriginal(state);
121
+ historyApiReplaceStateOriginal(state);
118
122
  });
119
123
  };
120
124
  });
121
125
  }
126
+ function getState() {
127
+ const state = window.history.state;
128
+ // *Every* state added to the history needs to go through Vike.
129
+ // - Otherwise Vike's `popstate` listener won't work. (Because, for example, if globalObject.previous is outdated => isHashNavigation faulty => client-side navigation is wrongfully skipped.)
130
+ // - Therefore, we have to monkey patch history.pushState() and history.replaceState()
131
+ // - Therefore, we need the assert() below to ensure history.state has been enhanced by Vike
132
+ // - If users stumble upon this assert() then let's make it a assertUsage()
133
+ assertIsEnhanced(state);
134
+ return state;
135
+ }
136
+ function assertIsEnhanced(state) {
137
+ if (isEnhanced(state))
138
+ return;
139
+ assert(false, { state });
140
+ }
122
141
  function isEnhanced(state) {
123
142
  if (state?.vike) {
124
143
  /* We don't use the assert() below to save client-side KBs.
@@ -135,10 +154,21 @@ function isEnhanced(state) {
135
154
  }
136
155
  return false;
137
156
  }
138
- function assertIsEnhanced(state) {
139
- if (isEnhanced(state))
157
+ // `window.history.state === null` when:
158
+ // - The very first render
159
+ // - Click on `<a href="#some-hash" />`
160
+ // - `location.hash = 'some-hash'`
161
+ function enhanceState() {
162
+ if (isEnhanced(window.history.state))
140
163
  return;
141
- assert(false, { state });
164
+ const stateEnhanced = {
165
+ vike: {
166
+ timestamp: getTimestamp(),
167
+ scrollPosition: getScrollPosition(),
168
+ triggeredBy: 'browser',
169
+ },
170
+ };
171
+ historyApiReplaceState(stateEnhanced);
142
172
  }
143
173
  function getHistoryInfo() {
144
174
  return {
@@ -146,34 +176,10 @@ function getHistoryInfo() {
146
176
  state: getState(),
147
177
  };
148
178
  }
149
- function onPopStateBegin() {
150
- const { previous } = globalObject;
151
- const isStateEnhanced = isEnhanced(window.history.state);
152
- // Either:
153
- // - `window.history.pushState(null, '', '/some-path')` , or
154
- // - hash navigation
155
- // - Click on `<a href="#some-hash">`
156
- // - Using the `location` API (only hash navigation)
157
- // See comments a the top of the ./initOnPopState.ts file.
158
- const isStatePristine = window.history.state === null;
159
- if (!isStateEnhanced && !isStatePristine) {
160
- // Going to a history entry not created by Vike — entering another "SPA realm" => hard reload
161
- // https://github.com/vikejs/vike/issues/2801#issuecomment-3490431479
162
- redirectHard(getCurrentUrl());
163
- return { skip: true };
164
- }
165
- if (!isStateEnhanced)
166
- enhance();
167
- const current = getHistoryInfo();
168
- globalObject.previous = current;
169
- // Let the browser handle hash navigations.
170
- // - Upon hash navigation: `isHistoryStatePristine===true` (see comment above).
171
- if (isStatePristine) {
172
- return { skip: true };
173
- }
174
- return { previous, current };
179
+ function getScrollPosition() {
180
+ const scrollPosition = { x: window.scrollX, y: window.scrollY };
181
+ return scrollPosition;
175
182
  }
176
- function initHistory() {
177
- 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)
178
- enhance(); // enhance very first window.history.state which is `null`
183
+ function getTimestamp() {
184
+ return new Date().getTime();
179
185
  }