@teyik0/furin 0.4.0-alpha.2 → 0.5.0-alpha.2
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/build/compile-entry.d.ts +2 -1
- package/dist/build/compile-entry.ts +17 -13
- package/dist/build/elysia-aot.d.ts +9 -0
- package/dist/build/entry-template.d.ts +5 -1
- package/dist/build/entry-template.ts +27 -6
- package/dist/build/index.js +969 -528
- package/dist/build/private-server-sourcemaps.d.ts +2 -0
- package/dist/build/server-app-entry.d.ts +12 -0
- package/dist/build/types.d.ts +2 -0
- package/dist/cache.d.ts +1 -0
- package/dist/cache.js +185 -0
- package/dist/cli/index.js +983 -541
- package/dist/client/document.d.ts +2 -0
- package/dist/client/internal/runtime-types.d.ts +8 -1
- package/dist/client/link.js +232 -11
- package/dist/client/sync.d.ts +1 -0
- package/dist/client.js +11 -0
- package/dist/config.d.ts +28 -22
- package/dist/config.js +25 -2
- package/dist/define-route.d.ts +273 -1009
- package/dist/furin.d.ts +20 -5
- package/dist/furin.js +6431 -5457
- package/dist/plugin/routes.d.ts +6 -0
- package/dist/rsc-client.js +9 -1
- package/dist/rsc.js +19 -7
- package/dist/server/assets/production.d.ts +3 -0
- package/dist/server/auto-invalidate/plugin.d.ts +8 -24
- package/dist/server/auto-invalidate/runtime.d.ts +2 -2
- package/dist/server/browser-events/shutdown.d.ts +3 -0
- package/dist/server/cache/invalidation.d.ts +1 -1
- package/dist/server/cache/page-cache-state.d.ts +5 -0
- package/dist/server/cache/page-cache.d.ts +53 -0
- package/dist/server/cache/redis/index.d.ts +41 -0
- package/dist/server/cache/redis/index.js +451 -0
- package/dist/server/cache/redis/scripts.d.ts +6 -0
- package/dist/server/context-logger.d.ts +2 -2
- package/dist/server/dev/plugin.d.ts +1 -0
- package/dist/server/dev/runtime.production.d.ts +1 -0
- package/dist/server/dev-page-plugin.d.ts +7 -0
- package/dist/server/evlog.d.ts +14 -0
- package/dist/server/internal.d.ts +6 -0
- package/dist/server/internal.js +2539 -0
- package/dist/server/production-server.d.ts +13 -0
- package/dist/server/production-server.js +2906 -0
- package/dist/server/render/not-found.d.ts +1 -1
- package/dist/server/render/ssg.d.ts +5 -0
- package/dist/server/render/static-params.d.ts +4 -0
- package/dist/server/router/defined-route.d.ts +1 -1
- package/dist/server/router/hmr.d.ts +6 -0
- package/dist/server/router/hmr.production.d.ts +5 -0
- package/dist/server/router/plugin.d.ts +5 -1
- package/dist/server/router/schemas.d.ts +1 -0
- package/dist/server/sync/index.js +798 -117
- package/dist/server/sync/plugin.d.ts +31 -158
- package/dist/server/sync/stream.d.ts +38 -49
- package/dist/shared/elysia-contract.d.ts +8 -14
- package/dist/shared/production-build.d.ts +2 -0
- package/package.json +19 -8
- package/src/adapter/README.md +9 -2
- package/src/adapter/bun.ts +146 -41
- package/src/adapter/package.ts +6 -2
- package/src/adapter/runtime-build.ts +11 -4
- package/src/adapter/static.ts +8 -6
- package/src/adapter/vercel.ts +64 -31
- package/src/build/client.ts +18 -1
- package/src/build/compile-entry.ts +17 -13
- package/src/build/elysia-aot.ts +102 -0
- package/src/build/entry-template.ts +27 -6
- package/src/build/hydrate.ts +5 -1
- package/src/build/index.ts +5 -5
- package/src/build/private-server-sourcemaps.ts +20 -0
- package/src/build/production-instrumentation.ts +2 -2
- package/src/build/server-app-entry.ts +61 -0
- package/src/build/shared.ts +3 -1
- package/src/build/ssg-cache.ts +5 -10
- package/src/build/types.ts +2 -0
- package/src/build/virtual-entry.ts +5 -4
- package/src/cache.ts +12 -0
- package/src/cli/config.ts +3 -3
- package/src/cli/index.ts +6 -2
- package/src/client/document.tsx +19 -0
- package/src/client/internal/runtime-types.ts +10 -1
- package/src/client/sync.ts +1 -0
- package/src/config.ts +33 -4
- package/src/define-route.ts +266 -128
- package/src/furin.ts +168 -154
- package/src/plugin/route-config-autofix.ts +11 -2
- package/src/plugin/routes.ts +85 -29
- package/src/rsc/codec.ts +11 -6
- package/src/rsc/render-error.ts +3 -1
- package/src/server/assets/production.ts +99 -0
- package/src/server/auto-invalidate/plugin.ts +4 -4
- package/src/server/auto-invalidate/runtime.ts +74 -42
- package/src/server/browser-events/plugin.ts +13 -1
- package/src/server/browser-events/shutdown.ts +32 -0
- package/src/server/cache/invalidation.ts +61 -20
- package/src/server/cache/page-cache-state.ts +22 -0
- package/src/server/cache/page-cache.ts +300 -0
- package/src/server/cache/redis/index.ts +367 -0
- package/src/server/cache/redis/scripts.ts +200 -0
- package/src/server/context-logger.ts +5 -5
- package/src/server/dev/plugin.ts +10 -1
- package/src/server/dev/runtime.production.ts +4 -0
- package/src/server/dev-page-plugin.ts +54 -10
- package/src/server/evlog.ts +106 -0
- package/src/server/internal.ts +7 -0
- package/src/server/production-server.ts +135 -0
- package/src/server/render/document.tsx +7 -1
- package/src/server/render/isr.ts +480 -132
- package/src/server/render/loaders.ts +18 -12
- package/src/server/render/not-found.ts +4 -4
- package/src/server/render/ppr-document.ts +2 -2
- package/src/server/render/ppr-route.ts +193 -7
- package/src/server/render/ssg.ts +148 -4
- package/src/server/render/ssr.ts +6 -7
- package/src/server/render/static-params.ts +174 -0
- package/src/server/render/template.ts +5 -1
- package/src/server/router/defined-route.ts +2 -1
- package/src/server/router/discovery.ts +3 -1
- package/src/server/router/hmr.production.ts +11 -0
- package/src/server/router/hmr.ts +85 -76
- package/src/server/router/plugin.ts +118 -50
- package/src/server/router/schema-merge.ts +10 -3
- package/src/server/router/schemas.ts +174 -61
- package/src/server/sync/plugin.ts +14 -10
- package/src/server/sync/response.ts +3 -3
- package/src/server/sync/stream.ts +43 -2
- package/src/shared/elysia-contract.ts +23 -25
- package/src/shared/production-build.ts +20 -0
|
@@ -5,7 +5,7 @@ import type { RequestLoaderContext } from "../../define-route.ts";
|
|
|
5
5
|
import { isFurinRscRenderError } from "../../rsc/render-error.ts";
|
|
6
6
|
import { computeErrorDigest } from "../../shared/digest.ts";
|
|
7
7
|
import { type FurinNotFoundError, isNotFoundError } from "../../shared/not-found.ts";
|
|
8
|
-
import {
|
|
8
|
+
import { getLogger } from "../context-logger.ts";
|
|
9
9
|
import { currentInstrumentationRequest, emitLoaderFinished } from "../devtools/instrumentation.ts";
|
|
10
10
|
import type { ResolvedRoute } from "../router/types.ts";
|
|
11
11
|
import { IS_DEV } from "../runtime-env.ts";
|
|
@@ -63,13 +63,17 @@ const FURIN_RESERVED_KEY_PREFIX = "__furin";
|
|
|
63
63
|
* names is silently dead on arrival.
|
|
64
64
|
*/
|
|
65
65
|
const ROUTE_CTX_RESERVED_KEYS = new Set([
|
|
66
|
+
"catch",
|
|
66
67
|
"children",
|
|
68
|
+
"finally",
|
|
67
69
|
"key",
|
|
68
70
|
"params",
|
|
69
71
|
"path",
|
|
70
72
|
"query",
|
|
71
73
|
"ref",
|
|
72
74
|
"requestData",
|
|
75
|
+
"then",
|
|
76
|
+
"toJSON",
|
|
73
77
|
]);
|
|
74
78
|
|
|
75
79
|
function assertPublicLoaderKey(key: string): void {
|
|
@@ -137,11 +141,13 @@ function createLoaderCtx(
|
|
|
137
141
|
if (prop === "then" || prop === "catch" || prop === "finally" || prop === "toJSON") {
|
|
138
142
|
return Reflect.get(target, prop);
|
|
139
143
|
}
|
|
140
|
-
//
|
|
141
|
-
//
|
|
142
|
-
//
|
|
143
|
-
|
|
144
|
-
|
|
144
|
+
// Kiana keeps status/problem/redirect/defer and decorators on the
|
|
145
|
+
// Elysia context prototype. Preserve those while excluding generic
|
|
146
|
+
// Object.prototype keys that may legitimately be parent loader data.
|
|
147
|
+
if (
|
|
148
|
+
Object.hasOwn(target, prop) ||
|
|
149
|
+
(prop in target && !Object.hasOwn(Object.prototype, prop))
|
|
150
|
+
) {
|
|
145
151
|
return target[prop];
|
|
146
152
|
}
|
|
147
153
|
// Everything else is a parent-data field → individual lazy Promise.
|
|
@@ -203,7 +209,7 @@ function createRequestLoaderContext(ctx: Context): RequestLoaderContext {
|
|
|
203
209
|
get: (name: string) => headers.get(name),
|
|
204
210
|
has: (name: string) => headers.has(name),
|
|
205
211
|
}),
|
|
206
|
-
log:
|
|
212
|
+
log: getLogger(),
|
|
207
213
|
params: ctx.params,
|
|
208
214
|
path: ctx.path,
|
|
209
215
|
query: ctx.query,
|
|
@@ -417,7 +423,7 @@ async function normalizeLoaderError(
|
|
|
417
423
|
return { error: err, headers, message, status, type: "error" };
|
|
418
424
|
}
|
|
419
425
|
if (isFurinRscRenderError(err)) {
|
|
420
|
-
|
|
426
|
+
getLogger().error(err);
|
|
421
427
|
return {
|
|
422
428
|
error: err,
|
|
423
429
|
headers,
|
|
@@ -443,11 +449,11 @@ async function runLoadersInternal(
|
|
|
443
449
|
try {
|
|
444
450
|
const requestData = includeRequestData ? runRequestLoaderData(route, ctx) : undefined;
|
|
445
451
|
// Inject `log` so loaders can destructure it directly as `({ log })`.
|
|
446
|
-
//
|
|
452
|
+
// getLogger() resolves the correct logger for every rendering context:
|
|
447
453
|
// live request → evlog request-scoped logger, synthetic render → detached
|
|
448
454
|
// createLogger() from runInSyntheticRenderScope, outside any context → no-op.
|
|
449
455
|
const ctxRecord = includeRequestData
|
|
450
|
-
?
|
|
456
|
+
? Object.assign(Object.create(Object.getPrototypeOf(ctx)), ctx, { log: getLogger() })
|
|
451
457
|
: createPublicLoaderContext(ctx);
|
|
452
458
|
const loaderMap = new Map<RuntimeRoute, Promise<Record<string, unknown>>>();
|
|
453
459
|
|
|
@@ -484,7 +490,7 @@ async function runLoadersInternal(
|
|
|
484
490
|
// ancestor's field (deepest wins). Surface the collision instead.
|
|
485
491
|
for (const key of Object.keys(own)) {
|
|
486
492
|
if (Object.hasOwn(acc, key)) {
|
|
487
|
-
|
|
493
|
+
getLogger().warn(
|
|
488
494
|
`[furin] Loader data collision on "${key}" for ${ctx.path}: a deeper loader overwrites the value inherited from its layout chain (deepest wins).`
|
|
489
495
|
);
|
|
490
496
|
}
|
|
@@ -553,7 +559,7 @@ function createPublicLoaderContext(ctx: Context): { [key: string]: unknown } {
|
|
|
553
559
|
Object.defineProperties(publicContext, {
|
|
554
560
|
cookie: { enumerable: true, get: fail },
|
|
555
561
|
headers: { enumerable: true, get: fail },
|
|
556
|
-
log: { enumerable: true, value:
|
|
562
|
+
log: { enumerable: true, value: getLogger() },
|
|
557
563
|
params: { enumerable: true, value: ctx.params },
|
|
558
564
|
path: { enumerable: true, value: ctx.path },
|
|
559
565
|
query: { enumerable: true, value: ctx.query },
|
|
@@ -6,7 +6,7 @@ import type { RouterContextValue } from "../../client/router/types.ts";
|
|
|
6
6
|
import type { FurinSchema } from "../../shared/elysia-contract.ts";
|
|
7
7
|
import { FurinNotFoundError } from "../../shared/not-found.ts";
|
|
8
8
|
import type { SearchParamsInput } from "../../shared/search-params.ts";
|
|
9
|
-
import {
|
|
9
|
+
import { getLogger } from "../context-logger.ts";
|
|
10
10
|
import { currentInstance } from "../instance.ts";
|
|
11
11
|
import { parseRouteQuery } from "../router/schemas.ts";
|
|
12
12
|
import type { RootLayout } from "../router/types.ts";
|
|
@@ -24,7 +24,7 @@ import {
|
|
|
24
24
|
|
|
25
25
|
/**
|
|
26
26
|
* Renders the root-level not-found component into a complete 404 HTML Response.
|
|
27
|
-
* Used by the Elysia `.
|
|
27
|
+
* Used by the Elysia `.error` catch-all when no route matches the request URL.
|
|
28
28
|
*/
|
|
29
29
|
export async function renderRootNotFound(
|
|
30
30
|
root: RootLayout,
|
|
@@ -87,7 +87,7 @@ export async function renderRootNotFound(
|
|
|
87
87
|
searchRoutes: [],
|
|
88
88
|
};
|
|
89
89
|
|
|
90
|
-
|
|
90
|
+
getLogger().set({
|
|
91
91
|
furin: {
|
|
92
92
|
action: "catch_all",
|
|
93
93
|
path: requestUrl?.pathname ?? "/",
|
|
@@ -117,7 +117,7 @@ export async function renderRootNotFound(
|
|
|
117
117
|
// The user's not-found component itself threw. Fall back to the built-in
|
|
118
118
|
// screen, but surface the failure — silently swallowing it hides a broken
|
|
119
119
|
// 404 page from logs and drains.
|
|
120
|
-
|
|
120
|
+
getLogger().set({
|
|
121
121
|
furin: {
|
|
122
122
|
action: "component_render_failed",
|
|
123
123
|
error: renderError instanceof Error ? renderError.message : String(renderError),
|
|
@@ -5,7 +5,7 @@ import { computeErrorDigest } from "../../shared/digest.ts";
|
|
|
5
5
|
import { physicalPath } from "../../shared/prefix.ts";
|
|
6
6
|
import { parseRouteFrameLines, serializeRouteFrames } from "../../shared/route-frame.ts";
|
|
7
7
|
import type { SearchRouteMetadata } from "../../shared/search-params.ts";
|
|
8
|
-
import {
|
|
8
|
+
import { getLogger } from "../context-logger.ts";
|
|
9
9
|
import { currentInstance } from "../instance.ts";
|
|
10
10
|
import type { ResolvedRoute, RootLayout } from "../router/types.ts";
|
|
11
11
|
import { resolvePath } from "./assemble.ts";
|
|
@@ -196,7 +196,7 @@ export async function resumePprDocument(
|
|
|
196
196
|
if (artifact.state.postponed !== null) {
|
|
197
197
|
const stream = await resume(tree, structuredClone(artifact.state.postponed), {
|
|
198
198
|
onError: (error) => {
|
|
199
|
-
|
|
199
|
+
getLogger().error(error instanceof Error ? error : new Error(String(error)));
|
|
200
200
|
return computeErrorDigest(error);
|
|
201
201
|
},
|
|
202
202
|
signal: ctx.request.signal,
|
|
@@ -3,10 +3,13 @@ import { physicalPath } from "../../shared/prefix.ts";
|
|
|
3
3
|
import type { SearchRouteMetadata } from "../../shared/search-params.ts";
|
|
4
4
|
import { autoInvalidateRegistry, getAutoInvalidateRegistry } from "../auto-invalidate/registry.ts";
|
|
5
5
|
import { pendingISRRevalidations } from "../cache/isr.ts";
|
|
6
|
+
import type { PageCacheAdapter, PageCacheIdentity, PageCacheLease } from "../cache/page-cache.ts";
|
|
7
|
+
import { waitForPageCacheEntry } from "../cache/page-cache.ts";
|
|
8
|
+
import { getPageCacheAdapter } from "../cache/page-cache-state.ts";
|
|
6
9
|
import { registerCacheInvalidator } from "../cache/registry.ts";
|
|
7
10
|
import { type Cache, createRouteCache, type RevalidateType } from "../cache/route-cache.ts";
|
|
8
11
|
import { getCache, hasExternalRuntimeCache } from "../cache/runtime-cache.ts";
|
|
9
|
-
import {
|
|
12
|
+
import { getLogger } from "../context-logger.ts";
|
|
10
13
|
import { isExternalPrerenderRequest } from "../external-prerender.ts";
|
|
11
14
|
import { allStateBuckets, currentInstance, type FurinInstance } from "../instance.ts";
|
|
12
15
|
import { resolveRouteRevalidate } from "../router/patterns.ts";
|
|
@@ -107,7 +110,7 @@ async function readPprArtifact(key: string, buildId: string): Promise<PprArtifac
|
|
|
107
110
|
await pprPublicResult(artifact.state);
|
|
108
111
|
return artifact;
|
|
109
112
|
} catch {
|
|
110
|
-
|
|
113
|
+
getLogger().warn("PPR runtime cache read failed; rendering fresh public data");
|
|
111
114
|
}
|
|
112
115
|
}
|
|
113
116
|
|
|
@@ -150,10 +153,181 @@ async function writePprArtifact(
|
|
|
150
153
|
ttl,
|
|
151
154
|
});
|
|
152
155
|
} catch {
|
|
153
|
-
|
|
156
|
+
getLogger().warn("PPR runtime cache write failed; serving the fresh result");
|
|
154
157
|
}
|
|
155
158
|
}
|
|
156
159
|
|
|
160
|
+
async function parseSharedPprArtifact(
|
|
161
|
+
payload: string,
|
|
162
|
+
buildId: string
|
|
163
|
+
): Promise<PprArtifact | undefined> {
|
|
164
|
+
const artifact: unknown = JSON.parse(payload);
|
|
165
|
+
if (!isPprArtifact(artifact) || artifact.state.buildId !== buildId) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
await pprPublicResult(artifact.state);
|
|
169
|
+
return artifact;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
interface SharedPprInput {
|
|
173
|
+
buildId: string;
|
|
174
|
+
cacheKey: string;
|
|
175
|
+
ctx: Context;
|
|
176
|
+
pageCache: PageCacheAdapter;
|
|
177
|
+
resolvedPath: string;
|
|
178
|
+
root: RootLayout;
|
|
179
|
+
route: ResolvedRoute;
|
|
180
|
+
searchRoutes: SearchRouteMetadata[] | undefined;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async function renderSharedPpr(
|
|
184
|
+
input: SharedPprInput,
|
|
185
|
+
identity: PageCacheIdentity,
|
|
186
|
+
lease: PageCacheLease | null,
|
|
187
|
+
revalidate: number
|
|
188
|
+
): Promise<PprResult> {
|
|
189
|
+
try {
|
|
190
|
+
const result = await prerenderPprDocument(
|
|
191
|
+
input.route,
|
|
192
|
+
input.ctx,
|
|
193
|
+
input.root,
|
|
194
|
+
input.buildId,
|
|
195
|
+
input.searchRoutes,
|
|
196
|
+
undefined
|
|
197
|
+
);
|
|
198
|
+
if (lease === null || !isPprArtifact(result)) {
|
|
199
|
+
return result;
|
|
200
|
+
}
|
|
201
|
+
try {
|
|
202
|
+
await input.pageCache.commit({
|
|
203
|
+
entry: {
|
|
204
|
+
cachedAt: result.cachedAt,
|
|
205
|
+
payload: JSON.stringify(result),
|
|
206
|
+
revalidate: input.route.mode === "isr" ? revalidate : null,
|
|
207
|
+
},
|
|
208
|
+
identity,
|
|
209
|
+
lease,
|
|
210
|
+
});
|
|
211
|
+
} catch {
|
|
212
|
+
getLogger().warn("PPR shared page cache write failed; serving fresh public data");
|
|
213
|
+
}
|
|
214
|
+
return result;
|
|
215
|
+
} finally {
|
|
216
|
+
if (lease !== null) {
|
|
217
|
+
try {
|
|
218
|
+
await input.pageCache.release({ identity, lease });
|
|
219
|
+
} catch {
|
|
220
|
+
getLogger().warn("PPR shared page cache lease release failed");
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function revalidateSharedPpr(
|
|
227
|
+
input: SharedPprInput,
|
|
228
|
+
identity: PageCacheIdentity,
|
|
229
|
+
lease: PageCacheLease,
|
|
230
|
+
revalidate: number
|
|
231
|
+
): void {
|
|
232
|
+
const pending = pendingISRRevalidations();
|
|
233
|
+
const pendingKey = `ppr-shared:${input.cacheKey}`;
|
|
234
|
+
const refresh = renderSharedPpr(input, identity, lease, revalidate)
|
|
235
|
+
.then(() => undefined)
|
|
236
|
+
.catch(() => {
|
|
237
|
+
getLogger().warn("PPR shared page cache background regeneration failed");
|
|
238
|
+
})
|
|
239
|
+
.finally(() => pending.delete(pendingKey));
|
|
240
|
+
pending.set(pendingKey, refresh);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
interface SharedPprLookup {
|
|
244
|
+
artifact: PprArtifact | undefined;
|
|
245
|
+
available: boolean;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
async function lookupSharedPpr(
|
|
249
|
+
input: SharedPprInput,
|
|
250
|
+
identity: PageCacheIdentity
|
|
251
|
+
): Promise<SharedPprLookup> {
|
|
252
|
+
try {
|
|
253
|
+
const cached = await input.pageCache.read(identity);
|
|
254
|
+
return {
|
|
255
|
+
artifact:
|
|
256
|
+
cached === null ? undefined : await parseSharedPprArtifact(cached.payload, input.buildId),
|
|
257
|
+
available: true,
|
|
258
|
+
};
|
|
259
|
+
} catch {
|
|
260
|
+
getLogger().warn("PPR shared page cache read failed; rendering fresh public data");
|
|
261
|
+
return { artifact: undefined, available: false };
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function renderFreshPpr(input: SharedPprInput): Promise<PprResult> {
|
|
266
|
+
return prerenderPprDocument(
|
|
267
|
+
input.route,
|
|
268
|
+
input.ctx,
|
|
269
|
+
input.root,
|
|
270
|
+
input.buildId,
|
|
271
|
+
input.searchRoutes,
|
|
272
|
+
undefined
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
async function getSharedPprArtifact(input: SharedPprInput): Promise<PprResult> {
|
|
277
|
+
const { prefix } = currentInstance();
|
|
278
|
+
const identity: PageCacheIdentity = {
|
|
279
|
+
buildId: input.buildId,
|
|
280
|
+
key: input.cacheKey,
|
|
281
|
+
mode: "ppr",
|
|
282
|
+
path: input.resolvedPath,
|
|
283
|
+
scope: prefix,
|
|
284
|
+
tags: input.route.tags ?? [],
|
|
285
|
+
};
|
|
286
|
+
const revalidate = resolveRouteRevalidate(input.route.page) ?? 60;
|
|
287
|
+
const lookup = await lookupSharedPpr(input, identity);
|
|
288
|
+
if (!lookup.available) {
|
|
289
|
+
return renderFreshPpr(input);
|
|
290
|
+
}
|
|
291
|
+
const cachedArtifact = lookup.artifact;
|
|
292
|
+
if (
|
|
293
|
+
cachedArtifact !== undefined &&
|
|
294
|
+
(input.route.mode !== "isr" || Date.now() - cachedArtifact.cachedAt < revalidate * 1000)
|
|
295
|
+
) {
|
|
296
|
+
return cachedArtifact;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
let lease: PageCacheLease | null;
|
|
300
|
+
try {
|
|
301
|
+
lease = await input.pageCache.acquire({ identity, leaseMs: 30_000 });
|
|
302
|
+
} catch {
|
|
303
|
+
getLogger().warn("PPR shared page cache lease failed");
|
|
304
|
+
if (cachedArtifact !== undefined) {
|
|
305
|
+
return cachedArtifact;
|
|
306
|
+
}
|
|
307
|
+
return renderFreshPpr(input);
|
|
308
|
+
}
|
|
309
|
+
if (cachedArtifact !== undefined) {
|
|
310
|
+
if (lease !== null) {
|
|
311
|
+
revalidateSharedPpr(input, identity, lease, revalidate);
|
|
312
|
+
}
|
|
313
|
+
return cachedArtifact;
|
|
314
|
+
}
|
|
315
|
+
if (lease === null) {
|
|
316
|
+
try {
|
|
317
|
+
const concurrent = await waitForPageCacheEntry(input.pageCache, identity, 2000);
|
|
318
|
+
if (concurrent !== null) {
|
|
319
|
+
const artifact = await parseSharedPprArtifact(concurrent.payload, input.buildId);
|
|
320
|
+
if (artifact !== undefined) {
|
|
321
|
+
return artifact;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
} catch {
|
|
325
|
+
getLogger().warn("PPR shared page cache wait failed; rendering fresh public data");
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
return renderSharedPpr(input, identity, lease, revalidate);
|
|
329
|
+
}
|
|
330
|
+
|
|
157
331
|
async function getPprArtifact(
|
|
158
332
|
route: ResolvedRoute,
|
|
159
333
|
ctx: Context,
|
|
@@ -164,12 +338,24 @@ async function getPprArtifact(
|
|
|
164
338
|
const requestUrl = new URL(ctx.request.url);
|
|
165
339
|
const resolvedPath = resolvePath(route.pattern, ctx.params ?? {});
|
|
166
340
|
const cacheKey = `${route.mode}:${resolvedPath}${requestUrl.search}`;
|
|
341
|
+
const externalPrerender = isExternalPrerenderRequest(ctx.request);
|
|
342
|
+
const pageCache = externalPrerender ? undefined : getPageCacheAdapter();
|
|
343
|
+
if (pageCache !== undefined) {
|
|
344
|
+
return getSharedPprArtifact({
|
|
345
|
+
buildId,
|
|
346
|
+
cacheKey,
|
|
347
|
+
ctx,
|
|
348
|
+
pageCache,
|
|
349
|
+
resolvedPath,
|
|
350
|
+
root,
|
|
351
|
+
route,
|
|
352
|
+
searchRoutes,
|
|
353
|
+
});
|
|
354
|
+
}
|
|
167
355
|
if (hasExternalRuntimeCache()) {
|
|
168
356
|
const { prefix } = currentInstance();
|
|
169
357
|
const key = JSON.stringify([process.env.VERCEL_DEPLOYMENT_ID, buildId, prefix, cacheKey]);
|
|
170
|
-
const stored =
|
|
171
|
-
? undefined
|
|
172
|
-
: await readPprArtifact(key, buildId);
|
|
358
|
+
const stored = externalPrerender ? undefined : await readPprArtifact(key, buildId);
|
|
173
359
|
if (stored !== undefined) {
|
|
174
360
|
return stored;
|
|
175
361
|
}
|
|
@@ -183,7 +369,7 @@ async function getPprArtifact(
|
|
|
183
369
|
return result;
|
|
184
370
|
}
|
|
185
371
|
const pprRoutes = getPprRoutes();
|
|
186
|
-
const cached = pprRoutes.get(cacheKey);
|
|
372
|
+
const cached = externalPrerender ? undefined : pprRoutes.get(cacheKey);
|
|
187
373
|
if (cached === undefined || cached.artifact.state.buildId !== buildId) {
|
|
188
374
|
const result = await prerenderPprDocument(route, ctx, root, buildId, searchRoutes, undefined);
|
|
189
375
|
if (!isPprArtifact(result)) {
|
package/src/server/render/ssg.ts
CHANGED
|
@@ -3,11 +3,16 @@ import type { Context } from "elysia";
|
|
|
3
3
|
import type { SearchRouteMetadata } from "../../shared/search-params.ts";
|
|
4
4
|
import { mapWithConcurrency } from "../../shared/utils/index.ts";
|
|
5
5
|
import type { SsgCacheEntry } from "../cache/isr-ssg.ts";
|
|
6
|
+
import type { PageCacheAdapter, PageCacheIdentity, PageCacheLease } from "../cache/page-cache.ts";
|
|
7
|
+
import { waitForPageCacheEntry } from "../cache/page-cache.ts";
|
|
8
|
+
import { getPageCacheAdapter } from "../cache/page-cache-state.ts";
|
|
6
9
|
import { getSSGCache, setSSGCache } from "../cache/ssg.ts";
|
|
7
|
-
import { createLogger } from "../context-logger.ts";
|
|
10
|
+
import { createLogger, getLogger } from "../context-logger.ts";
|
|
11
|
+
import { currentInstance } from "../instance.ts";
|
|
8
12
|
import type { ResolvedRoute, RootLayout } from "../router/types.ts";
|
|
9
13
|
import { resolvePath } from "./assemble.ts";
|
|
10
14
|
import { renderForPath } from "./ssr.ts";
|
|
15
|
+
import { hasStaticParams, resolveStaticParams } from "./static-params.ts";
|
|
11
16
|
|
|
12
17
|
export async function prerenderRoute(
|
|
13
18
|
route: ResolvedRoute,
|
|
@@ -72,6 +77,138 @@ export async function prerenderSSG(
|
|
|
72
77
|
return entry;
|
|
73
78
|
}
|
|
74
79
|
|
|
80
|
+
export interface RuntimeSsgResult {
|
|
81
|
+
cacheStored: boolean;
|
|
82
|
+
entry: SsgCacheEntry | Response;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function parseSharedSsgEntry(payload: string): SsgCacheEntry | undefined {
|
|
86
|
+
const value: unknown = JSON.parse(payload);
|
|
87
|
+
if (
|
|
88
|
+
typeof value !== "object" ||
|
|
89
|
+
value === null ||
|
|
90
|
+
!("cachedAt" in value) ||
|
|
91
|
+
typeof value.cachedAt !== "number" ||
|
|
92
|
+
!("html" in value) ||
|
|
93
|
+
typeof value.html !== "string" ||
|
|
94
|
+
!("ndjson" in value) ||
|
|
95
|
+
typeof value.ndjson !== "string" ||
|
|
96
|
+
!("status" in value) ||
|
|
97
|
+
typeof value.status !== "number" ||
|
|
98
|
+
("tags" in value &&
|
|
99
|
+
value.tags !== undefined &&
|
|
100
|
+
(!Array.isArray(value.tags) || value.tags.some((tag) => typeof tag !== "string")))
|
|
101
|
+
) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
return value as SsgCacheEntry;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
interface SharedSsgInput {
|
|
108
|
+
identity: PageCacheIdentity;
|
|
109
|
+
pageCache: PageCacheAdapter;
|
|
110
|
+
renderFresh: () => Promise<SsgCacheEntry | Response>;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async function renderAndStoreSharedSsg(
|
|
114
|
+
input: SharedSsgInput,
|
|
115
|
+
lease: PageCacheLease | null
|
|
116
|
+
): Promise<RuntimeSsgResult> {
|
|
117
|
+
try {
|
|
118
|
+
const entry = await input.renderFresh();
|
|
119
|
+
if (entry instanceof Response || lease === null) {
|
|
120
|
+
return { cacheStored: false, entry };
|
|
121
|
+
}
|
|
122
|
+
let cacheStored = false;
|
|
123
|
+
try {
|
|
124
|
+
cacheStored =
|
|
125
|
+
(await input.pageCache.commit({
|
|
126
|
+
entry: { cachedAt: entry.cachedAt, payload: JSON.stringify(entry), revalidate: null },
|
|
127
|
+
identity: input.identity,
|
|
128
|
+
lease,
|
|
129
|
+
})) === "stored";
|
|
130
|
+
} catch {
|
|
131
|
+
getLogger().warn("SSG shared page cache write failed; serving fresh with no-store");
|
|
132
|
+
}
|
|
133
|
+
return { cacheStored, entry };
|
|
134
|
+
} finally {
|
|
135
|
+
if (lease !== null) {
|
|
136
|
+
try {
|
|
137
|
+
await input.pageCache.release({ identity: input.identity, lease });
|
|
138
|
+
} catch {
|
|
139
|
+
getLogger().warn("SSG shared page cache lease release failed");
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async function resolveSharedSsg(input: SharedSsgInput): Promise<RuntimeSsgResult> {
|
|
146
|
+
try {
|
|
147
|
+
const cached = await input.pageCache.read(input.identity);
|
|
148
|
+
if (cached !== null) {
|
|
149
|
+
const entry = parseSharedSsgEntry(cached.payload);
|
|
150
|
+
if (entry !== undefined) {
|
|
151
|
+
return { cacheStored: true, entry };
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
} catch {
|
|
155
|
+
getLogger().warn("SSG shared page cache read failed; rendering fresh with no-store");
|
|
156
|
+
return { cacheStored: false, entry: await input.renderFresh() };
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
let lease: PageCacheLease | null;
|
|
160
|
+
try {
|
|
161
|
+
lease = await input.pageCache.acquire({ identity: input.identity, leaseMs: 30_000 });
|
|
162
|
+
} catch {
|
|
163
|
+
getLogger().warn("SSG shared page cache lease failed; rendering fresh with no-store");
|
|
164
|
+
return { cacheStored: false, entry: await input.renderFresh() };
|
|
165
|
+
}
|
|
166
|
+
if (lease === null) {
|
|
167
|
+
try {
|
|
168
|
+
const concurrent = await waitForPageCacheEntry(input.pageCache, input.identity, 2000);
|
|
169
|
+
if (concurrent !== null) {
|
|
170
|
+
const entry = parseSharedSsgEntry(concurrent.payload);
|
|
171
|
+
if (entry !== undefined) {
|
|
172
|
+
return { cacheStored: true, entry };
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
} catch {
|
|
176
|
+
getLogger().warn("SSG shared page cache wait failed; rendering fresh with no-store");
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return renderAndStoreSharedSsg(input, lease);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export async function prerenderRuntimeSSG(
|
|
183
|
+
route: ResolvedRoute,
|
|
184
|
+
params: Record<string, string>,
|
|
185
|
+
root: RootLayout,
|
|
186
|
+
origin: string,
|
|
187
|
+
buildId: string,
|
|
188
|
+
searchRoutes: SearchRouteMetadata[] | undefined
|
|
189
|
+
): Promise<RuntimeSsgResult> {
|
|
190
|
+
const pageCache = getPageCacheAdapter();
|
|
191
|
+
if (pageCache === undefined) {
|
|
192
|
+
return {
|
|
193
|
+
cacheStored: true,
|
|
194
|
+
entry: await prerenderSSG(route, params, root, origin, undefined, searchRoutes),
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const resolvedPath = resolvePath(route.pattern, params);
|
|
199
|
+
const identity: PageCacheIdentity = {
|
|
200
|
+
buildId,
|
|
201
|
+
key: resolvedPath,
|
|
202
|
+
mode: "ssg",
|
|
203
|
+
path: resolvedPath,
|
|
204
|
+
scope: currentInstance().prefix,
|
|
205
|
+
tags: route.tags ?? [],
|
|
206
|
+
};
|
|
207
|
+
const renderFresh = () =>
|
|
208
|
+
prerenderRoute(route, params, root, origin, "ssg", undefined, searchRoutes);
|
|
209
|
+
return resolveSharedSsg({ identity, pageCache, renderFresh });
|
|
210
|
+
}
|
|
211
|
+
|
|
75
212
|
/**
|
|
76
213
|
* Emits a single structured error log for an SSG warm-up/prerender failure.
|
|
77
214
|
* Centralises the repeated `createLogger → set → error → emit` dance so every
|
|
@@ -97,7 +234,7 @@ export async function warmSSGCache(
|
|
|
97
234
|
origin: string,
|
|
98
235
|
searchRoutes?: SearchRouteMetadata[]
|
|
99
236
|
): Promise<void> {
|
|
100
|
-
const targets = routes.filter((r) => r.mode === "ssg" && r
|
|
237
|
+
const targets = routes.filter((r) => r.mode === "ssg" && hasStaticParams(r));
|
|
101
238
|
const warmupLogger = createLogger({});
|
|
102
239
|
warmupLogger.set({
|
|
103
240
|
furin: {
|
|
@@ -117,7 +254,7 @@ export async function warmSSGCache(
|
|
|
117
254
|
SSG_WARM_CONCURRENCY,
|
|
118
255
|
async (route) => {
|
|
119
256
|
try {
|
|
120
|
-
const paramSets = (await route
|
|
257
|
+
const paramSets = (await resolveStaticParams(route, origin)) ?? [];
|
|
121
258
|
return { paramSets, route };
|
|
122
259
|
} catch (err) {
|
|
123
260
|
return { error: err, route };
|
|
@@ -145,7 +282,14 @@ export async function warmSSGCache(
|
|
|
145
282
|
for (const params of paramSets) {
|
|
146
283
|
tasks.push(async () => {
|
|
147
284
|
try {
|
|
148
|
-
await
|
|
285
|
+
await prerenderRuntimeSSG(
|
|
286
|
+
route,
|
|
287
|
+
params,
|
|
288
|
+
root,
|
|
289
|
+
origin,
|
|
290
|
+
currentInstance().buildId,
|
|
291
|
+
searchRoutes
|
|
292
|
+
);
|
|
149
293
|
} catch (err) {
|
|
150
294
|
logSsgError({ action: "prerender_failed", render: "ssg", route: route.pattern }, err);
|
|
151
295
|
}
|
package/src/server/render/ssr.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
// biome-ignore-all lint/correctness/useHookAtTopLevel: useLogger is not a hook attached to a react component
|
|
2
|
-
|
|
3
1
|
import type { Context } from "elysia";
|
|
4
2
|
import { createElement, type ReactNode } from "react";
|
|
5
3
|
import { renderToReadableStream } from "react-dom/server";
|
|
@@ -16,9 +14,10 @@ import type { RouterContextValue } from "../../client/router/types.ts";
|
|
|
16
14
|
import type { HeadOptions } from "../../client.ts";
|
|
17
15
|
import { serializeCompactJsonLine } from "../../shared/compact-json.ts";
|
|
18
16
|
import { computeErrorDigest } from "../../shared/digest.ts";
|
|
17
|
+
import { isProductionBuild } from "../../shared/production-build.ts";
|
|
19
18
|
import { containsRscSource, serializeRouteFrames } from "../../shared/route-frame.ts";
|
|
20
19
|
import type { SearchParamsInput, SearchRouteMetadata } from "../../shared/search-params.ts";
|
|
21
|
-
import {
|
|
20
|
+
import { getLogger, runInSyntheticRenderScope } from "../context-logger.ts";
|
|
22
21
|
import { currentInstance } from "../instance.ts";
|
|
23
22
|
// FurinNotFoundError is used indirectly via buildNotFoundElement in element.tsx
|
|
24
23
|
import type { ResolvedRoute, RootLayout } from "../router/types.ts";
|
|
@@ -558,7 +557,7 @@ export function renderForPath(
|
|
|
558
557
|
return prepared;
|
|
559
558
|
}
|
|
560
559
|
|
|
561
|
-
|
|
560
|
+
getLogger().set({
|
|
562
561
|
furin: {
|
|
563
562
|
cache: mode === "isr" ? "revalidated" : "miss",
|
|
564
563
|
loader_ms: prepared.loader_ms,
|
|
@@ -852,7 +851,7 @@ export async function renderSSR(
|
|
|
852
851
|
return prepared;
|
|
853
852
|
}
|
|
854
853
|
|
|
855
|
-
|
|
854
|
+
getLogger().set({
|
|
856
855
|
furin: {
|
|
857
856
|
loader_ms: prepared.loader_ms,
|
|
858
857
|
render: route.mode,
|
|
@@ -900,7 +899,7 @@ export async function renderSSR(
|
|
|
900
899
|
status = 500;
|
|
901
900
|
finalDigest = shellError.digest;
|
|
902
901
|
finalMessage = shellError.message;
|
|
903
|
-
|
|
902
|
+
getLogger().set({
|
|
904
903
|
furin: { digest: finalDigest, phase: "shell", render: route.mode, route: route.pattern },
|
|
905
904
|
});
|
|
906
905
|
}
|
|
@@ -917,7 +916,7 @@ export async function renderSSR(
|
|
|
917
916
|
}
|
|
918
917
|
}
|
|
919
918
|
if (
|
|
920
|
-
|
|
919
|
+
!isProductionBuild() &&
|
|
921
920
|
dataPayload.__furinError !== undefined &&
|
|
922
921
|
dataPayload.__furinNotFound !== undefined
|
|
923
922
|
) {
|