vike 0.4.247-commit-dbcfa58 → 0.4.247-commit-e331796

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.
@@ -5,14 +5,12 @@ export { saveScrollPosition };
5
5
  export { initHistory };
6
6
  export type { HistoryInfo };
7
7
  export type { ScrollPosition };
8
- type VikeHistoryData = {
9
- timestamp: number;
10
- scrollPosition: null | ScrollPosition;
11
- triggeredBy: 'user' | 'vike' | 'browser';
12
- };
13
8
  type StateEnhanced = {
14
- _isVikeEnhanced: VikeHistoryData;
15
- [key: string]: unknown;
9
+ vike: {
10
+ timestamp: number;
11
+ scrollPosition: null | ScrollPosition;
12
+ triggeredBy: 'user' | 'vike' | 'browser';
13
+ };
16
14
  };
17
15
  type ScrollPosition = {
18
16
  x: number;
@@ -15,53 +15,17 @@ globalObject.previous = getHistoryInfo();
15
15
  // - The very first render
16
16
  // - Click on `<a href="#some-hash" />`
17
17
  // - `location.hash = 'some-hash'`
18
- function enhanceHistoryState() {
19
- if (isVikeEnhanced(window.history.state))
18
+ function enhance() {
19
+ if (isEnhanced(window.history.state))
20
20
  return;
21
- const stateVikeEnhanced = enhance(window.history.state);
22
- replaceHistoryState(stateVikeEnhanced);
23
- }
24
- function enhance(stateNotEnhanced) {
25
- const timestamp = getTimestamp();
26
- const scrollPosition = getScrollPosition();
27
- const triggeredBy = 'browser';
28
- let stateVikeEnhanced;
29
- if (!stateNotEnhanced) {
30
- stateVikeEnhanced = {
31
- _isVikeEnhanced: {
32
- timestamp,
33
- scrollPosition,
34
- triggeredBy,
35
- },
36
- };
37
- }
38
- else {
39
- // State information may be incomplete if `window.history.state` is set by an old Vike version. (E.g. `state.timestamp` was introduced for `pageContext.isBackwardNavigation` in `0.4.19`.)
40
- let oldVikeData;
41
- if (isObject(stateNotEnhanced) && '_isVikeEnhanced' in stateNotEnhanced) {
42
- if (isObject(stateNotEnhanced._isVikeEnhanced)) {
43
- // New format: _isVikeEnhanced is an object with nested properties
44
- oldVikeData = stateNotEnhanced._isVikeEnhanced;
45
- }
46
- else {
47
- // Old format: _isVikeEnhanced is true, properties are on state root
48
- oldVikeData = stateNotEnhanced;
49
- }
50
- }
51
- else {
52
- oldVikeData = {};
53
- }
54
- stateVikeEnhanced = {
55
- ...stateNotEnhanced,
56
- _isVikeEnhanced: {
57
- timestamp: oldVikeData.timestamp ?? timestamp,
58
- scrollPosition: oldVikeData.scrollPosition ?? scrollPosition,
59
- triggeredBy: oldVikeData.triggeredBy ?? triggeredBy,
60
- },
61
- };
62
- }
63
- assertIsVikeEnhanced(stateVikeEnhanced);
64
- return stateVikeEnhanced;
21
+ const stateEnhanced = {
22
+ vike: {
23
+ timestamp: getTimestamp(),
24
+ scrollPosition: getScrollPosition(),
25
+ triggeredBy: 'browser',
26
+ },
27
+ };
28
+ replaceHistoryState(stateEnhanced);
65
29
  }
66
30
  function getState() {
67
31
  const state = window.history.state;
@@ -70,7 +34,7 @@ function getState() {
70
34
  // - Therefore, we have to monkey patch history.pushState() and history.replaceState()
71
35
  // - Therefore, we need the assert() below to ensure history.state has been enhanced by Vike
72
36
  // - If users stumble upon this assert() then let's make it a assertUsage()
73
- assertIsVikeEnhanced(state);
37
+ assertIsEnhanced(state);
74
38
  return state;
75
39
  }
76
40
  function getScrollPosition() {
@@ -84,15 +48,15 @@ function saveScrollPosition() {
84
48
  const scrollPosition = getScrollPosition();
85
49
  // Don't overwrite history.state if it was set by a non-Vike history.pushState() call.
86
50
  // https://github.com/vikejs/vike/issues/2801#issuecomment-3490431479
87
- if (!isVikeEnhanced(window.history.state))
51
+ if (!isEnhanced(window.history.state))
88
52
  return;
89
53
  const state = getState();
90
- replaceHistoryState({ ...state, _isVikeEnhanced: { ...state._isVikeEnhanced, scrollPosition } });
54
+ replaceHistoryState({ ...state, vike: { ...state.vike, scrollPosition } });
91
55
  }
92
56
  function pushHistoryState(url, overwriteLastHistoryEntry) {
93
57
  if (!overwriteLastHistoryEntry) {
94
58
  const state = {
95
- _isVikeEnhanced: {
59
+ vike: {
96
60
  timestamp: getTimestamp(),
97
61
  // I don't remember why I set it to `null`, maybe because setting it now would be too early? (Maybe there is a delay between renderPageClient() is finished and the browser updating the scroll position.) Anyways, it seems like autoSaveScrollPosition() is enough.
98
62
  scrollPosition: null,
@@ -110,7 +74,7 @@ function pushHistoryState(url, overwriteLastHistoryEntry) {
110
74
  function replaceHistoryState(state, url) {
111
75
  const url_ = url ?? null; // Passing `undefined` chokes older Edge versions.
112
76
  window.history.replaceState(state, '', url_);
113
- assertIsVikeEnhanced(window.history.state);
77
+ assertIsEnhanced(window.history.state);
114
78
  }
115
79
  function replaceHistoryStateOriginal(state, url) {
116
80
  // Bypass all monkey patches.
@@ -126,42 +90,42 @@ function monkeyPatchHistoryAPI() {
126
90
  globalObject.monkeyPatched = true;
127
91
  ['pushState', 'replaceState'].forEach((funcName) => {
128
92
  const funcOriginal = window.history[funcName].bind(window.history);
129
- window.history[funcName] = (stateOriginal = {}, ...rest) => {
130
- assertUsage(stateOriginal === undefined || stateOriginal === null || isObject(stateOriginal), `history.${funcName}(state) argument state must be an object`);
131
- const stateEnhanced = isVikeEnhanced(stateOriginal)
132
- ? stateOriginal
93
+ window.history[funcName] = (stateFromUser = {}, ...rest) => {
94
+ assertUsage(stateFromUser === undefined || stateFromUser === null || isObject(stateFromUser), `history.${funcName}(state) argument state must be an object`);
95
+ const state = isEnhanced(stateFromUser)
96
+ ? stateFromUser
133
97
  : {
134
- ...stateOriginal,
135
- _isVikeEnhanced: {
98
+ ...stateFromUser,
99
+ vike: {
136
100
  scrollPosition: getScrollPosition(),
137
101
  timestamp: getTimestamp(),
138
102
  triggeredBy: 'user',
139
- ...stateOriginal?._isVikeEnhanced,
140
103
  },
141
104
  };
142
- assertIsVikeEnhanced(stateEnhanced);
143
- funcOriginal(stateEnhanced, ...rest);
144
- assert(isEqual(stateEnhanced, window.history.state));
105
+ assertIsEnhanced(state);
106
+ funcOriginal(state, ...rest);
107
+ // TO-DO/eventually remove excessive assertions to save client-side KBs
108
+ assert(isEqual(state, window.history.state));
145
109
  globalObject.previous = getHistoryInfo();
146
110
  // Workaround https://github.com/vikejs/vike/issues/2504#issuecomment-3149764736
147
111
  queueMicrotask(() => {
148
- if (isEqual(stateEnhanced, window.history.state))
112
+ if (isEqual(state, window.history.state))
149
113
  return;
150
- Object.assign(stateEnhanced, window.history.state);
151
- assertIsVikeEnhanced(stateEnhanced);
152
- replaceHistoryStateOriginal(stateEnhanced, rest[1]);
153
- assert(isEqual(stateEnhanced, window.history.state));
114
+ Object.assign(state, window.history.state);
115
+ assertIsEnhanced(state);
116
+ replaceHistoryStateOriginal(state, rest[1]);
117
+ assert(isEqual(state, window.history.state));
154
118
  });
155
119
  };
156
120
  });
157
121
  }
158
122
  function isEqual(state1, state2) {
159
- return deepEqual(state1?._isVikeEnhanced, state2?._isVikeEnhanced);
123
+ return deepEqual(state1?.vike, state2?.vike);
160
124
  }
161
- function isVikeEnhanced(state) {
162
- if (state?._isVikeEnhanced) {
125
+ function isEnhanced(state) {
126
+ if (state?.vike) {
163
127
  /* We don't use the assert() below to save client-side KBs.
164
- const vikeData = state._isVikeEnhanced
128
+ const vikeData = state.vike
165
129
  assert(isObject(vikeData))
166
130
  assert(hasProp(vikeData, 'timestamp', 'number'))
167
131
  assert(hasProp(vikeData, 'scrollPosition'))
@@ -174,8 +138,8 @@ function isVikeEnhanced(state) {
174
138
  }
175
139
  return false;
176
140
  }
177
- function assertIsVikeEnhanced(state) {
178
- if (isVikeEnhanced(state))
141
+ function assertIsEnhanced(state) {
142
+ if (isEnhanced(state))
179
143
  return;
180
144
  assert(false, { state });
181
145
  }
@@ -187,33 +151,32 @@ function getHistoryInfo() {
187
151
  }
188
152
  function onPopStateBegin() {
189
153
  const { previous } = globalObject;
190
- const isHistoryStateEnhanced = isVikeEnhanced(window.history.state);
154
+ const isStateEnhanced = isEnhanced(window.history.state);
191
155
  // Either:
192
156
  // - `window.history.pushState(null, '', '/some-path')` , or
193
157
  // - hash navigation
194
158
  // - Click on `<a href="#some-hash">`
195
159
  // - Using the `location` API (only hash navigation)
196
160
  // See comments a the top of the ./initOnPopState.ts file.
197
- const isHistoryStatePristine = window.history.state === null;
198
- if (!isHistoryStateEnhanced && !isHistoryStatePristine) {
161
+ const isStatePristine = window.history.state === null;
162
+ if (!isStateEnhanced && !isStatePristine) {
199
163
  // Going to a history entry not created by Vike — entering another "SPA realm" => hard reload
200
164
  // https://github.com/vikejs/vike/issues/2801#issuecomment-3490431479
201
165
  redirectHard(getCurrentUrl());
202
166
  return { skip: true };
203
167
  }
204
- if (!isHistoryStateEnhanced)
205
- enhanceHistoryState();
206
- assertIsVikeEnhanced(window.history.state);
168
+ if (!isStateEnhanced)
169
+ enhance();
207
170
  const current = getHistoryInfo();
208
171
  globalObject.previous = current;
209
172
  // Let the browser handle hash navigations.
210
173
  // - Upon hash navigation: `isHistoryStatePristine===true` (see comment above).
211
- if (isHistoryStatePristine) {
174
+ if (isStatePristine) {
212
175
  return { skip: true };
213
176
  }
214
177
  return { previous, current };
215
178
  }
216
179
  function initHistory() {
217
180
  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)
218
- enhanceHistoryState(); // enhance very first window.history.state which is `null`
181
+ enhance(); // enhance very first window.history.state which is `null`
219
182
  }
@@ -37,18 +37,18 @@ async function onPopState() {
37
37
  await handleHistoryNavigation(previous, current);
38
38
  }
39
39
  async function handleHistoryNavigation(previous, current) {
40
- const scrollTarget = current.state._isVikeEnhanced.scrollPosition || undefined;
40
+ const scrollTarget = current.state.vike.scrollPosition || undefined;
41
41
  const isHashNavigation = removeHash(current.url) === removeHash(previous.url) && current.url !== previous.url;
42
42
  if (isHashNavigation) {
43
43
  // We have to scroll ourselves because we have set `window.history.scrollRestoration = 'manual'`
44
44
  setScrollPosition(scrollTarget);
45
45
  return;
46
46
  }
47
- const isUserPushStateNavigation = current.state._isVikeEnhanced.triggeredBy === 'user' || previous.state._isVikeEnhanced.triggeredBy === 'user';
47
+ const isUserPushStateNavigation = current.state.vike.triggeredBy === 'user' || previous.state.vike.triggeredBy === 'user';
48
48
  const doNotRenderIfSamePage = isUserPushStateNavigation;
49
- const isBackwardNavigation = !current.state._isVikeEnhanced.timestamp || !previous.state._isVikeEnhanced.timestamp
49
+ const isBackwardNavigation = !current.state.vike.timestamp || !previous.state.vike.timestamp
50
50
  ? null
51
- : current.state._isVikeEnhanced.timestamp < previous.state._isVikeEnhanced.timestamp;
51
+ : current.state.vike.timestamp < previous.state.vike.timestamp;
52
52
  await renderPageClient({ scrollTarget, isBackwardNavigation, doNotRenderIfSamePage, isHistoryNavigation: true });
53
53
  }
54
54
  function removeHash(url) {
@@ -220,6 +220,10 @@ function logHttpRequest(urlOriginal, pageContextInit, requestId) {
220
220
  logRuntimeInfo?.(getRequestInfoMessage(urlOriginal), pageContext_logRuntime, 'info');
221
221
  }
222
222
  /*
223
+ const arrowRight = pc.dim('→')
224
+ const arrowLeft = pc.dim('←')
225
+ */
226
+ /*
223
227
  const arrowRight = pc.dim('>>')
224
228
  const arrowLeft = pc.dim('<<')
225
229
  /*/
@@ -1 +1 @@
1
- export declare const PROJECT_VERSION: "0.4.247-commit-dbcfa58";
1
+ export declare const PROJECT_VERSION: "0.4.247-commit-e331796";
@@ -1,2 +1,2 @@
1
1
  // Automatically updated by @brillout/release-me
2
- export const PROJECT_VERSION = '0.4.247-commit-dbcfa58';
2
+ export const PROJECT_VERSION = '0.4.247-commit-e331796';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vike",
3
- "version": "0.4.247-commit-dbcfa58",
3
+ "version": "0.4.247-commit-e331796",
4
4
  "repository": "https://github.com/vikejs/vike",
5
5
  "exports": {
6
6
  "./server": {