vike 0.4.235-commit-16164c3 → 0.4.235-commit-7b1ab35

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.
@@ -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.235-commit-16164c3';
5
+ exports.PROJECT_VERSION = '0.4.235-commit-7b1ab35';
@@ -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 = {
@@ -19,7 +18,6 @@ type ScrollPosition = {
19
18
  declare function saveScrollPosition(): void;
20
19
  declare function pushHistoryState(url: string, overwriteLastHistoryEntry: boolean): void;
21
20
  declare function replaceHistoryStateOriginal(state: unknown, url: string): void;
22
- declare function monkeyPatchHistoryAPI(): 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
7
  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() });
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,11 @@ 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;
110
+ // Ensure Vike's monkey patch is the first.
111
+ assert(window.history.pushState === History.prototype.pushState);
104
112
  ['pushState', 'replaceState'].forEach((funcName) => {
105
113
  const funcOriginal = window.history[funcName].bind(window.history);
106
114
  window.history[funcName] = (stateOriginal = {}, ...rest) => {
@@ -114,11 +122,14 @@ function monkeyPatchHistoryAPI() {
114
122
  triggeredBy: 'user',
115
123
  ...stateOriginal,
116
124
  };
117
- assert(isVikeEnhanced(stateEnhanced));
118
- const ret = funcOriginal(stateEnhanced, ...rest);
125
+ assertIsVikeEnhanced(stateEnhanced);
126
+ funcOriginal(stateEnhanced, ...rest);
127
+ assertIsVikeEnhanced(getState());
119
128
  globalObject.previous = getHistoryInfo();
120
- return ret;
121
129
  };
130
+ window.history[funcName]._isVikeMonkeyPatch = true;
131
+ // Ensure assert() above isn't a false positive
132
+ assert(window.history.pushState !== History.prototype.pushState);
122
133
  });
123
134
  }
124
135
  function isVikeEnhanced(state) {
@@ -136,6 +147,16 @@ function isVikeEnhanced(state) {
136
147
  }
137
148
  return false;
138
149
  }
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
+ }
139
160
  function getHistoryInfo() {
140
161
  return {
141
162
  url: getCurrentUrl(),
@@ -147,11 +168,12 @@ function onPopStateBegin() {
147
168
  const isHistoryStateEnhanced = window.history.state !== null;
148
169
  if (!isHistoryStateEnhanced)
149
170
  enhanceHistoryState();
150
- assert(isVikeEnhanced(window.history.state));
171
+ assertIsVikeEnhanced(window.history.state);
151
172
  const current = getHistoryInfo();
152
173
  globalObject.previous = current;
153
174
  return { isHistoryStateEnhanced, previous, current };
154
175
  }
155
- function initHistoryState() {
156
- enhanceHistoryState();
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
+ enhanceHistoryState(); // enhance very first window.history.state which is `null`
157
179
  }
@@ -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();
@@ -1 +1 @@
1
- export declare const PROJECT_VERSION: "0.4.235-commit-16164c3";
1
+ export declare const PROJECT_VERSION: "0.4.235-commit-7b1ab35";
@@ -1,2 +1,2 @@
1
1
  // Automatically updated by @brillout/release-me
2
- export const PROJECT_VERSION = '0.4.235-commit-16164c3';
2
+ export const PROJECT_VERSION = '0.4.235-commit-7b1ab35';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vike",
3
- "version": "0.4.235-commit-16164c3",
3
+ "version": "0.4.235-commit-7b1ab35",
4
4
  "repository": "https://github.com/vikejs/vike",
5
5
  "exports": {
6
6
  "./server": {