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.
- package/dist/cjs/utils/PROJECT_VERSION.js +1 -1
- package/dist/esm/client/runtime-client-routing/history.d.ts +2 -4
- package/dist/esm/client/runtime-client-routing/history.js +35 -13
- package/dist/esm/client/runtime-client-routing/initClientRouter.js +2 -3
- package/dist/esm/utils/PROJECT_VERSION.d.ts +1 -1
- package/dist/esm/utils/PROJECT_VERSION.js +1 -1
- package/package.json +1 -1
|
@@ -2,8 +2,7 @@ export { pushHistoryState };
|
|
|
2
2
|
export { replaceHistoryStateOriginal };
|
|
3
3
|
export { onPopStateBegin };
|
|
4
4
|
export { saveScrollPosition };
|
|
5
|
-
export {
|
|
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
|
|
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 {
|
|
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
|
-
|
|
10
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
118
|
-
|
|
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
|
-
|
|
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
|
|
156
|
-
|
|
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 {
|
|
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
|
-
|
|
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-
|
|
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-
|
|
2
|
+
export const PROJECT_VERSION = '0.4.235-commit-7b1ab35';
|