isr-engine 4.0.0 → 4.0.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "isr-engine",
3
- "version": "4.0.0",
3
+ "version": "4.0.1",
4
4
  "description": "ISR/SSG/Fallback rendering orchestration on Vite + React 19 RSC.",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
@@ -72,6 +72,7 @@ interface DefaultRscPayload {
72
72
  seoMeta?: PageSeoMeta | null;
73
73
  /** canonical / og:image 转绝对 URL 用 */
74
74
  siteBaseUrl?: string | null;
75
+ routeStatus?: 404;
75
76
  formState?: import('react-dom/client').ReactFormState;
76
77
  returnValue?: { ok: boolean; data: unknown };
77
78
  }
@@ -150,7 +151,7 @@ function transitionPayload(
150
151
 
151
152
  function PayloadRoot({ payload, generation, styleIds }: DefaultRscPayloadState): React.ReactNode {
152
153
  React.useLayoutEffect(() => {
153
- applySeoToDocument(payload.seoMeta, payload.siteBaseUrl ?? undefined);
154
+ applySeoToDocument(payload.seoMeta, payload.siteBaseUrl ?? undefined, payload.routeStatus);
154
155
  }, [payload]);
155
156
  if (!import.meta.env.DEV || generation === undefined) return payload.root;
156
157
  return (
@@ -61,6 +61,7 @@ interface DefaultRscPayload {
61
61
  seoMeta?: PageSeoMeta | null;
62
62
  /** 客户端 applySeoToDocument 用来把 canonical / og:image 转绝对 URL */
63
63
  siteBaseUrl?: string | null;
64
+ routeStatus?: 404;
64
65
  returnValue?: { ok: boolean; data: unknown };
65
66
  formState?: ReactFormState;
66
67
  }
@@ -248,7 +249,7 @@ export function defineServerEntry<C extends ServerCtx = ServerCtx>(
248
249
 
249
250
  const response = await runWithI18n(intl, () =>
250
251
  runRscPipeline(renderRequest, {
251
- routeStatus: notFound && !renderRequest.isAction ? 404 : undefined,
252
+ routeStatus: notFound ? 404 : undefined,
252
253
  intl,
253
254
  seoMeta,
254
255
  siteBaseUrl: hooks.siteBaseUrl,
@@ -297,6 +298,7 @@ async function runRscPipeline(
297
298
  extras: PipelineExtras
298
299
  ): Promise<Response> {
299
300
  const { request } = renderRequest;
301
+ const responseRouteStatus = renderRequest.isAction ? undefined : extras.routeStatus;
300
302
 
301
303
  let returnValue: DefaultRscPayload['returnValue'] | undefined;
302
304
  let formState: ReactFormState | undefined;
@@ -337,6 +339,7 @@ async function runRscPipeline(
337
339
  intl: extras.intl ?? null,
338
340
  seoMeta: extras.seoMeta ?? null,
339
341
  siteBaseUrl: extras.siteBaseUrl ?? null,
342
+ routeStatus: extras.routeStatus,
340
343
  formState,
341
344
  returnValue,
342
345
  };
@@ -357,7 +360,7 @@ async function runRscPipeline(
357
360
 
358
361
  if (renderRequest.isRsc) {
359
362
  return new Response(rscStream, {
360
- status: actionStatus ?? extras.routeStatus,
363
+ status: actionStatus ?? responseRouteStatus,
361
364
  headers: { 'content-type': 'text/x-component;charset=utf-8' },
362
365
  });
363
366
  }
@@ -410,7 +413,12 @@ async function runRscPipeline(
410
413
  let finalStream = ssrResult.stream;
411
414
  if (!ssrResult.csrShellFallback) {
412
415
  if (extras.seoMeta) {
413
- finalStream = injectSeoMeta(finalStream, extras.seoMeta, extras.siteBaseUrl);
416
+ finalStream = injectSeoMeta(
417
+ finalStream,
418
+ extras.seoMeta,
419
+ extras.siteBaseUrl,
420
+ extras.routeStatus
421
+ );
414
422
  }
415
423
  if (extras.headExtras) {
416
424
  finalStream = injectHeadExtras(finalStream, extras.headExtras);
@@ -418,7 +426,7 @@ async function runRscPipeline(
418
426
  }
419
427
 
420
428
  return new Response(finalStream, {
421
- status: ssrResult.status ?? extras.routeStatus,
429
+ status: ssrResult.status ?? responseRouteStatus,
422
430
  headers: responseHeaders,
423
431
  });
424
432
  }
@@ -70,7 +70,16 @@ function toAbsolute(href: string, baseUrl?: string): string {
70
70
  }
71
71
  }
72
72
 
73
- export function renderPageSeoMeta(meta: PageSeoMeta, baseUrl?: string): string {
73
+ function resolveCanonical(
74
+ meta: PageSeoMeta,
75
+ baseUrl?: string,
76
+ routeStatus?: 404
77
+ ): string | undefined {
78
+ if (meta.canonical) return toAbsolute(meta.canonical, baseUrl);
79
+ return routeStatus === 404 ? undefined : baseUrl;
80
+ }
81
+
82
+ export function renderPageSeoMeta(meta: PageSeoMeta, baseUrl?: string, routeStatus?: 404): string {
74
83
  const tags: string[] = [];
75
84
  const esc = (s: string) =>
76
85
  s.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
@@ -86,10 +95,8 @@ export function renderPageSeoMeta(meta: PageSeoMeta, baseUrl?: string): string {
86
95
  if (meta.noindex) {
87
96
  tags.push(`<meta data-isr-seo name="robots" content="noindex, nofollow">`);
88
97
  }
89
- if (meta.canonical || baseUrl) {
90
- const href = toAbsolute(meta.canonical || '', baseUrl) || baseUrl || '';
91
- if (href) tags.push(`<link data-isr-seo rel="canonical" href="${esc(href)}">`);
92
- }
98
+ const canonical = resolveCanonical(meta, baseUrl, routeStatus);
99
+ if (canonical) tags.push(`<link data-isr-seo rel="canonical" href="${esc(canonical)}">`);
93
100
  if (meta.alternates?.length) {
94
101
  for (const a of meta.alternates) {
95
102
  tags.push(
@@ -129,11 +136,12 @@ const HEAD_OPEN_RE = /<head\b[^>]*>/i;
129
136
  export function injectSeoMeta(
130
137
  source: ReadableStream<Uint8Array>,
131
138
  meta: PageSeoMeta,
132
- baseUrl?: string
139
+ baseUrl?: string,
140
+ routeStatus?: 404
133
141
  ): ReadableStream<Uint8Array> {
134
142
  const encoder = new TextEncoder();
135
143
  const decoder = new TextDecoder('utf-8', { fatal: false });
136
- const injection = '\n ' + renderPageSeoMeta(meta, baseUrl) + '\n';
144
+ const injection = '\n ' + renderPageSeoMeta(meta, baseUrl, routeStatus) + '\n';
137
145
 
138
146
  let buffer = '';
139
147
  let injected = false;
@@ -179,7 +187,11 @@ export function clearInjectedSeo(): void {
179
187
  }
180
188
 
181
189
  /** Replace managed head metadata after the route commits. */
182
- export function applySeoToDocument(meta: PageSeoMeta | null | undefined, baseUrl?: string): void {
190
+ export function applySeoToDocument(
191
+ meta: PageSeoMeta | null | undefined,
192
+ baseUrl?: string,
193
+ routeStatus?: 404
194
+ ): void {
183
195
  if (typeof document === 'undefined' || !document.head) return;
184
196
  const head = document.head;
185
197
  clearInjectedSeo();
@@ -215,10 +227,8 @@ export function applySeoToDocument(meta: PageSeoMeta | null | undefined, baseUrl
215
227
  if (meta.noindex) {
216
228
  fragments.push(tag('meta', { name: 'robots', content: 'noindex, nofollow' }));
217
229
  }
218
- if (meta.canonical || baseUrl) {
219
- const href = toAbsolute(meta.canonical || '', baseUrl) || baseUrl || '';
220
- if (href) fragments.push(tag('link', { rel: 'canonical', href }));
221
- }
230
+ const canonical = resolveCanonical(meta, baseUrl, routeStatus);
231
+ if (canonical) fragments.push(tag('link', { rel: 'canonical', href: canonical }));
222
232
  if (meta.alternates?.length) {
223
233
  for (const a of meta.alternates) {
224
234
  fragments.push(