zuby 1.0.47 → 1.0.48

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/config.d.ts CHANGED
@@ -48,3 +48,7 @@ export type ExecutePluginsParams = Omit<ZubyHookParams, 'command' | 'logger' | '
48
48
  * @param plugins
49
49
  */
50
50
  export declare const normalizePlugins: (plugins: (ZubyPlugin | ZubyPlugin[] | VitePluginOption | VitePluginOption[])[]) => Promise<(ZubyPlugin | VitePlugin)[]>;
51
+ /**
52
+ * Returns random build ID.
53
+ */
54
+ export declare const generateDefaultBuildId: () => string;
package/config.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { PLUGIN_HOOKS, } from './types.js';
2
2
  import { BUILD_CHUNKS_MANIFEST, ZUBY_CONFIG_FILE } from './constants.js';
3
3
  import { existsSync } from 'fs';
4
+ import { randomBytes } from 'crypto';
4
5
  import { bundleRequire } from 'bundle-require';
5
6
  import { createLogger } from './logger/index.js';
6
7
  // Plugins
@@ -103,6 +104,8 @@ export const mergeDefaultConfig = async (config) => {
103
104
  config.minifyCSS = config.minifyCSS ?? true;
104
105
  config.minifyHTML = config.minifyHTML ?? true;
105
106
  config.minifyJS = config.minifyJS ?? true;
107
+ // Build ID generator
108
+ config.generateBuildId = config.generateBuildId ?? generateDefaultBuildId;
106
109
  // Add logger
107
110
  config.customLogger =
108
111
  config.customLogger ??
@@ -134,6 +137,7 @@ export const mergeDefaultConfig = async (config) => {
134
137
  return {
135
138
  ...config,
136
139
  templateExtensions: ['js', 'jsx', 'ts', 'tsx'],
140
+ buildId: await config.generateBuildId(),
137
141
  };
138
142
  };
139
143
  /**
@@ -206,3 +210,9 @@ export const normalizePlugins = async (plugins) => {
206
210
  // Remove false, undefined, null values
207
211
  .filter(plugin => !!plugin);
208
212
  };
213
+ /**
214
+ * Returns random build ID.
215
+ */
216
+ export const generateDefaultBuildId = () => {
217
+ return randomBytes(8).toString('hex');
218
+ };
@@ -20,5 +20,6 @@ export declare class ZubyContext {
20
20
  locales: string[];
21
21
  defaultLocale: string;
22
22
  } | undefined;
23
+ get buildId(): string | undefined;
23
24
  }
24
25
  export declare const getContext: () => ZubyContext;
package/context/index.js CHANGED
@@ -23,6 +23,9 @@ export class ZubyContext {
23
23
  get i18n() {
24
24
  return this.rawContext.i18n;
25
25
  }
26
+ get buildId() {
27
+ return this.rawContext.buildId;
28
+ }
26
29
  }
27
30
  const getRawContext = () => {
28
31
  return globalThis.ZubyRawContext;
@@ -35,6 +35,10 @@ export interface ZubyRawContext {
35
35
  * @example 1.0.0
36
36
  */
37
37
  version?: string;
38
+ /**
39
+ * The build ID of the site.
40
+ */
41
+ buildId?: string;
38
42
  /**
39
43
  * The internalization config from ZubyConfig.
40
44
  * @example {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zuby",
3
- "version": "1.0.47",
3
+ "version": "1.0.48",
4
4
  "description": "Zuby.js is framework for building SPA apps using Vite",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -172,4 +172,9 @@ export declare class ZubyPageContext {
172
172
  * was made by the Zuby.js pre-render build step.
173
173
  */
174
174
  get isPrerendering(): boolean;
175
+ /**
176
+ * The current build ID of the site.
177
+ * @example ecdf1a94cc9b4f4c
178
+ */
179
+ get buildId(): string | undefined;
175
180
  }
@@ -225,4 +225,11 @@ export class ZubyPageContext {
225
225
  get isPrerendering() {
226
226
  return this._request?.headers?.get('user-agent') === 'zuby-prerender';
227
227
  }
228
+ /**
229
+ * The current build ID of the site.
230
+ * @example ecdf1a94cc9b4f4c
231
+ */
232
+ get buildId() {
233
+ return this._zubyContext.buildId;
234
+ }
228
235
  }
@@ -25,7 +25,7 @@ export default function index() {
25
25
  };
26
26
  }
27
27
  export async function generateCompileTimeContextCode(ssr) {
28
- const { site, i18n } = await getZubyInternalConfig();
28
+ const { site, i18n, buildId } = await getZubyInternalConfig();
29
29
  const { version } = await getZubyPackageConfig();
30
30
  return `globalThis.ZubyRawContext = {
31
31
  ...(globalThis.ZubyRawContext || {}),
@@ -34,6 +34,7 @@ export async function generateCompileTimeContextCode(ssr) {
34
34
  site: '${site || ''}',
35
35
  generator: 'Zuby.js ${version}',
36
36
  version: '${version}',
37
+ buildId: '${buildId}',
37
38
  i18n: ${JSON.stringify(i18n)},
38
39
  };`;
39
40
  }
