isr-engine 3.1.0 → 3.1.1
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/cli/cli.cjs +1 -1
- package/dist/cli/cli.js +1 -1
- package/dist/{createCacheInspectorMiddleware-Bjku5v62.js → createCacheInspectorMiddleware-BpMpmP_a.js} +2 -2
- package/dist/{createCacheInspectorMiddleware-Bjku5v62.js.map → createCacheInspectorMiddleware-BpMpmP_a.js.map} +1 -1
- package/dist/{createCacheInspectorMiddleware-DG1_VLBS.cjs → createCacheInspectorMiddleware-DvRhF_-D.cjs} +2 -2
- package/dist/{createCacheInspectorMiddleware-DG1_VLBS.cjs.map → createCacheInspectorMiddleware-DvRhF_-D.cjs.map} +1 -1
- package/dist/{createISRApp-DYVU8ydT.js → createISRApp-D88Xp0Fb.js} +5 -5
- package/dist/createISRApp-D88Xp0Fb.js.map +1 -0
- package/dist/{createISRApp-HX6X8UNb.cjs → createISRApp-tRkpdmpp.cjs} +5 -5
- package/dist/createISRApp-tRkpdmpp.cjs.map +1 -0
- package/dist/{createImagePlugin-BZJHOvN9.js → createImagePlugin-Cdz3QfrJ.js} +2 -2
- package/dist/{createImagePlugin-BZJHOvN9.js.map → createImagePlugin-Cdz3QfrJ.js.map} +1 -1
- package/dist/{createImagePlugin-Ca4t_WhM.cjs → createImagePlugin-DomuzpoI.cjs} +2 -2
- package/dist/{createImagePlugin-Ca4t_WhM.cjs.map → createImagePlugin-DomuzpoI.cjs.map} +1 -1
- package/dist/defaults/runtime/defineSiteHooks.cjs +1 -1
- package/dist/defaults/runtime/defineSiteHooks.d.ts.map +1 -1
- package/dist/defaults/runtime/defineSiteHooks.js +1 -1
- package/dist/{defineSiteHooks-w2IrF5-D.cjs → defineSiteHooks-BuhvW5YB.cjs} +45 -36
- package/dist/defineSiteHooks-BuhvW5YB.cjs.map +1 -0
- package/dist/{defineSiteHooks-CV4tqHvB.js → defineSiteHooks-bHtGQftZ.js} +45 -36
- package/dist/defineSiteHooks-bHtGQftZ.js.map +1 -0
- package/dist/isr-engine.cjs +3 -3
- package/dist/isr-engine.js +3 -3
- package/dist/types/ISRConfig.d.ts +3 -2
- package/dist/types/ISRConfig.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/defaults/runtime/defineSiteHooks.ts +58 -54
- package/src/types/ISRConfig.ts +3 -2
- package/dist/createISRApp-DYVU8ydT.js.map +0 -1
- package/dist/createISRApp-HX6X8UNb.cjs.map +0 -1
- package/dist/defineSiteHooks-CV4tqHvB.js.map +0 -1
- package/dist/defineSiteHooks-w2IrF5-D.cjs.map +0 -1
|
@@ -108,36 +108,6 @@ export interface CreateAdminSeoLoaderOptions {
|
|
|
108
108
|
dynamicResolvers?: readonly DynamicSeoResolver[];
|
|
109
109
|
}
|
|
110
110
|
|
|
111
|
-
/**
|
|
112
|
-
* 内部用:把 `/books/:id` 这种 pattern 编译成 RegExp + 参数名列表。
|
|
113
|
-
* `:name` 匹配单段(不跨 /)。`*` 匹配任意后缀(包括 /),收成 ":wildcard" param。
|
|
114
|
-
* 编译后缓存,避免每次请求重算。
|
|
115
|
-
*/
|
|
116
|
-
interface CompiledSeoResolver {
|
|
117
|
-
re: RegExp;
|
|
118
|
-
paramNames: string[];
|
|
119
|
-
resolver: DynamicSeoResolver;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
function compileSeoResolver(resolver: DynamicSeoResolver): CompiledSeoResolver {
|
|
123
|
-
const paramNames: string[] = [];
|
|
124
|
-
const reSrc = resolver.pattern
|
|
125
|
-
.replace(/[.+?^${}()|[\]\\]/g, '\\$&')
|
|
126
|
-
.replace(/:([A-Za-z_][A-Za-z0-9_]*)|\\\*/g, (m, name) => {
|
|
127
|
-
if (m === '\\*') {
|
|
128
|
-
paramNames.push('wildcard');
|
|
129
|
-
return '(.*)';
|
|
130
|
-
}
|
|
131
|
-
paramNames.push(name);
|
|
132
|
-
return '([^/]+)';
|
|
133
|
-
});
|
|
134
|
-
return {
|
|
135
|
-
re: new RegExp(`^${reSrc}$`),
|
|
136
|
-
paramNames,
|
|
137
|
-
resolver,
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
|
|
141
111
|
export interface DefineAdminSiteHooksOptions {
|
|
142
112
|
i18n?: {
|
|
143
113
|
locales?: readonly string[];
|
|
@@ -271,23 +241,38 @@ export interface SiteHooksConfig {
|
|
|
271
241
|
headExtras?: (ctx: { nonce?: string }) => string | undefined;
|
|
272
242
|
}
|
|
273
243
|
|
|
274
|
-
interface
|
|
244
|
+
interface CompiledSeoPattern {
|
|
275
245
|
re: RegExp;
|
|
276
246
|
paramNames: string[];
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
interface CompiledRoute extends CompiledSeoPattern {
|
|
277
250
|
resolver: (params: Record<string, string>) => Promise<PageSeoMeta | null> | PageSeoMeta | null;
|
|
278
251
|
}
|
|
279
252
|
|
|
280
|
-
|
|
281
|
-
|
|
253
|
+
function escapeSeoPatternLiteral(literal: string): string {
|
|
254
|
+
return literal.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/** Both SEO entry points recognize named parameters and a terminal /* before escaping literals. */
|
|
258
|
+
function compileSeoPattern(pattern: string, captureWildcard = false): CompiledSeoPattern {
|
|
282
259
|
const paramNames: string[] = [];
|
|
283
260
|
const wildcard = pattern.endsWith('/*');
|
|
284
261
|
const base = wildcard ? pattern.slice(0, -2) : pattern;
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
262
|
+
let cursor = 0;
|
|
263
|
+
let source = '';
|
|
264
|
+
for (const match of base.matchAll(/:(\w+)/g)) {
|
|
265
|
+
source += escapeSeoPatternLiteral(base.slice(cursor, match.index)) + '([^/]+)';
|
|
266
|
+
paramNames.push(match[1]);
|
|
267
|
+
cursor = match.index + match[0].length;
|
|
268
|
+
}
|
|
269
|
+
source += escapeSeoPatternLiteral(base.slice(cursor));
|
|
270
|
+
|
|
271
|
+
if (wildcard) {
|
|
272
|
+
if (captureWildcard) paramNames.push('wildcard');
|
|
273
|
+
source += captureWildcard ? '(?:/(.*))?' : '(?:/.*)?';
|
|
274
|
+
}
|
|
275
|
+
return { re: new RegExp(`^${source}$`), paramNames };
|
|
291
276
|
}
|
|
292
277
|
|
|
293
278
|
function fillTemplate(template: string, vars: Record<string, string>): string {
|
|
@@ -335,9 +320,10 @@ export function createAdminSeoLoader(
|
|
|
335
320
|
const endpoint = options.endpoint ?? '/api/seo?path={pathname}';
|
|
336
321
|
const fallbackEntries = options.fallbackEntries ?? [];
|
|
337
322
|
// 编译一次 + 缓存。N 个 resolver 在每个请求里只是 N 次正则 test,cost 可忽略。
|
|
338
|
-
const compiledResolvers
|
|
339
|
-
|
|
340
|
-
|
|
323
|
+
const compiledResolvers = (options.dynamicResolvers ?? []).map(resolver => ({
|
|
324
|
+
...compileSeoPattern(resolver.pattern, true),
|
|
325
|
+
resolver,
|
|
326
|
+
}));
|
|
341
327
|
|
|
342
328
|
return async (params, ctx) => {
|
|
343
329
|
const pathname = normalizePathname(params.pathname || '/');
|
|
@@ -390,6 +376,23 @@ export function defineAdminSiteHooks(options: DefineAdminSiteHooksOptions = {}):
|
|
|
390
376
|
return createAdminSiteHooks(options, createEmptyRuntimeConfig());
|
|
391
377
|
}
|
|
392
378
|
|
|
379
|
+
function createAdminSeoEntry(
|
|
380
|
+
seo: NonNullable<DefineAdminSiteHooksOptions['seo']>,
|
|
381
|
+
runtimeSeo: Partial<RuntimeSeoConfig>
|
|
382
|
+
): SeoLocalEntry {
|
|
383
|
+
return {
|
|
384
|
+
load: createAdminSeoLoader({
|
|
385
|
+
endpoint: seo.endpoint ?? runtimeSeo.endpoint,
|
|
386
|
+
fallbackEntries: normalizeAdminSeoFallbackEntries(
|
|
387
|
+
seo.fallbackLocal ?? runtimeSeo.fallbackLocal
|
|
388
|
+
),
|
|
389
|
+
timeoutMs: seo.timeoutMs ?? runtimeSeo.timeoutMs,
|
|
390
|
+
dynamicResolvers: seo.dynamicResolvers ?? runtimeSeo.dynamicResolvers,
|
|
391
|
+
}),
|
|
392
|
+
ttl: seo.ttl ?? runtimeSeo.ttl ?? 60_000,
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
|
|
393
396
|
function createAdminSiteHooks(
|
|
394
397
|
options: DefineAdminSiteHooksOptions,
|
|
395
398
|
runtime: SiteRuntimeConfig
|
|
@@ -426,17 +429,7 @@ function createAdminSiteHooks(
|
|
|
426
429
|
ttl: i18n.ttl ?? runtimeI18n.ttl ?? 60_000,
|
|
427
430
|
},
|
|
428
431
|
seo: {
|
|
429
|
-
'/*':
|
|
430
|
-
load: createAdminSeoLoader({
|
|
431
|
-
endpoint: seo.endpoint ?? runtimeSeo.endpoint,
|
|
432
|
-
fallbackEntries: normalizeAdminSeoFallbackEntries(
|
|
433
|
-
seo.fallbackLocal ?? runtimeSeo.fallbackLocal
|
|
434
|
-
),
|
|
435
|
-
timeoutMs: seo.timeoutMs ?? runtimeSeo.timeoutMs,
|
|
436
|
-
dynamicResolvers: seo.dynamicResolvers ?? runtimeSeo.dynamicResolvers,
|
|
437
|
-
}),
|
|
438
|
-
ttl: seo.ttl ?? runtimeSeo.ttl ?? 60_000,
|
|
439
|
-
},
|
|
432
|
+
'/*': createAdminSeoEntry(seo, runtimeSeo),
|
|
440
433
|
},
|
|
441
434
|
},
|
|
442
435
|
runtime
|
|
@@ -526,10 +519,21 @@ export function applyRuntimeToServerHooks<T extends object>(
|
|
|
526
519
|
} as T;
|
|
527
520
|
}
|
|
528
521
|
|
|
522
|
+
// A dynamic SEO table on its own does not opt a plain server entry into the admin i18n
|
|
523
|
+
// loader or wrap its beforeRequest callback.
|
|
524
|
+
const runtimeSeo = runtime.seo;
|
|
525
|
+
const runtimeSeoHook =
|
|
526
|
+
!hasUserContentHooks && runtimeSeo?.dynamicResolvers?.length
|
|
527
|
+
? createSiteHooks(
|
|
528
|
+
{ intl: runtime.i18n, seo: { '/*': createAdminSeoEntry({}, runtimeSeo) } },
|
|
529
|
+
runtime
|
|
530
|
+
).loadSeoMeta
|
|
531
|
+
: undefined;
|
|
529
532
|
return {
|
|
530
533
|
...hooks,
|
|
531
534
|
siteBaseUrl,
|
|
532
535
|
apiBaseUrl,
|
|
536
|
+
...(runtimeSeoHook && { loadSeoMeta: runtimeSeoHook }),
|
|
533
537
|
} as T;
|
|
534
538
|
}
|
|
535
539
|
|
|
@@ -642,7 +646,7 @@ function createSiteHooks(config: SiteHooksConfig, runtime: SiteRuntimeConfig): S
|
|
|
642
646
|
>();
|
|
643
647
|
|
|
644
648
|
for (const [pattern, value] of Object.entries(config.seo ?? {})) {
|
|
645
|
-
const { re, paramNames } =
|
|
649
|
+
const { re, paramNames } = compileSeoPattern(pattern);
|
|
646
650
|
|
|
647
651
|
if (typeof value === 'function') {
|
|
648
652
|
// 自定义 resolver
|
package/src/types/ISRConfig.ts
CHANGED
|
@@ -143,7 +143,7 @@ export interface RuntimeI18nConfig {
|
|
|
143
143
|
*/
|
|
144
144
|
export interface DynamicSeoResolver {
|
|
145
145
|
/**
|
|
146
|
-
* 路径 pattern。支持 `:name`
|
|
146
|
+
* 路径 pattern。支持 `:name` 匹配单段和末尾 `/*` 匹配路径本身及其子路径。
|
|
147
147
|
* 例:`/books/:id`、`/u/:handle`、`/dashboard/*`
|
|
148
148
|
*/
|
|
149
149
|
pattern: string;
|
|
@@ -151,7 +151,8 @@ export interface DynamicSeoResolver {
|
|
|
151
151
|
* 命中后的 resolver。返回 PageSeoMeta(平铺)或 null。
|
|
152
152
|
* 抛错会被引擎吞掉并写到 onError,不会导致 SSR 失败。
|
|
153
153
|
*
|
|
154
|
-
* input.params
|
|
154
|
+
* input.params 包含命名参数。末尾 `/*` 还会返回 `wildcard`,
|
|
155
|
+
* 其值为不带起始斜杠的剩余路径,本身命中时为空字符串。
|
|
155
156
|
* input.locale 是当前请求 locale(可能 undefined,特别是没启 i18n 时)。
|
|
156
157
|
* input.services / input.runtime 给到完整 runtime context,可拿 api origin。
|
|
157
158
|
*/
|