@lytjs/ssr 6.0.0 → 6.4.0
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/index.cjs +482 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +241 -1
- package/dist/index.d.ts +241 -1
- package/dist/index.mjs +470 -7
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/index.d.cts
CHANGED
|
@@ -49,6 +49,16 @@ interface StreamRenderOptions {
|
|
|
49
49
|
onShellReady?: () => void;
|
|
50
50
|
/** 错误回调 */
|
|
51
51
|
onError?: (error: Error) => void;
|
|
52
|
+
/** 流式渲染超时时间(毫秒),默认 30000 */
|
|
53
|
+
timeout?: number;
|
|
54
|
+
/** 当渲染超时时的回退 HTML 内容 */
|
|
55
|
+
fallbackHtml?: string;
|
|
56
|
+
/** 流控制:每秒最大字节数,用于防止突发流量 */
|
|
57
|
+
maxBytesPerSecond?: number;
|
|
58
|
+
/** 是否启用错误恢复模式,默认 true */
|
|
59
|
+
errorRecovery?: boolean;
|
|
60
|
+
/** 单个组件渲染超时(毫秒),默认 5000 */
|
|
61
|
+
componentTimeout?: number;
|
|
52
62
|
}
|
|
53
63
|
/** 异步数据预取上下文 */
|
|
54
64
|
interface DataPrefetchContext {
|
|
@@ -90,6 +100,8 @@ interface EnhancedStreamRenderOptions extends StreamRenderOptions {
|
|
|
90
100
|
* - 支持 Suspense 边界(先发送 shell,再发送异步内容)
|
|
91
101
|
* - 使用 TextEncoder 编码为 Uint8Array
|
|
92
102
|
* - 可配置分块大小和回调
|
|
103
|
+
* - 超时控制和错误恢复
|
|
104
|
+
* - 流速率控制
|
|
93
105
|
*
|
|
94
106
|
* @param vnode - 要渲染的 VNode
|
|
95
107
|
* @param options - 流式渲染配置选项
|
|
@@ -101,6 +113,7 @@ interface EnhancedStreamRenderOptions extends StreamRenderOptions {
|
|
|
101
113
|
* chunkSize: 2048,
|
|
102
114
|
* onShellReady: () => console.log('Shell 已发送'),
|
|
103
115
|
* onError: (err) => console.error(err),
|
|
116
|
+
* timeout: 30000,
|
|
104
117
|
* });
|
|
105
118
|
*
|
|
106
119
|
* for await (const chunk of stream) {
|
|
@@ -182,6 +195,15 @@ interface SSGOptions {
|
|
|
182
195
|
globalScripts?: string[];
|
|
183
196
|
/** 全局额外样式 */
|
|
184
197
|
globalStyles?: string[];
|
|
198
|
+
/** ISR 配置(增量静态再生成) */
|
|
199
|
+
isr?: {
|
|
200
|
+
/** 重新验证间隔(秒),0 表示按需重新验证 */
|
|
201
|
+
revalidate?: number;
|
|
202
|
+
/** 是否启用增量静态再生成 */
|
|
203
|
+
enabled?: boolean;
|
|
204
|
+
/** 预渲染 fallback 页面 */
|
|
205
|
+
fallback?: 'blocking' | boolean;
|
|
206
|
+
};
|
|
185
207
|
}
|
|
186
208
|
/**
|
|
187
209
|
* 将生成的 HTML 写入文件系统
|
|
@@ -263,6 +285,224 @@ declare function generateRouteManifest(pages: SSGPage[], baseUrl?: string): Arra
|
|
|
263
285
|
* @returns 错误信息数组,空数组表示全部合法
|
|
264
286
|
*/
|
|
265
287
|
declare function validatePages(pages: SSGPage[]): string[];
|
|
288
|
+
/**
|
|
289
|
+
* 创建 ISR 中间件
|
|
290
|
+
*
|
|
291
|
+
* @description
|
|
292
|
+
* 创建用于 Express/Fastify 等框架的 ISR 中间件,
|
|
293
|
+
* 支持增量静态再生成和背景重新验证。
|
|
294
|
+
*
|
|
295
|
+
* @param options - ISR 选项
|
|
296
|
+
* @returns 中间件函数
|
|
297
|
+
*
|
|
298
|
+
* @example
|
|
299
|
+
* ```typescript
|
|
300
|
+
* import express from 'express';
|
|
301
|
+
* import { createISRMiddleware, generateStaticPages } from '@lytjs/ssr';
|
|
302
|
+
*
|
|
303
|
+
* const app = express();
|
|
304
|
+
*
|
|
305
|
+
* // 预生成的页面
|
|
306
|
+
* const pages = [
|
|
307
|
+
* { path: '/', component: homeComponent },
|
|
308
|
+
* ];
|
|
309
|
+
*
|
|
310
|
+
* const staticPages = generateStaticPages(pages);
|
|
311
|
+
*
|
|
312
|
+
* app.use(createISRMiddleware({
|
|
313
|
+
* staticPages,
|
|
314
|
+
* revalidate: 60, // 60秒后重新验证
|
|
315
|
+
* async regenerate(path) {
|
|
316
|
+
* const page = pages.find(p => p.path === path);
|
|
317
|
+
* if (page) {
|
|
318
|
+
* return generateStaticPages([page]).get('/index.html')!;
|
|
319
|
+
* }
|
|
320
|
+
* throw new Error('Page not found');
|
|
321
|
+
* }
|
|
322
|
+
* }));
|
|
323
|
+
* ```
|
|
324
|
+
*/
|
|
325
|
+
declare function createISRMiddleware(options: {
|
|
326
|
+
/** 预生成的静态页面 */
|
|
327
|
+
staticPages: Map<string, string>;
|
|
328
|
+
/** 重新验证间隔(秒) */
|
|
329
|
+
revalidate?: number;
|
|
330
|
+
/** 是否启用 ISR */
|
|
331
|
+
enabled?: boolean;
|
|
332
|
+
/** 重新生成页面的函数 */
|
|
333
|
+
regenerate?: (path: string) => Promise<string>;
|
|
334
|
+
}): (req: any, res: any, next: any) => Promise<any>;
|
|
335
|
+
/**
|
|
336
|
+
* 触发按需重新验证
|
|
337
|
+
*
|
|
338
|
+
* @description
|
|
339
|
+
* 手动触发特定路径的重新验证,适合于内容更新后调用。
|
|
340
|
+
*
|
|
341
|
+
* @param path - 要重新验证的路径
|
|
342
|
+
* @param regenerate - 重新生成页面的函数
|
|
343
|
+
* @returns Promise<string> 新生成的 HTML
|
|
344
|
+
*
|
|
345
|
+
* @example
|
|
346
|
+
* ```typescript
|
|
347
|
+
* // 当博客文章更新时
|
|
348
|
+
* await revalidateOnDemand(
|
|
349
|
+
* '/blog/my-post',
|
|
350
|
+
* async () => generatePostHTML('my-post')
|
|
351
|
+
* );
|
|
352
|
+
* ```
|
|
353
|
+
*/
|
|
354
|
+
declare function revalidateOnDemand(path: string, regenerate: () => Promise<string>): Promise<string>;
|
|
355
|
+
/**
|
|
356
|
+
* 获取 ISR 缓存统计信息
|
|
357
|
+
*
|
|
358
|
+
* @description
|
|
359
|
+
* 获取当前 ISR 缓存的统计信息,用于监控和调试。
|
|
360
|
+
*
|
|
361
|
+
* @returns 缓存统计信息
|
|
362
|
+
*/
|
|
363
|
+
declare function getISRCacheStats(): {
|
|
364
|
+
total: number;
|
|
365
|
+
paths: string[];
|
|
366
|
+
};
|
|
367
|
+
/**
|
|
368
|
+
* 清除 ISR 缓存
|
|
369
|
+
*
|
|
370
|
+
* @description
|
|
371
|
+
* 清除指定路径或所有过期的缓存。
|
|
372
|
+
*
|
|
373
|
+
* @param path - 可选,要清除的特定路径
|
|
374
|
+
* @param maxAge - 可选,清除超过指定秒数的缓存
|
|
375
|
+
*/
|
|
376
|
+
declare function clearISRCache(path?: string, maxAge?: number): void;
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* @lytjs/ssr - 服务端组件完善
|
|
380
|
+
*
|
|
381
|
+
* 提供服务端组件生命周期管理、数据预取优化、状态序列化等功能
|
|
382
|
+
*/
|
|
383
|
+
|
|
384
|
+
/** 服务端组件生命周期钩子类型 */
|
|
385
|
+
type ServerLifecycleHook = (context: ServerComponentContext) => Promise<void> | void;
|
|
386
|
+
/** 服务端组件上下文 */
|
|
387
|
+
interface ServerComponentContext {
|
|
388
|
+
/** 组件唯一 ID */
|
|
389
|
+
componentId: string;
|
|
390
|
+
/** 路由信息 */
|
|
391
|
+
route?: {
|
|
392
|
+
path: string;
|
|
393
|
+
params: Record<string, string>;
|
|
394
|
+
query: Record<string, string>;
|
|
395
|
+
};
|
|
396
|
+
/** 请求上下文 */
|
|
397
|
+
request?: {
|
|
398
|
+
headers: Record<string, string | undefined>;
|
|
399
|
+
cookies: Record<string, string>;
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
/** 服务端组件注册信息 */
|
|
403
|
+
interface ServerComponentRegistration {
|
|
404
|
+
/** 组件名称 */
|
|
405
|
+
name: string;
|
|
406
|
+
/** 组件渲染函数 */
|
|
407
|
+
render: () => VNode;
|
|
408
|
+
/** 服务端初始化钩子 */
|
|
409
|
+
onServerInit?: ServerLifecycleHook;
|
|
410
|
+
/** 数据预取钩子 */
|
|
411
|
+
prefetch?: (context: DataPrefetchContext) => Promise<PrefetchResult>;
|
|
412
|
+
/** 服务端清理钩子 */
|
|
413
|
+
onServerCleanup?: ServerLifecycleHook;
|
|
414
|
+
}
|
|
415
|
+
/** 服务端组件状态管理器 */
|
|
416
|
+
declare class ServerComponentStateManager {
|
|
417
|
+
/** 注册的组件 */
|
|
418
|
+
private registrations;
|
|
419
|
+
/** 正在执行的预取请求 */
|
|
420
|
+
private pendingPrefetches;
|
|
421
|
+
/** 组件初始化状态 */
|
|
422
|
+
private initializationStates;
|
|
423
|
+
/**
|
|
424
|
+
* 注册服务端组件
|
|
425
|
+
*/
|
|
426
|
+
register(name: string, registration: ServerComponentRegistration): void;
|
|
427
|
+
/**
|
|
428
|
+
* 取消注册服务端组件
|
|
429
|
+
*/
|
|
430
|
+
unregister(name: string): void;
|
|
431
|
+
/**
|
|
432
|
+
* 获取已注册的组件
|
|
433
|
+
*/
|
|
434
|
+
getRegistration(name: string): ServerComponentRegistration | undefined;
|
|
435
|
+
/**
|
|
436
|
+
* 初始化服务端组件
|
|
437
|
+
*/
|
|
438
|
+
initializeComponent(name: string, context: ServerComponentContext): Promise<void>;
|
|
439
|
+
/**
|
|
440
|
+
* 清理服务端组件
|
|
441
|
+
*/
|
|
442
|
+
cleanupComponent(name: string, context: ServerComponentContext): Promise<void>;
|
|
443
|
+
/**
|
|
444
|
+
* 预取组件数据(带缓存)
|
|
445
|
+
*/
|
|
446
|
+
prefetchComponentData(name: string, context: DataPrefetchContext, cacheKey?: string): Promise<PrefetchResult>;
|
|
447
|
+
/**
|
|
448
|
+
* 清除所有缓存
|
|
449
|
+
*/
|
|
450
|
+
clearAll(): void;
|
|
451
|
+
}
|
|
452
|
+
/** 全局状态管理器实例 */
|
|
453
|
+
declare const stateManager: ServerComponentStateManager;
|
|
454
|
+
/**
|
|
455
|
+
* 注册服务端组件
|
|
456
|
+
*/
|
|
457
|
+
declare function registerServerComponent(name: string, registration: ServerComponentRegistration): void;
|
|
458
|
+
/**
|
|
459
|
+
* 取消注册服务端组件
|
|
460
|
+
*/
|
|
461
|
+
declare function unregisterServerComponent(name: string): void;
|
|
462
|
+
/**
|
|
463
|
+
* 从 VNode 树中收集需要预取数据的组件
|
|
464
|
+
*/
|
|
465
|
+
declare function collectPrefetchComponents(vnode: VNode | VNode[] | string | number | null | undefined): string[];
|
|
466
|
+
/**
|
|
467
|
+
* 并发预取多个组件的数据
|
|
468
|
+
*/
|
|
469
|
+
declare function prefetchAllComponents(components: string[], context: DataPrefetchContext): Promise<Record<string, PrefetchResult>>;
|
|
470
|
+
/**
|
|
471
|
+
* 安全的状态序列化
|
|
472
|
+
* 处理循环引用、日期、正则表达式等特殊类型
|
|
473
|
+
*/
|
|
474
|
+
declare function safeSerializeState(state: unknown): string;
|
|
475
|
+
/**
|
|
476
|
+
* 安全的状态反序列化
|
|
477
|
+
* 恢复特殊类型
|
|
478
|
+
*/
|
|
479
|
+
declare function safeDeserializeState(serialized: string): unknown;
|
|
480
|
+
/**
|
|
481
|
+
* 创建组件脱水状态
|
|
482
|
+
*/
|
|
483
|
+
interface ComponentDehydratedState {
|
|
484
|
+
/** 组件名称 */
|
|
485
|
+
componentName: string;
|
|
486
|
+
/** 组件 Props */
|
|
487
|
+
props: Record<string, unknown>;
|
|
488
|
+
/** 预取的数据 */
|
|
489
|
+
data?: Record<string, unknown>;
|
|
490
|
+
/** 错误信息 */
|
|
491
|
+
error?: string;
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* 构建完整的脱水状态
|
|
495
|
+
*/
|
|
496
|
+
declare function buildDehydratedState(prefetchResults: Record<string, PrefetchResult>): Record<string, ComponentDehydratedState>;
|
|
497
|
+
/**
|
|
498
|
+
* 服务端组件管理器装饰器
|
|
499
|
+
*/
|
|
500
|
+
declare function ServerComponent(options: {
|
|
501
|
+
name: string;
|
|
502
|
+
prefetch?: (context: DataPrefetchContext) => Promise<PrefetchResult>;
|
|
503
|
+
onInit?: ServerLifecycleHook;
|
|
504
|
+
onCleanup?: ServerLifecycleHook;
|
|
505
|
+
}): (target: any) => void;
|
|
266
506
|
|
|
267
507
|
/**
|
|
268
508
|
* @lytjs/ssr - 组件级水合提示
|
|
@@ -369,4 +609,4 @@ declare function serializeHydrationState(state: unknown): string;
|
|
|
369
609
|
*/
|
|
370
610
|
declare function createDehydratedState(vnode: VNode, initialState?: Record<string, unknown>): string;
|
|
371
611
|
|
|
372
|
-
export { type DataPrefetchContext, type EnhancedStreamRenderOptions, type HydrationHints, type HydrationState, type HydrationStrategy, type PrefetchResult, type PrefetchableComponent, type SSGOptions, type SSGPage, type StreamRenderOptions, VirtualList, createDehydratedState, createHydrationMarkers, _default as default, generateRouteManifest, generateStaticPages, getHydrationStrategy, renderToHtml, renderToStream, renderToStreamAsync, renderToStreamEnhanced, renderToString, resetComponentIdCounter, serializeHydrationState, validatePages, writeStaticFiles };
|
|
612
|
+
export { type ComponentDehydratedState, type DataPrefetchContext, type EnhancedStreamRenderOptions, type HydrationHints, type HydrationState, type HydrationStrategy, type PrefetchResult, type PrefetchableComponent, type SSGOptions, type SSGPage, ServerComponent, type ServerComponentContext, type ServerComponentRegistration, type ServerLifecycleHook, type StreamRenderOptions, VirtualList, buildDehydratedState, clearISRCache, collectPrefetchComponents, createDehydratedState, createHydrationMarkers, createISRMiddleware, _default as default, generateRouteManifest, generateStaticPages, getHydrationStrategy, getISRCacheStats, prefetchAllComponents, registerServerComponent, renderToHtml, renderToStream, renderToStreamAsync, renderToStreamEnhanced, renderToString, resetComponentIdCounter, revalidateOnDemand, safeDeserializeState, safeSerializeState, serializeHydrationState, stateManager, unregisterServerComponent, validatePages, writeStaticFiles };
|
package/dist/index.d.ts
CHANGED
|
@@ -49,6 +49,16 @@ interface StreamRenderOptions {
|
|
|
49
49
|
onShellReady?: () => void;
|
|
50
50
|
/** 错误回调 */
|
|
51
51
|
onError?: (error: Error) => void;
|
|
52
|
+
/** 流式渲染超时时间(毫秒),默认 30000 */
|
|
53
|
+
timeout?: number;
|
|
54
|
+
/** 当渲染超时时的回退 HTML 内容 */
|
|
55
|
+
fallbackHtml?: string;
|
|
56
|
+
/** 流控制:每秒最大字节数,用于防止突发流量 */
|
|
57
|
+
maxBytesPerSecond?: number;
|
|
58
|
+
/** 是否启用错误恢复模式,默认 true */
|
|
59
|
+
errorRecovery?: boolean;
|
|
60
|
+
/** 单个组件渲染超时(毫秒),默认 5000 */
|
|
61
|
+
componentTimeout?: number;
|
|
52
62
|
}
|
|
53
63
|
/** 异步数据预取上下文 */
|
|
54
64
|
interface DataPrefetchContext {
|
|
@@ -90,6 +100,8 @@ interface EnhancedStreamRenderOptions extends StreamRenderOptions {
|
|
|
90
100
|
* - 支持 Suspense 边界(先发送 shell,再发送异步内容)
|
|
91
101
|
* - 使用 TextEncoder 编码为 Uint8Array
|
|
92
102
|
* - 可配置分块大小和回调
|
|
103
|
+
* - 超时控制和错误恢复
|
|
104
|
+
* - 流速率控制
|
|
93
105
|
*
|
|
94
106
|
* @param vnode - 要渲染的 VNode
|
|
95
107
|
* @param options - 流式渲染配置选项
|
|
@@ -101,6 +113,7 @@ interface EnhancedStreamRenderOptions extends StreamRenderOptions {
|
|
|
101
113
|
* chunkSize: 2048,
|
|
102
114
|
* onShellReady: () => console.log('Shell 已发送'),
|
|
103
115
|
* onError: (err) => console.error(err),
|
|
116
|
+
* timeout: 30000,
|
|
104
117
|
* });
|
|
105
118
|
*
|
|
106
119
|
* for await (const chunk of stream) {
|
|
@@ -182,6 +195,15 @@ interface SSGOptions {
|
|
|
182
195
|
globalScripts?: string[];
|
|
183
196
|
/** 全局额外样式 */
|
|
184
197
|
globalStyles?: string[];
|
|
198
|
+
/** ISR 配置(增量静态再生成) */
|
|
199
|
+
isr?: {
|
|
200
|
+
/** 重新验证间隔(秒),0 表示按需重新验证 */
|
|
201
|
+
revalidate?: number;
|
|
202
|
+
/** 是否启用增量静态再生成 */
|
|
203
|
+
enabled?: boolean;
|
|
204
|
+
/** 预渲染 fallback 页面 */
|
|
205
|
+
fallback?: 'blocking' | boolean;
|
|
206
|
+
};
|
|
185
207
|
}
|
|
186
208
|
/**
|
|
187
209
|
* 将生成的 HTML 写入文件系统
|
|
@@ -263,6 +285,224 @@ declare function generateRouteManifest(pages: SSGPage[], baseUrl?: string): Arra
|
|
|
263
285
|
* @returns 错误信息数组,空数组表示全部合法
|
|
264
286
|
*/
|
|
265
287
|
declare function validatePages(pages: SSGPage[]): string[];
|
|
288
|
+
/**
|
|
289
|
+
* 创建 ISR 中间件
|
|
290
|
+
*
|
|
291
|
+
* @description
|
|
292
|
+
* 创建用于 Express/Fastify 等框架的 ISR 中间件,
|
|
293
|
+
* 支持增量静态再生成和背景重新验证。
|
|
294
|
+
*
|
|
295
|
+
* @param options - ISR 选项
|
|
296
|
+
* @returns 中间件函数
|
|
297
|
+
*
|
|
298
|
+
* @example
|
|
299
|
+
* ```typescript
|
|
300
|
+
* import express from 'express';
|
|
301
|
+
* import { createISRMiddleware, generateStaticPages } from '@lytjs/ssr';
|
|
302
|
+
*
|
|
303
|
+
* const app = express();
|
|
304
|
+
*
|
|
305
|
+
* // 预生成的页面
|
|
306
|
+
* const pages = [
|
|
307
|
+
* { path: '/', component: homeComponent },
|
|
308
|
+
* ];
|
|
309
|
+
*
|
|
310
|
+
* const staticPages = generateStaticPages(pages);
|
|
311
|
+
*
|
|
312
|
+
* app.use(createISRMiddleware({
|
|
313
|
+
* staticPages,
|
|
314
|
+
* revalidate: 60, // 60秒后重新验证
|
|
315
|
+
* async regenerate(path) {
|
|
316
|
+
* const page = pages.find(p => p.path === path);
|
|
317
|
+
* if (page) {
|
|
318
|
+
* return generateStaticPages([page]).get('/index.html')!;
|
|
319
|
+
* }
|
|
320
|
+
* throw new Error('Page not found');
|
|
321
|
+
* }
|
|
322
|
+
* }));
|
|
323
|
+
* ```
|
|
324
|
+
*/
|
|
325
|
+
declare function createISRMiddleware(options: {
|
|
326
|
+
/** 预生成的静态页面 */
|
|
327
|
+
staticPages: Map<string, string>;
|
|
328
|
+
/** 重新验证间隔(秒) */
|
|
329
|
+
revalidate?: number;
|
|
330
|
+
/** 是否启用 ISR */
|
|
331
|
+
enabled?: boolean;
|
|
332
|
+
/** 重新生成页面的函数 */
|
|
333
|
+
regenerate?: (path: string) => Promise<string>;
|
|
334
|
+
}): (req: any, res: any, next: any) => Promise<any>;
|
|
335
|
+
/**
|
|
336
|
+
* 触发按需重新验证
|
|
337
|
+
*
|
|
338
|
+
* @description
|
|
339
|
+
* 手动触发特定路径的重新验证,适合于内容更新后调用。
|
|
340
|
+
*
|
|
341
|
+
* @param path - 要重新验证的路径
|
|
342
|
+
* @param regenerate - 重新生成页面的函数
|
|
343
|
+
* @returns Promise<string> 新生成的 HTML
|
|
344
|
+
*
|
|
345
|
+
* @example
|
|
346
|
+
* ```typescript
|
|
347
|
+
* // 当博客文章更新时
|
|
348
|
+
* await revalidateOnDemand(
|
|
349
|
+
* '/blog/my-post',
|
|
350
|
+
* async () => generatePostHTML('my-post')
|
|
351
|
+
* );
|
|
352
|
+
* ```
|
|
353
|
+
*/
|
|
354
|
+
declare function revalidateOnDemand(path: string, regenerate: () => Promise<string>): Promise<string>;
|
|
355
|
+
/**
|
|
356
|
+
* 获取 ISR 缓存统计信息
|
|
357
|
+
*
|
|
358
|
+
* @description
|
|
359
|
+
* 获取当前 ISR 缓存的统计信息,用于监控和调试。
|
|
360
|
+
*
|
|
361
|
+
* @returns 缓存统计信息
|
|
362
|
+
*/
|
|
363
|
+
declare function getISRCacheStats(): {
|
|
364
|
+
total: number;
|
|
365
|
+
paths: string[];
|
|
366
|
+
};
|
|
367
|
+
/**
|
|
368
|
+
* 清除 ISR 缓存
|
|
369
|
+
*
|
|
370
|
+
* @description
|
|
371
|
+
* 清除指定路径或所有过期的缓存。
|
|
372
|
+
*
|
|
373
|
+
* @param path - 可选,要清除的特定路径
|
|
374
|
+
* @param maxAge - 可选,清除超过指定秒数的缓存
|
|
375
|
+
*/
|
|
376
|
+
declare function clearISRCache(path?: string, maxAge?: number): void;
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* @lytjs/ssr - 服务端组件完善
|
|
380
|
+
*
|
|
381
|
+
* 提供服务端组件生命周期管理、数据预取优化、状态序列化等功能
|
|
382
|
+
*/
|
|
383
|
+
|
|
384
|
+
/** 服务端组件生命周期钩子类型 */
|
|
385
|
+
type ServerLifecycleHook = (context: ServerComponentContext) => Promise<void> | void;
|
|
386
|
+
/** 服务端组件上下文 */
|
|
387
|
+
interface ServerComponentContext {
|
|
388
|
+
/** 组件唯一 ID */
|
|
389
|
+
componentId: string;
|
|
390
|
+
/** 路由信息 */
|
|
391
|
+
route?: {
|
|
392
|
+
path: string;
|
|
393
|
+
params: Record<string, string>;
|
|
394
|
+
query: Record<string, string>;
|
|
395
|
+
};
|
|
396
|
+
/** 请求上下文 */
|
|
397
|
+
request?: {
|
|
398
|
+
headers: Record<string, string | undefined>;
|
|
399
|
+
cookies: Record<string, string>;
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
/** 服务端组件注册信息 */
|
|
403
|
+
interface ServerComponentRegistration {
|
|
404
|
+
/** 组件名称 */
|
|
405
|
+
name: string;
|
|
406
|
+
/** 组件渲染函数 */
|
|
407
|
+
render: () => VNode;
|
|
408
|
+
/** 服务端初始化钩子 */
|
|
409
|
+
onServerInit?: ServerLifecycleHook;
|
|
410
|
+
/** 数据预取钩子 */
|
|
411
|
+
prefetch?: (context: DataPrefetchContext) => Promise<PrefetchResult>;
|
|
412
|
+
/** 服务端清理钩子 */
|
|
413
|
+
onServerCleanup?: ServerLifecycleHook;
|
|
414
|
+
}
|
|
415
|
+
/** 服务端组件状态管理器 */
|
|
416
|
+
declare class ServerComponentStateManager {
|
|
417
|
+
/** 注册的组件 */
|
|
418
|
+
private registrations;
|
|
419
|
+
/** 正在执行的预取请求 */
|
|
420
|
+
private pendingPrefetches;
|
|
421
|
+
/** 组件初始化状态 */
|
|
422
|
+
private initializationStates;
|
|
423
|
+
/**
|
|
424
|
+
* 注册服务端组件
|
|
425
|
+
*/
|
|
426
|
+
register(name: string, registration: ServerComponentRegistration): void;
|
|
427
|
+
/**
|
|
428
|
+
* 取消注册服务端组件
|
|
429
|
+
*/
|
|
430
|
+
unregister(name: string): void;
|
|
431
|
+
/**
|
|
432
|
+
* 获取已注册的组件
|
|
433
|
+
*/
|
|
434
|
+
getRegistration(name: string): ServerComponentRegistration | undefined;
|
|
435
|
+
/**
|
|
436
|
+
* 初始化服务端组件
|
|
437
|
+
*/
|
|
438
|
+
initializeComponent(name: string, context: ServerComponentContext): Promise<void>;
|
|
439
|
+
/**
|
|
440
|
+
* 清理服务端组件
|
|
441
|
+
*/
|
|
442
|
+
cleanupComponent(name: string, context: ServerComponentContext): Promise<void>;
|
|
443
|
+
/**
|
|
444
|
+
* 预取组件数据(带缓存)
|
|
445
|
+
*/
|
|
446
|
+
prefetchComponentData(name: string, context: DataPrefetchContext, cacheKey?: string): Promise<PrefetchResult>;
|
|
447
|
+
/**
|
|
448
|
+
* 清除所有缓存
|
|
449
|
+
*/
|
|
450
|
+
clearAll(): void;
|
|
451
|
+
}
|
|
452
|
+
/** 全局状态管理器实例 */
|
|
453
|
+
declare const stateManager: ServerComponentStateManager;
|
|
454
|
+
/**
|
|
455
|
+
* 注册服务端组件
|
|
456
|
+
*/
|
|
457
|
+
declare function registerServerComponent(name: string, registration: ServerComponentRegistration): void;
|
|
458
|
+
/**
|
|
459
|
+
* 取消注册服务端组件
|
|
460
|
+
*/
|
|
461
|
+
declare function unregisterServerComponent(name: string): void;
|
|
462
|
+
/**
|
|
463
|
+
* 从 VNode 树中收集需要预取数据的组件
|
|
464
|
+
*/
|
|
465
|
+
declare function collectPrefetchComponents(vnode: VNode | VNode[] | string | number | null | undefined): string[];
|
|
466
|
+
/**
|
|
467
|
+
* 并发预取多个组件的数据
|
|
468
|
+
*/
|
|
469
|
+
declare function prefetchAllComponents(components: string[], context: DataPrefetchContext): Promise<Record<string, PrefetchResult>>;
|
|
470
|
+
/**
|
|
471
|
+
* 安全的状态序列化
|
|
472
|
+
* 处理循环引用、日期、正则表达式等特殊类型
|
|
473
|
+
*/
|
|
474
|
+
declare function safeSerializeState(state: unknown): string;
|
|
475
|
+
/**
|
|
476
|
+
* 安全的状态反序列化
|
|
477
|
+
* 恢复特殊类型
|
|
478
|
+
*/
|
|
479
|
+
declare function safeDeserializeState(serialized: string): unknown;
|
|
480
|
+
/**
|
|
481
|
+
* 创建组件脱水状态
|
|
482
|
+
*/
|
|
483
|
+
interface ComponentDehydratedState {
|
|
484
|
+
/** 组件名称 */
|
|
485
|
+
componentName: string;
|
|
486
|
+
/** 组件 Props */
|
|
487
|
+
props: Record<string, unknown>;
|
|
488
|
+
/** 预取的数据 */
|
|
489
|
+
data?: Record<string, unknown>;
|
|
490
|
+
/** 错误信息 */
|
|
491
|
+
error?: string;
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* 构建完整的脱水状态
|
|
495
|
+
*/
|
|
496
|
+
declare function buildDehydratedState(prefetchResults: Record<string, PrefetchResult>): Record<string, ComponentDehydratedState>;
|
|
497
|
+
/**
|
|
498
|
+
* 服务端组件管理器装饰器
|
|
499
|
+
*/
|
|
500
|
+
declare function ServerComponent(options: {
|
|
501
|
+
name: string;
|
|
502
|
+
prefetch?: (context: DataPrefetchContext) => Promise<PrefetchResult>;
|
|
503
|
+
onInit?: ServerLifecycleHook;
|
|
504
|
+
onCleanup?: ServerLifecycleHook;
|
|
505
|
+
}): (target: any) => void;
|
|
266
506
|
|
|
267
507
|
/**
|
|
268
508
|
* @lytjs/ssr - 组件级水合提示
|
|
@@ -369,4 +609,4 @@ declare function serializeHydrationState(state: unknown): string;
|
|
|
369
609
|
*/
|
|
370
610
|
declare function createDehydratedState(vnode: VNode, initialState?: Record<string, unknown>): string;
|
|
371
611
|
|
|
372
|
-
export { type DataPrefetchContext, type EnhancedStreamRenderOptions, type HydrationHints, type HydrationState, type HydrationStrategy, type PrefetchResult, type PrefetchableComponent, type SSGOptions, type SSGPage, type StreamRenderOptions, VirtualList, createDehydratedState, createHydrationMarkers, _default as default, generateRouteManifest, generateStaticPages, getHydrationStrategy, renderToHtml, renderToStream, renderToStreamAsync, renderToStreamEnhanced, renderToString, resetComponentIdCounter, serializeHydrationState, validatePages, writeStaticFiles };
|
|
612
|
+
export { type ComponentDehydratedState, type DataPrefetchContext, type EnhancedStreamRenderOptions, type HydrationHints, type HydrationState, type HydrationStrategy, type PrefetchResult, type PrefetchableComponent, type SSGOptions, type SSGPage, ServerComponent, type ServerComponentContext, type ServerComponentRegistration, type ServerLifecycleHook, type StreamRenderOptions, VirtualList, buildDehydratedState, clearISRCache, collectPrefetchComponents, createDehydratedState, createHydrationMarkers, createISRMiddleware, _default as default, generateRouteManifest, generateStaticPages, getHydrationStrategy, getISRCacheStats, prefetchAllComponents, registerServerComponent, renderToHtml, renderToStream, renderToStreamAsync, renderToStreamEnhanced, renderToString, resetComponentIdCounter, revalidateOnDemand, safeDeserializeState, safeSerializeState, serializeHydrationState, stateManager, unregisterServerComponent, validatePages, writeStaticFiles };
|