vike 0.4.247-commit-c3d567b → 0.4.247-commit-9b74d50

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,11 +5,14 @@ export { saveScrollPosition };
5
5
  export { initHistory };
6
6
  export type { HistoryInfo };
7
7
  export type { ScrollPosition };
8
- type StateEnhanced = {
8
+ type VikeHistoryData = {
9
9
  timestamp: number;
10
10
  scrollPosition: null | ScrollPosition;
11
11
  triggeredBy: 'user' | 'vike' | 'browser';
12
- _isVikeEnhanced: true;
12
+ };
13
+ type StateEnhanced = {
14
+ _isVikeEnhanced: VikeHistoryData;
15
+ [key: string]: unknown;
13
16
  };
14
17
  type ScrollPosition = {
15
18
  x: number;
@@ -4,7 +4,7 @@ export { onPopStateBegin };
4
4
  export { saveScrollPosition };
5
5
  export { initHistory };
6
6
  import { getCurrentUrl } from '../shared/getCurrentUrl.js';
7
- import { assert, assertUsage, getGlobalObject, isObject, deepEqual, cast, redirectHard } from './utils.js';
7
+ import { assert, assertUsage, getGlobalObject, isObject, deepEqual, redirectHard } from './utils.js';
8
8
  const globalObject = getGlobalObject('history.ts', {
9
9
  monkeyPatched: false,
10
10
  previous: undefined,
@@ -28,20 +28,36 @@ function enhance(stateNotEnhanced) {
28
28
  let stateVikeEnhanced;
29
29
  if (!stateNotEnhanced) {
30
30
  stateVikeEnhanced = {
31
- timestamp,
32
- scrollPosition,
33
- triggeredBy,
34
- _isVikeEnhanced: true,
31
+ _isVikeEnhanced: {
32
+ timestamp,
33
+ scrollPosition,
34
+ triggeredBy,
35
+ },
35
36
  };
36
37
  }
37
38
  else {
38
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`.)
39
- cast(stateNotEnhanced);
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
+ }
40
54
  stateVikeEnhanced = {
41
- timestamp: stateNotEnhanced.timestamp ?? timestamp,
42
- scrollPosition: stateNotEnhanced.scrollPosition ?? scrollPosition,
43
- triggeredBy: stateNotEnhanced.triggeredBy ?? triggeredBy,
44
- _isVikeEnhanced: true,
55
+ ...stateNotEnhanced,
56
+ _isVikeEnhanced: {
57
+ timestamp: oldVikeData.timestamp ?? timestamp,
58
+ scrollPosition: oldVikeData.scrollPosition ?? scrollPosition,
59
+ triggeredBy: oldVikeData.triggeredBy ?? triggeredBy,
60
+ },
45
61
  };
46
62
  }
47
63
  assertIsVikeEnhanced(stateVikeEnhanced);
@@ -71,16 +87,17 @@ function saveScrollPosition() {
71
87
  if (!isVikeEnhanced(window.history.state))
72
88
  return;
73
89
  const state = getState();
74
- replaceHistoryState({ ...state, scrollPosition });
90
+ replaceHistoryState({ ...state, _isVikeEnhanced: { ...state._isVikeEnhanced, scrollPosition } });
75
91
  }
76
92
  function pushHistoryState(url, overwriteLastHistoryEntry) {
77
93
  if (!overwriteLastHistoryEntry) {
78
94
  const state = {
79
- timestamp: getTimestamp(),
80
- // 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.
81
- scrollPosition: null,
82
- triggeredBy: 'vike',
83
- _isVikeEnhanced: true,
95
+ _isVikeEnhanced: {
96
+ timestamp: getTimestamp(),
97
+ // 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
+ scrollPosition: null,
99
+ triggeredBy: 'vike',
100
+ },
84
101
  };
85
102
  // 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.
86
103
  // - https://github.com/vikejs/vike/issues/1582
@@ -114,37 +131,43 @@ function monkeyPatchHistoryAPI() {
114
131
  const stateEnhanced = isVikeEnhanced(stateOriginal)
115
132
  ? stateOriginal
116
133
  : {
117
- _isVikeEnhanced: true,
118
- scrollPosition: getScrollPosition(),
119
- timestamp: getTimestamp(),
120
- triggeredBy: 'user',
121
134
  ...stateOriginal,
135
+ _isVikeEnhanced: {
136
+ scrollPosition: getScrollPosition(),
137
+ timestamp: getTimestamp(),
138
+ triggeredBy: 'user',
139
+ ...stateOriginal?._isVikeEnhanced,
140
+ },
122
141
  };
123
142
  assertIsVikeEnhanced(stateEnhanced);
124
143
  funcOriginal(stateEnhanced, ...rest);
125
- assert(deepEqual(stateEnhanced, window.history.state));
144
+ assert(isEqual(stateEnhanced, window.history.state));
126
145
  globalObject.previous = getHistoryInfo();
127
146
  // Workaround https://github.com/vikejs/vike/issues/2504#issuecomment-3149764736
128
147
  queueMicrotask(() => {
129
- if (deepEqual(stateEnhanced, window.history.state))
148
+ if (isEqual(stateEnhanced, window.history.state))
130
149
  return;
131
150
  Object.assign(stateEnhanced, window.history.state);
132
151
  assertIsVikeEnhanced(stateEnhanced);
133
152
  replaceHistoryStateOriginal(stateEnhanced, rest[1]);
134
- assert(deepEqual(stateEnhanced, window.history.state));
153
+ assert(isEqual(stateEnhanced, window.history.state));
135
154
  });
136
155
  };
137
156
  });
138
157
  }
158
+ function isEqual(state1, state2) {
159
+ return isObject(state1) && isObject(state2) && deepEqual(state1._isVikeEnhanced, state2._isVikeEnhanced);
160
+ }
139
161
  function isVikeEnhanced(state) {
140
162
  if (isObject(state) && '_isVikeEnhanced' in state) {
141
163
  /* We don't use the assert() below to save client-side KBs.
142
- assert(hasProp(state, '_isVikeEnhanced', 'true'))
143
- assert(hasProp(state, 'timestamp', 'number'))
144
- assert(hasProp(state, 'scrollPosition'))
145
- if (state.scrollPosition !== null) {
146
- assert(hasProp(state, 'scrollPosition', 'object'))
147
- assert(hasProp(state.scrollPosition, 'x', 'number') && hasProp(state.scrollPosition, 'y', 'number'))
164
+ const vikeData = state._isVikeEnhanced
165
+ assert(isObject(vikeData))
166
+ assert(hasProp(vikeData, 'timestamp', 'number'))
167
+ assert(hasProp(vikeData, 'scrollPosition'))
168
+ if (vikeData.scrollPosition !== null) {
169
+ assert(hasProp(vikeData, 'scrollPosition', 'object'))
170
+ assert(hasProp(vikeData.scrollPosition, 'x', 'number') && hasProp(vikeData.scrollPosition, 'y', 'number'))
148
171
  }
149
172
  //*/
150
173
  return true;
@@ -37,16 +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.scrollPosition || undefined;
40
+ const scrollTarget = current.state._isVikeEnhanced.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.triggeredBy === 'user' || previous.state.triggeredBy === 'user';
47
+ const isUserPushStateNavigation = current.state._isVikeEnhanced.triggeredBy === 'user' || previous.state._isVikeEnhanced.triggeredBy === 'user';
48
48
  const doNotRenderIfSamePage = isUserPushStateNavigation;
49
- const isBackwardNavigation = !current.state.timestamp || !previous.state.timestamp ? null : current.state.timestamp < previous.state.timestamp;
49
+ const isBackwardNavigation = !current.state._isVikeEnhanced.timestamp || !previous.state._isVikeEnhanced.timestamp
50
+ ? null
51
+ : current.state._isVikeEnhanced.timestamp < previous.state._isVikeEnhanced.timestamp;
50
52
  await renderPageClient({ scrollTarget, isBackwardNavigation, doNotRenderIfSamePage, isHistoryNavigation: true });
51
53
  }
52
54
  function removeHash(url) {
@@ -5,29 +5,31 @@ import { getMagicString } from '../../shared/getMagicString.js';
5
5
  // But, anyways, we want to prepend the banner at the beginning of each module, not at the beginning of each file (I believe that's what Rollup's banner feature does).
6
6
  function pluginModuleBanner() {
7
7
  let config;
8
+ let isEnabled = false;
8
9
  return [
9
10
  {
10
11
  name: 'vike:build:pluginModuleBanner',
11
12
  enforce: 'post',
12
13
  apply: 'build',
13
14
  applyToEnvironment(environment) {
14
- const { config } = environment;
15
- const { consumer } = config;
16
- const { minify } = config.build;
17
- assert(minify === false || minify, { minify, consumer });
18
- return !minify;
15
+ return checkIsEnabled(environment.config);
19
16
  },
20
17
  configResolved: {
21
18
  handler(config_) {
22
19
  config = config_;
20
+ isEnabled = checkIsEnabled(config);
23
21
  },
24
22
  },
25
23
  transform: {
26
24
  order: 'post',
27
- /* Using a Rolldown hook filter doesn't make sense here: we apply this transformer to each module. But we use applyToEnvironment() to conditionally apply this plugin.
28
- filter: {},
29
- */
25
+ filter: {
26
+ get id() {
27
+ return isEnabled ? undefined : 'this-module-id-does-not-exist-and-never-matches-any-module';
28
+ },
29
+ },
30
30
  handler(code, id) {
31
+ if (!isEnabled)
32
+ return undefined;
31
33
  const { minify } = this.environment.config.build;
32
34
  assert(minify === false, { minify });
33
35
  if (id.startsWith('\0'))
@@ -47,3 +49,13 @@ function pluginModuleBanner() {
47
49
  },
48
50
  ];
49
51
  }
52
+ function checkIsEnabled(config) {
53
+ const { minify } = config.build;
54
+ assert(minify === false || minify, { minify });
55
+ const isEnabled = !minify;
56
+ // Avoid the legal comments inserted in the transform() hook to be removed.
57
+ // https://github.com/vitejs/vite/issues/21085#issuecomment-3502781005
58
+ if (isEnabled && config.esbuild)
59
+ config.esbuild.legalComments = 'inline';
60
+ return isEnabled;
61
+ }
@@ -1,4 +1,5 @@
1
1
  export { pluginReplaceConstantsGlobalThis };
2
+ export { VIRTUAL_FILE_ID_constantsGlobalThis };
2
3
  export type _LoadDeclareGlobal__VIKE__IS = never;
3
4
  import type { Plugin } from 'vite';
4
5
  declare global {
@@ -8,4 +9,5 @@ declare global {
8
9
  var __VIKE__IS_CLIENT: boolean;
9
10
  var __VIKE__IS_DEBUG: boolean;
10
11
  }
12
+ declare const VIRTUAL_FILE_ID_constantsGlobalThis = "virtual:vike:server:constantsGlobalThis";
11
13
  declare function pluginReplaceConstantsGlobalThis(): Plugin[];
@@ -1,16 +1,17 @@
1
1
  export { pluginReplaceConstantsGlobalThis };
2
+ export { VIRTUAL_FILE_ID_constantsGlobalThis };
2
3
  import { assert, isDebug, addVirtualFileIdPrefix, escapeRegex } from '../utils.js';
3
4
  import { isViteServerSide_applyToEnvironment, isViteServerSide_configEnvironment, isViteServerSide_extraSafe, } from '../shared/isViteServerSide.js';
4
5
  const isDebugVal = isDebug();
5
6
  globalThis.__VIKE__IS_CLIENT = false;
6
7
  globalThis.__VIKE__IS_DEBUG = isDebugVal;
7
- const VIRTUAL_FILE_ID = 'virtual:vike:server:globalThis-constants';
8
+ const VIRTUAL_FILE_ID_constantsGlobalThis = 'virtual:vike:server:constantsGlobalThis';
8
9
  const filterRolldown = {
9
10
  id: {
10
- include: new RegExp(escapeRegex(VIRTUAL_FILE_ID)),
11
+ include: new RegExp(escapeRegex(VIRTUAL_FILE_ID_constantsGlobalThis)),
11
12
  },
12
13
  };
13
- const filterFunction = (id) => id === VIRTUAL_FILE_ID || id === addVirtualFileIdPrefix(VIRTUAL_FILE_ID);
14
+ const filterFunction = (id) => id === VIRTUAL_FILE_ID_constantsGlobalThis || id === addVirtualFileIdPrefix(VIRTUAL_FILE_ID_constantsGlobalThis);
14
15
  function pluginReplaceConstantsGlobalThis() {
15
16
  let config;
16
17
  let isDev;
@@ -54,7 +55,7 @@ function pluginReplaceConstantsGlobalThis() {
54
55
  filter: filterRolldown,
55
56
  handler(id) {
56
57
  assert(filterFunction(id));
57
- assert(id === VIRTUAL_FILE_ID);
58
+ assert(id === VIRTUAL_FILE_ID_constantsGlobalThis);
58
59
  return addVirtualFileIdPrefix(id);
59
60
  },
60
61
  },
@@ -3,6 +3,7 @@ import { generateVirtualFileId } from '../../../../shared-server-node/virtualFil
3
3
  import { debug } from './debug.js';
4
4
  import { getVikeConfigInternal } from '../../shared/resolveVikeConfigInternal.js';
5
5
  import { serializeConfigValues, } from '../../../../shared-server-client/page-configs/serialize/serializeConfigValues.js';
6
+ import { VIRTUAL_FILE_ID_constantsGlobalThis } from '../pluginReplaceConstantsGlobalThis.js';
6
7
  async function generateVirtualFileGlobalEntry(isForClientSide, isDev, id, isClientRouting) {
7
8
  const vikeConfig = await getVikeConfigInternal(true);
8
9
  const { _pageConfigs: pageConfigs, _pageConfigGlobal: pageConfigGlobal } = vikeConfig;
@@ -13,7 +14,7 @@ function getCode(pageConfigs, pageConfigGlobal, isForClientSide, isDev, id, isCl
13
14
  const importStatements = [];
14
15
  const filesEnv = new Map();
15
16
  if (!isForClientSide) {
16
- importStatements.push("import 'virtual:vike:server:globalThis-constants';");
17
+ importStatements.push(`import '${VIRTUAL_FILE_ID_constantsGlobalThis}';`);
17
18
  }
18
19
  lines.push('export const pageConfigsSerialized = [');
19
20
  lines.push(getCodePageConfigsSerialized(pageConfigs, isForClientSide, isClientRouting, isDev, importStatements, filesEnv));
@@ -27,7 +28,7 @@ function getCode(pageConfigs, pageConfigGlobal, isForClientSide, isDev, id, isCl
27
28
  }
28
29
  let code = [...importStatements, ...lines].join('\n');
29
30
  if (!isForClientSide) {
30
- code = "import 'virtual:vike:server:globalThis-constants';\n" + code;
31
+ code = `import '${VIRTUAL_FILE_ID_constantsGlobalThis}';\n` + code;
31
32
  }
32
33
  debug(id, isForClientSide ? 'CLIENT-SIDE' : 'SERVER-SIDE', code);
33
34
  return code;
@@ -9,6 +9,7 @@ import { getVikeConfigInternal, isV1Design as isV1Design_ } from '../../shared/r
9
9
  import { getOutDirs } from '../../shared/getOutDirs.js';
10
10
  import { isViteServerSide_extraSafe } from '../../shared/isViteServerSide.js';
11
11
  import { resolveIncludeAssetsImportedByServer } from '../../../../server/runtime/renderPageServer/getPageAssets/retrievePageAssetsProd.js';
12
+ import { VIRTUAL_FILE_ID_constantsGlobalThis } from '../pluginReplaceConstantsGlobalThis.js';
12
13
  async function generateVirtualFileGlobalEntryWithOldDesign(id, options, config, env, isDev) {
13
14
  const idParsed = parseVirtualFileId(id);
14
15
  assert(idParsed && idParsed.type === 'global-entry');
@@ -24,7 +25,7 @@ async function getCode(config, isForClientSide, isClientRouting, isDev, id) {
24
25
  assert(isDev === !isBuild);
25
26
  let content = '';
26
27
  if (!isForClientSide) {
27
- content += "import 'virtual:vike:server:globalThis-constants';\n";
28
+ content += `import '${VIRTUAL_FILE_ID_constantsGlobalThis}';\n`;
28
29
  }
29
30
  {
30
31
  const globRoots = getGlobRoots(config);
@@ -1 +1 @@
1
- export declare const PROJECT_VERSION: "0.4.247-commit-c3d567b";
1
+ export declare const PROJECT_VERSION: "0.4.247-commit-9b74d50";
@@ -1,2 +1,2 @@
1
1
  // Automatically updated by @brillout/release-me
2
- export const PROJECT_VERSION = '0.4.247-commit-c3d567b';
2
+ export const PROJECT_VERSION = '0.4.247-commit-9b74d50';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vike",
3
- "version": "0.4.247-commit-c3d567b",
3
+ "version": "0.4.247-commit-9b74d50",
4
4
  "repository": "https://github.com/vikejs/vike",
5
5
  "exports": {
6
6
  "./server": {