package/preload/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { getContext } from '../context/index.js';
2
+ import { PREALOD_MANIFEST } from '../constants.js';
2
3
  /**
3
4
  * The set of links that were already preloaded.
4
5
  */
@@ -59,7 +60,7 @@ export function preloadPage(href, onHandle = () => { }) {
59
60
  return;
60
61
  }
61
62
  window.requestIdleCallback(async () => {
62
- await loadPreloadManifest();
63
+ const preloadManifest = await getPreloadManifest();
63
64
  // Preload assets such as scripts and styles
64
65
  const preloadAssets = preloadManifest?.[page.filename] || [];
65
66
  preloadAssets.forEach(href => preload(href));
@@ -78,10 +79,16 @@ function addPreloadEntry({ href, as }) {
78
79
  preloadLink.rel = 'preload';
79
80
  document.head.appendChild(preloadLink);
80
81
  }
81
- async function loadPreloadManifest() {
82
- if (preloadManifest)
83
- return;
84
- preloadManifest = {};
85
- const res = await fetch('/preload-manifest.json');
86
- preloadManifest = await res.json();
82
+ /**
83
+ * Returns preload manifest the way
84
+ * that ensures it is loaded only once.
85
+ */
86
+ async function getPreloadManifest() {
87
+ return (preloadManifest =
88
+ preloadManifest ||
89
+ (async () => {
90
+ const { buildId } = getContext();
91
+ const res = await fetch(`/${PREALOD_MANIFEST}?${buildId}`);
92
+ return res.json();
93
+ })());
87
94
  }
package/server/index.js CHANGED
@@ -2088,6 +2088,9 @@ var ZubyContext = class {
2088
2088
  get i18n() {
2089
2089
  return this.rawContext.i18n;
2090
2090
  }
2091
+ get buildId() {
2092
+ return this.rawContext.buildId;
2093
+ }
2091
2094
  };
2092
2095
  var getRawContext = () => {
2093
2096
  return globalThis.ZubyRawContext;
@@ -2326,6 +2329,13 @@ var ZubyPageContext = class {
2326
2329
  get isPrerendering() {
2327
2330
  return this._request?.headers?.get("user-agent") === "zuby-prerender";
2328
2331
  }
2332
+ /**
2333
+ * The current build ID of the site.
2334
+ * @example ecdf1a94cc9b4f4c
2335
+ */
2336
+ get buildId() {
2337
+ return this._zubyContext.buildId;
2338
+ }
2329
2339
  };
2330
2340
 
2331
2341
  // src/server/zubyRenderer.ts
package/types.d.ts CHANGED
@@ -122,6 +122,11 @@ export interface ZubyConfig {
122
122
  * @private
123
123
  */
124
124
  configFilePath?: string;
125
+ /**
126
+ * If you're building in multiple environments,
127
+ * you can use this option to generate consistent build IDs.
128
+ */
129
+ generateBuildId?: () => string | Promise<string>;
125
130
  }
126
131
  export interface ZubyInternalConfig extends Required<ZubyConfig> {
127
132
  /**
@@ -136,6 +141,10 @@ export interface ZubyInternalConfig extends Required<ZubyConfig> {
136
141
  * @default []
137
142
  */
138
143
  plugins: ZubyPlugin[];
144
+ /**
145
+ * The current build ID
146
+ */
147
+ buildId: string;
139
148
  }
140
149
  export interface BaseCommandOptions {
141
150
  /**
@@ -304,6 +313,14 @@ export interface ZubyPlugin extends VitePlugin {
304
313
  export interface ZubyConfigSetupHookParams {
305
314
  config: ZubyConfig;
306
315
  command: 'dev' | 'build';
316
+ addEntryTemplate: (entryFile: string) => void;
317
+ addAppTemplate: (appFile: string, path?: string) => void;
318
+ addLayoutTemplate: (layoutFile: string, path?: string) => void;
319
+ addInnerLayoutTemplate: (innerLayoutFile: string, path?: string) => void;
320
+ addErrorTemplate: (errorFile: string, path?: string) => void;
321
+ addLoaderTemplate: (loaderFile: string, path?: string) => void;
322
+ addPage: (pageFile: string, path?: string) => void;
323
+ addHandler: (handlerFile: string, path?: string) => void;
307
324
  }
308
325
  export interface ZubyConfigDoneHookParams extends ZubyConfigSetupHookParams {
309
326
  config: ZubyInternalConfig;