prunelle 0.1.1 → 0.7.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/LICENSE +21 -0
- package/llms.txt +137 -40
- package/package.json +11 -2
- package/src/a11y/declaration.ts +373 -0
- package/src/a11y/index.ts +477 -0
- package/src/a11y/rgaa-map.ts +230 -0
- package/src/attrs.ts +11 -3
- package/src/build/bundle.ts +89 -41
- package/src/build/byte-size.ts +11 -0
- package/src/build/client-entries.ts +3 -2
- package/src/build/client-plugin.ts +11 -5
- package/src/build/client-prune.ts +56 -6
- package/src/build/compile.ts +13 -10
- package/src/build/ecoindex.ts +238 -0
- package/src/build/files.ts +15 -0
- package/src/build/first-load.ts +85 -14
- package/src/build/graph.ts +29 -39
- package/src/build/index.ts +225 -122
- package/src/build/island-promote.ts +311 -0
- package/src/build/islands.ts +14 -203
- package/src/build/manifest.ts +56 -116
- package/src/build/output.ts +3 -6
- package/src/build/plugin-host.ts +115 -41
- package/src/build/precompress.ts +16 -3
- package/src/build/prerender.ts +177 -39
- package/src/build/reach.ts +200 -0
- package/src/build/render-rules.ts +2 -2
- package/src/build/report.ts +48 -5
- package/src/build/resolve.ts +8 -6
- package/src/build/routes.ts +175 -10
- package/src/build/static-headers.ts +112 -0
- package/src/build/static.ts +139 -39
- package/src/build/status-pages.ts +127 -0
- package/src/build/style-bundles.ts +526 -0
- package/src/cli/check-forms.ts +636 -0
- package/src/cli/check-imports.ts +31 -0
- package/src/cli/check-links.ts +632 -0
- package/src/cli/check-security.ts +64 -0
- package/src/cli/check.ts +101 -26
- package/src/cli/commands.ts +120 -11
- package/src/cli/docs.ts +403 -0
- package/src/cli/inspect.ts +105 -15
- package/src/cli/mcp-tools.ts +492 -0
- package/src/cli/mcp.ts +205 -0
- package/src/cli/new.ts +16 -5
- package/src/cli/render.ts +416 -0
- package/src/cli/upgrade-names.ts +73 -13
- package/src/cli/upgrade.ts +156 -22
- package/src/compiler/analyze.ts +87 -54
- package/src/compiler/browser-globals.ts +140 -17
- package/src/compiler/cacheability-private.ts +566 -0
- package/src/compiler/cacheability.ts +335 -0
- package/src/compiler/compliance-eco.ts +71 -0
- package/src/compiler/compliance-rgaa.ts +614 -0
- package/src/compiler/compliance-urls.ts +148 -0
- package/src/compiler/compliance.ts +407 -352
- package/src/compiler/data-keys.ts +199 -192
- package/src/compiler/dev-source.ts +87 -0
- package/src/compiler/dom-attrs.ts +138 -65
- package/src/compiler/dom.ts +517 -442
- package/src/compiler/expr-context.ts +260 -81
- package/src/compiler/foreign-imports.ts +88 -0
- package/src/compiler/mistakes.ts +23 -2
- package/src/compiler/props-body.ts +60 -0
- package/src/compiler/props.ts +87 -36
- package/src/compiler/scope.ts +400 -333
- package/src/compiler/server-only.ts +190 -151
- package/src/compiler/shared.ts +42 -14
- package/src/compiler/signal-reads.ts +6 -2
- package/src/compiler/spread-handlers.ts +295 -0
- package/src/compiler/ssr-prunelle-tags.ts +27 -14
- package/src/compiler/ssr.ts +220 -89
- package/src/context.ts +34 -0
- package/src/dev/error-log.ts +177 -0
- package/src/dev/host-check.ts +32 -0
- package/src/dev/index.ts +94 -34
- package/src/dev/open-editor.ts +228 -0
- package/src/dev/protocol.ts +3 -0
- package/src/dev/reload.ts +5 -3
- package/src/dev/supervisor.ts +50 -25
- package/src/dev/watchers.ts +74 -13
- package/src/dom/action.ts +47 -21
- package/src/dom/attrs.ts +15 -3
- package/src/dom/extras.ts +57 -40
- package/src/dom/index.ts +54 -14
- package/src/dom/island.ts +102 -68
- package/src/dom/loop.ts +66 -32
- package/src/eco/declaration.ts +328 -0
- package/src/eco/index.ts +69 -0
- package/src/index.ts +2 -2
- package/src/jsx-runtime.ts +2 -2
- package/src/plugins/config.ts +18 -12
- package/src/plugins/context.ts +44 -1
- package/src/plugins/index.ts +2 -2
- package/src/plugins/load.ts +30 -10
- package/src/plugins/order.ts +38 -1
- package/src/plugins/public.ts +6 -3
- package/src/plugins/services.ts +84 -0
- package/src/plugins/types.ts +51 -2
- package/src/reactivity/graph.ts +10 -0
- package/src/reactivity/index.ts +24 -16
- package/src/reactivity/selector.ts +31 -4
- package/src/reactivity/store.ts +64 -20
- package/src/server/action-flash.ts +158 -0
- package/src/server/action-protect.ts +199 -0
- package/src/server/action-registry.ts +1 -1
- package/src/server/action.ts +188 -152
- package/src/server/app-secret.ts +63 -0
- package/src/server/assets.ts +34 -2
- package/src/server/compress.ts +96 -10
- package/src/server/consent.ts +59 -22
- package/src/server/cookies.ts +16 -2
- package/src/server/document.ts +25 -10
- package/src/server/env.ts +14 -0
- package/src/server/errors.ts +308 -65
- package/src/server/form-schema.ts +337 -0
- package/src/server/health.ts +275 -0
- package/src/server/http.ts +186 -39
- package/src/server/index.ts +272 -273
- package/src/server/loader.ts +15 -2
- package/src/server/page.ts +15 -0
- package/src/server/param-matchers.ts +112 -0
- package/src/server/plugin-host.ts +125 -88
- package/src/server/public.ts +6 -5
- package/src/server/rate-limit.ts +102 -36
- package/src/server/request-body.ts +1 -1
- package/src/server/route-cache.ts +541 -0
- package/src/server/route-table.ts +356 -11
- package/src/server/router.ts +125 -40
- package/src/server/routing.ts +112 -63
- package/src/server/sanitize-html.ts +309 -0
- package/src/server/security.ts +279 -52
- package/src/server/serve-handlers.ts +31 -7
- package/src/server/standard-schema.ts +17 -0
- package/src/server/start.ts +60 -16
- package/src/server/static-dir.ts +91 -12
- package/src/server/stream.ts +68 -27
- package/src/ssr/consent-banner.ts +58 -0
- package/src/ssr/context.ts +106 -8
- package/src/ssr/index.ts +151 -4
- package/src/ssr/navigation.ts +2 -2
- package/src/ssr/region.ts +40 -19
- package/src/style.ts +64 -1
- package/src/test/browser.ts +28 -13
- package/src/test/index.ts +97 -12
- package/src/ts-plugin/diagnostics.ts +505 -0
- package/src/ts-plugin/fixes.ts +364 -0
- package/src/ts-plugin/index.ts +194 -0
- package/templates/_shared/AGENTS.md +2 -2
- package/templates/_shared/gitignore +5 -0
- package/templates/_shared/mcp.json +11 -0
- package/templates/_shared/src/routes/legal.tsx +15 -1
- package/templates/_shared/tsconfig.json +5 -0
- package/templates/app/.env.example +4 -0
- package/templates/app/package.json +2 -1
- package/templates/app/prunelle.config.ts +2 -0
- package/templates/app/src/auth.ts +4 -6
- package/templates/app/src/mail.ts +4 -0
- package/templates/app/src/routes/_layout.tsx +9 -1
- package/templates/app/src/routes/dashboard/index.tsx +42 -8
- package/templates/app/src/routes/login.tsx +49 -30
- package/templates/app/src/routes/privacy.tsx +25 -0
- package/templates/app/src/styles/app.css +8 -1
- package/templates/app/tests/app.test.ts +44 -3
- package/templates/blog/package.json +1 -1
- package/templates/blog/prunelle.config.ts +1 -1
- package/templates/blog/src/routes/_layout.tsx +6 -1
- package/templates/blog/src/routes/index.tsx +5 -1
- package/templates/blog/src/styles/app.css +6 -1
- package/templates/fullstack/package.json +1 -1
- package/templates/fullstack/src/components/GreetForm.tsx +2 -1
- package/templates/fullstack/src/routes/_layout.tsx +4 -0
- package/templates/fullstack/src/styles/app.css +11 -1
- package/templates/minimal/package.json +1 -1
- package/templates/minimal/src/routes/_layout.tsx +4 -0
- package/templates/minimal/src/styles/app.css +11 -1
- package/ts-plugin.cjs +4277 -0
|
@@ -1,15 +1,52 @@
|
|
|
1
1
|
// The page routes of a build, compiled once at startup: each route's modules, middlewares and document options, and the handler of a page nothing can intercept.
|
|
2
2
|
|
|
3
3
|
import type { BuildManifest, RouteManifestEntry } from '../build/index'
|
|
4
|
+
import type { Html, HydrateStrategy } from '../jsx-runtime'
|
|
5
|
+
import type { AppRuntimeContext } from '../plugins/index'
|
|
4
6
|
import type { DocumentOptions } from '../ssr/index'
|
|
5
|
-
import type {
|
|
7
|
+
import type { ActionRegistry } from './action-registry'
|
|
8
|
+
import type { ServedFile } from './assets'
|
|
9
|
+
import type { CompressionCache } from './compress'
|
|
10
|
+
import type { NormalizedConsentConfig } from './consent'
|
|
11
|
+
import type { CookieStore } from './cookies'
|
|
12
|
+
import type { DocumentHooks, HtmlHeaders } from './document'
|
|
13
|
+
import type { NotifyError } from './errors'
|
|
14
|
+
import type { DevTiming, RequestIdResolver } from './health'
|
|
15
|
+
import type { Middleware, RequestContext } from './http'
|
|
6
16
|
import type { LoadedLayoutModule, LoadedMiddlewareModule, LoadedPageModule } from './page'
|
|
17
|
+
import type { ParamMatcherMap } from './param-matchers'
|
|
7
18
|
import type { RequestKind } from './plugin-host'
|
|
19
|
+
import type { PrivateReadTracker, RouteCache, RouteCacheSingleflightResult, RouteCacheSpec } from './route-cache'
|
|
8
20
|
import type { FastHandler, FastStaticPageOptions } from './routing'
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
21
|
+
import type { TrustProxy } from './security'
|
|
22
|
+
import type { StreamTracker } from './stream'
|
|
23
|
+
import { runWithAppContext } from '../plugins/index'
|
|
24
|
+
import { createRenderContext, runWithRenderContext } from '../ssr/context'
|
|
25
|
+
import { raw, renderDocument, ssrChild, ssrIsland } from '../ssr/index'
|
|
26
|
+
import { ACTION_ERROR_COOKIE, hydrateActionErrorFlash } from './action'
|
|
27
|
+
import { compressedHtmlResponse, compressedHtmlStream, markCompressionHandled } from './compress'
|
|
28
|
+
import { addedInlineStyleHashes, headTagStyleHashes, mergeStyleHashes, resolveRenderedIslands, revivePreload } from './document'
|
|
29
|
+
import { endTimer, startTimer } from './health'
|
|
30
|
+
import { isNotFound, makeRequestContext, runChain } from './http'
|
|
31
|
+
import { allowedMethodsHeader, hasPublicFavicon, mergedHeadTags, pickPageIslandProps, stripHeadBody } from './page'
|
|
32
|
+
import { BUILTIN_PARAM_MATCHERS } from './param-matchers'
|
|
33
|
+
import { hasRegionHeader, invalidRegionResponse, requestedRegion, respondRegion, withRegionVary } from './region'
|
|
34
|
+
import { GATED_ROUTE_CACHE_CONTROL, isCacheableRequest, routeCacheKey, serveCachedHit, trackPrivateReads, withCacheControl } from './route-cache'
|
|
11
35
|
import { Router } from './router'
|
|
12
|
-
import { createFastStaticPageHandler } from './routing'
|
|
36
|
+
import { createFastStaticPageHandler, methodNotAllowed } from './routing'
|
|
37
|
+
import { styleHashesOf } from './security'
|
|
38
|
+
import { hasActionErrorCookie, isRead } from './serve-handlers'
|
|
39
|
+
import { STREAM_TAIL_MARK, streamDocument } from './stream'
|
|
40
|
+
|
|
41
|
+
export const HTTP_METHOD_EXPORTS: ReadonlySet<string> = new Set(['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'])
|
|
42
|
+
|
|
43
|
+
/** Returns the exported HTTP method handler for `method` only when `method` is a standard HTTP method export. */
|
|
44
|
+
export function routeMethodHandler(mod: LoadedPageModule, method: string): ((c: RequestContext) => Response | Promise<Response>) | undefined {
|
|
45
|
+
if (!HTTP_METHOD_EXPORTS.has(method))
|
|
46
|
+
return undefined
|
|
47
|
+
const handler = mod[method]
|
|
48
|
+
return typeof handler === 'function' ? handler as (c: RequestContext) => Response | Promise<Response> : undefined
|
|
49
|
+
}
|
|
13
50
|
|
|
14
51
|
export interface CompiledRoute {
|
|
15
52
|
entry: RouteManifestEntry
|
|
@@ -25,7 +62,7 @@ export interface CompiledRoute {
|
|
|
25
62
|
headStyleHashes: string[]
|
|
26
63
|
}
|
|
27
64
|
|
|
28
|
-
function routeDocument(manifest: BuildManifest, entry: RouteManifestEntry, lang: string, alwaysLoadRuntime: boolean, navigation: DocumentOptions['navigation']): DocumentOptions {
|
|
65
|
+
function routeDocument(manifest: BuildManifest, entry: RouteManifestEntry, lang: string, alwaysLoadRuntime: boolean, navigation: DocumentOptions['navigation'], hasFavicon: boolean): DocumentOptions {
|
|
29
66
|
return {
|
|
30
67
|
body: '',
|
|
31
68
|
headTags: mergedHeadTags(manifest, entry),
|
|
@@ -34,6 +71,7 @@ function routeDocument(manifest: BuildManifest, entry: RouteManifestEntry, lang:
|
|
|
34
71
|
chunkImports: manifest.chunkImports,
|
|
35
72
|
alwaysLoadRuntime,
|
|
36
73
|
navigation,
|
|
74
|
+
hasFavicon,
|
|
37
75
|
regionScript: entry.usesRegion === true ? manifest.regionScript : undefined,
|
|
38
76
|
styles: entry.styles ?? manifest.styles,
|
|
39
77
|
}
|
|
@@ -44,9 +82,11 @@ type SharedFastOptions = Omit<FastStaticPageOptions, 'entry' | 'mod' | 'layouts'
|
|
|
44
82
|
export interface RouteTableOptions {
|
|
45
83
|
manifest: BuildManifest
|
|
46
84
|
modules: Record<string, unknown>
|
|
85
|
+
paramMatchers?: ParamMatcherMap | undefined
|
|
47
86
|
lang: string
|
|
48
87
|
alwaysLoadRuntime: boolean
|
|
49
88
|
navigation: DocumentOptions['navigation']
|
|
89
|
+
hasFavicon?: boolean | undefined
|
|
50
90
|
middlewaresFor: (path: string, kind?: RequestKind) => readonly Middleware[]
|
|
51
91
|
/** Whether a plugin rewrite, head hook, HTML transform or route may change what `path` answers. */
|
|
52
92
|
pluginIntercepts: (path: string) => boolean
|
|
@@ -54,8 +94,9 @@ export interface RouteTableOptions {
|
|
|
54
94
|
}
|
|
55
95
|
|
|
56
96
|
export function compileRouteTable(options: RouteTableOptions): { router: Router<CompiledRoute>, fastStaticPages: Map<string, FastHandler> } {
|
|
57
|
-
const { manifest, modules, lang, alwaysLoadRuntime, navigation, middlewaresFor, pluginIntercepts, fast } = options
|
|
58
|
-
const
|
|
97
|
+
const { manifest, modules, paramMatchers, lang, alwaysLoadRuntime, navigation, middlewaresFor, pluginIntercepts, fast } = options
|
|
98
|
+
const hasFavicon = options.hasFavicon ?? hasPublicFavicon(manifest)
|
|
99
|
+
const router = new Router<CompiledRoute>(paramMatchers ?? BUILTIN_PARAM_MATCHERS)
|
|
59
100
|
const fastStaticPages = new Map<string, FastHandler>()
|
|
60
101
|
for (const entry of manifest.routes) {
|
|
61
102
|
const mod = modules[entry.file] as LoadedPageModule
|
|
@@ -63,17 +104,321 @@ export function compileRouteTable(options: RouteTableOptions): { router: Router<
|
|
|
63
104
|
const dirMiddlewares = entry.middlewares.map(m => (modules[m] as LoadedMiddlewareModule).default)
|
|
64
105
|
const hasLoad = Boolean(entry.hasLoad && typeof mod.load === 'function')
|
|
65
106
|
const dynamic = entry.path.includes(':') || entry.path.includes('*')
|
|
66
|
-
const doc = routeDocument(manifest, entry, lang, alwaysLoadRuntime, navigation)
|
|
107
|
+
const doc = routeDocument(manifest, entry, lang, alwaysLoadRuntime, navigation, hasFavicon)
|
|
67
108
|
|
|
68
|
-
// Zero-overhead path for a static route with no middleware, no <Head>/<Suspense>, and no intercepting plugin hook
|
|
109
|
+
// Zero-overhead path for a static route with no middleware, no <Head>/<Suspense>, no route cache, and no intercepting plugin hook
|
|
69
110
|
const canFastPath = middlewaresFor(entry.path, hasLoad ? 'dynamic' : 'static').length === 0
|
|
70
111
|
&& !pluginIntercepts(entry.path) && !dynamic
|
|
71
|
-
&& dirMiddlewares.length === 0 && !entry.usesRenderContext
|
|
112
|
+
&& dirMiddlewares.length === 0 && !entry.usesRenderContext && entry.cache === undefined
|
|
72
113
|
&& typeof mod.default === 'function' && mod.GET === undefined
|
|
73
114
|
if (canFastPath)
|
|
74
115
|
fastStaticPages.set(entry.path, createFastStaticPageHandler({ ...fast, entry, mod, layouts, doc, reviveScript: manifest.reviveScript, hasIslands: entry.islandChunks.length > 0, hasLoad }))
|
|
75
116
|
|
|
76
|
-
|
|
117
|
+
const routeEntry = entry.cache !== undefined && dirMiddlewares.length > 0 ? { ...entry, cache: { ...entry.cache, cacheControl: GATED_ROUTE_CACHE_CONTROL } } : entry
|
|
118
|
+
router.add(entry.path, { entry: routeEntry, mod, layouts, dirMiddlewares, middlewares: [...middlewaresFor(entry.path), ...dirMiddlewares], dynamic, doc, headStyleHashes: headTagStyleHashes(doc.headTags) })
|
|
77
119
|
}
|
|
78
120
|
return { router, fastStaticPages }
|
|
79
121
|
}
|
|
122
|
+
|
|
123
|
+
function lookupPrerendered<T>(map: ReadonlyMap<string, T>, staticPath: string | undefined): T | undefined {
|
|
124
|
+
if (staticPath === undefined)
|
|
125
|
+
return undefined
|
|
126
|
+
const normPath = staticPath.length > 1 && staticPath.endsWith('/') ? staticPath.slice(0, -1) : staticPath
|
|
127
|
+
return map.get(staticPath) ?? map.get(normPath)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async function renderRouteBody(route: CompiledRoute, data: unknown, pageProps: Record<string, unknown>, ctx: RequestContext): Promise<string> {
|
|
131
|
+
const { entry, mod, layouts } = route
|
|
132
|
+
let inner: string
|
|
133
|
+
if (entry.isIsland && entry.islandChunk !== undefined) {
|
|
134
|
+
inner = ssrIsland(
|
|
135
|
+
entry.islandChunk,
|
|
136
|
+
() => ssrChild(mod.default!(pageProps) as Html),
|
|
137
|
+
pickPageIslandProps(data, entry, mod),
|
|
138
|
+
entry.islandHydrate as HydrateStrategy | undefined,
|
|
139
|
+
true,
|
|
140
|
+
mod.default!.name,
|
|
141
|
+
)
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
inner = ssrChild(await mod.default!(pageProps) as Html)
|
|
145
|
+
}
|
|
146
|
+
for (let i = layouts.length - 1; i >= 0; i--)
|
|
147
|
+
inner = ssrChild(await layouts[i]!.default({ children: raw(inner), url: ctx.url, params: ctx.params, locals: ctx.locals }) as Html)
|
|
148
|
+
return inner
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export interface CompiledRouteRendererDeps {
|
|
152
|
+
manifest: BuildManifest
|
|
153
|
+
prerenderedMap: ReadonlyMap<string, ServedFile>
|
|
154
|
+
serveFile: (request: Request, file: ServedFile) => Promise<Response>
|
|
155
|
+
buildNotFoundResponse: (url?: URL, req?: Request, ctx?: RequestContext, routeFile?: string) => Promise<Response>
|
|
156
|
+
htmlHeaders: HtmlHeaders
|
|
157
|
+
resolveDocumentHead: DocumentHooks['resolveDocumentHead']
|
|
158
|
+
applyHtmlTransforms: DocumentHooks['applyHtmlTransforms']
|
|
159
|
+
hasHtmlTransformers: boolean
|
|
160
|
+
streamTimeout: number
|
|
161
|
+
/** Where a streamed page registers, so shutdown can end it. */
|
|
162
|
+
streams?: Pick<StreamTracker, 'trackPage'> | undefined
|
|
163
|
+
compressCache: CompressionCache | undefined
|
|
164
|
+
routeCache: RouteCache | undefined
|
|
165
|
+
notifyError: NotifyError
|
|
166
|
+
actions: ActionRegistry
|
|
167
|
+
rewrites: readonly ((pathname: string, ctx: RequestContext) => string | undefined)[]
|
|
168
|
+
trustProxy: TrustProxy
|
|
169
|
+
consent: NormalizedConsentConfig | undefined
|
|
170
|
+
resolveRequestId: RequestIdResolver
|
|
171
|
+
serverAppCtx: AppRuntimeContext
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
type RenderedPageOutput =
|
|
175
|
+
| { kind: 'stream', response: Response }
|
|
176
|
+
| { kind: 'html', html: string, headers: Record<string, string> }
|
|
177
|
+
|
|
178
|
+
async function renderPageDocument(
|
|
179
|
+
deps: CompiledRouteRendererDeps,
|
|
180
|
+
route: CompiledRoute,
|
|
181
|
+
data: unknown,
|
|
182
|
+
pageProps: Record<string, unknown>,
|
|
183
|
+
ctx: RequestContext,
|
|
184
|
+
hasFlash: boolean,
|
|
185
|
+
disableTimeout?: () => void,
|
|
186
|
+
timing?: DevTiming,
|
|
187
|
+
privateTracker?: PrivateReadTracker,
|
|
188
|
+
): Promise<RenderedPageOutput> {
|
|
189
|
+
const { entry, doc } = route
|
|
190
|
+
const renderStart = startTimer(timing)
|
|
191
|
+
const rctx = createRenderContext(undefined, undefined, privateTracker !== undefined ? privateTracker.consent : ctx.consent, error => deps.notifyError(error, ctx.request, 'route', ctx.requestId))
|
|
192
|
+
const body = await runWithRenderContext(rctx, () => renderRouteBody(route, data, pageProps, privateTracker?.ctx ?? ctx))
|
|
193
|
+
if (hasFlash)
|
|
194
|
+
ctx.cookies.delete(ACTION_ERROR_COOKIE)
|
|
195
|
+
const docHead = await deps.resolveDocumentHead(rctx, doc.headTags as readonly string[] | undefined, ctx.url, ctx.request, ctx)
|
|
196
|
+
const renderedIslands = resolveRenderedIslands(rctx, body)
|
|
197
|
+
const isStreamed = rctx.pending.length > 0
|
|
198
|
+
const hasIslands = renderedIslands.hasIslands || (isStreamed && entry.islandChunks.length > 0)
|
|
199
|
+
const preloadChunks = [...isStreamed ? entry.eagerIslandChunks ?? entry.islandChunks : renderedIslands.eagerChunks, ...revivePreload(rctx, deps.manifest.reviveScript)]
|
|
200
|
+
const hasRegions = rctx.regionNames.size > 0
|
|
201
|
+
const trustedStyleHashes = mergeStyleHashes(route.headStyleHashes, docHead.styleHashes, styleHashesOf(rctx.inlineStyles))
|
|
202
|
+
|
|
203
|
+
if (isStreamed) {
|
|
204
|
+
endTimer(timing, 'render', renderStart)
|
|
205
|
+
const streamStart = startTimer(timing)
|
|
206
|
+
if (ctx.request.method === 'HEAD') {
|
|
207
|
+
rctx.aborted = true
|
|
208
|
+
endTimer(timing, 'stream', streamStart)
|
|
209
|
+
return { kind: 'stream', response: markCompressionHandled(new Response(null, { status: 200, headers: withRegionVary(deps.htmlHeaders(hasIslands, true, true, trustedStyleHashes), hasRegions) })) }
|
|
210
|
+
}
|
|
211
|
+
disableTimeout?.()
|
|
212
|
+
const docShell = renderDocument({ ...doc, body: body + STREAM_TAIL_MARK, head: docHead.head, lang: docHead.lang, htmlAttrs: docHead.htmlAttrs, preloadChunks, hasIslands, hasSuspense: true })
|
|
213
|
+
const shell = deps.hasHtmlTransformers ? await deps.applyHtmlTransforms(docShell, ctx.url, ctx.request, ctx) : docShell
|
|
214
|
+
const streamStyleHashes = mergeStyleHashes(trustedStyleHashes, addedInlineStyleHashes(docShell, shell))
|
|
215
|
+
rctx.headersFlushed = true
|
|
216
|
+
endTimer(timing, 'stream', streamStart)
|
|
217
|
+
const transformChunk = deps.hasHtmlTransformers ? (html: string) => deps.applyHtmlTransforms(html, ctx.url, ctx.request, ctx) : undefined
|
|
218
|
+
const response = await compressedHtmlStream(streamDocument(shell, rctx, deps.streamTimeout, error => deps.notifyError(error, ctx.request, 'route', ctx.requestId), transformChunk, deps.streams), 200, withRegionVary(deps.htmlHeaders(hasIslands, true, true, streamStyleHashes), hasRegions), ctx.request, deps.compressCache)
|
|
219
|
+
return { kind: 'stream', response }
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const docHtml = renderDocument({ ...doc, body, head: docHead.head, lang: docHead.lang, htmlAttrs: docHead.htmlAttrs, preloadChunks, hasIslands })
|
|
223
|
+
const html = deps.hasHtmlTransformers ? await deps.applyHtmlTransforms(docHtml, ctx.url, ctx.request, ctx) : docHtml
|
|
224
|
+
const finalStyleHashes = mergeStyleHashes(trustedStyleHashes, addedInlineStyleHashes(docHtml, html))
|
|
225
|
+
endTimer(timing, 'render', renderStart)
|
|
226
|
+
return { kind: 'html', html, headers: withRegionVary(deps.htmlHeaders(hasIslands, false, true, finalStyleHashes), hasRegions) }
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
async function revalidateCompiledRoute(
|
|
230
|
+
deps: CompiledRouteRendererDeps,
|
|
231
|
+
route: CompiledRoute,
|
|
232
|
+
url: URL,
|
|
233
|
+
params: Record<string, string>,
|
|
234
|
+
cacheKey: string,
|
|
235
|
+
cacheSpec: RouteCacheSpec,
|
|
236
|
+
middlewares: readonly Middleware[],
|
|
237
|
+
): Promise<void> {
|
|
238
|
+
const routeCache = deps.routeCache
|
|
239
|
+
if (routeCache === undefined)
|
|
240
|
+
return
|
|
241
|
+
await runWithAppContext(deps.serverAppCtx, async () => {
|
|
242
|
+
const bgReq = new Request(url.href, { method: 'GET' })
|
|
243
|
+
const bgReqId = deps.resolveRequestId(bgReq)
|
|
244
|
+
const { ctx, cookieStore } = makeRequestContext(bgReq, { ...params }, { trustProxy: deps.trustProxy, url, consent: deps.consent, requestId: bgReqId })
|
|
245
|
+
for (const rewrite of deps.rewrites)
|
|
246
|
+
rewrite(url.pathname, ctx)
|
|
247
|
+
await deps.actions.run(ctx, async () => {
|
|
248
|
+
let rendered: RenderedPageOutput | undefined
|
|
249
|
+
let nonCacheable = false
|
|
250
|
+
const res = await runChain(ctx, middlewares, async () => {
|
|
251
|
+
const tracker = trackPrivateReads(ctx)
|
|
252
|
+
let data: unknown
|
|
253
|
+
if (route.entry.hasLoad && typeof route.mod.load === 'function') {
|
|
254
|
+
try {
|
|
255
|
+
data = await route.mod.load(tracker.ctx)
|
|
256
|
+
}
|
|
257
|
+
catch (error) {
|
|
258
|
+
if (isNotFound(error)) {
|
|
259
|
+
nonCacheable = true
|
|
260
|
+
return new Response(null, { status: 404 })
|
|
261
|
+
}
|
|
262
|
+
throw error
|
|
263
|
+
}
|
|
264
|
+
if (isNotFound(data) || data instanceof Response) {
|
|
265
|
+
nonCacheable = true
|
|
266
|
+
return data instanceof Response ? data : new Response(null, { status: 404 })
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
const pageProps: Record<string, unknown> = { params: ctx.params, locals: tracker.locals, url: ctx.url, ...(data !== undefined ? { data } : {}) }
|
|
270
|
+
rendered = await renderPageDocument(deps, route, data, pageProps, ctx, false, undefined, undefined, tracker)
|
|
271
|
+
if (tracker.didReadPrivate())
|
|
272
|
+
nonCacheable = true
|
|
273
|
+
return rendered.kind === 'html'
|
|
274
|
+
? new Response(null, { status: 200, headers: rendered.headers })
|
|
275
|
+
: rendered.response
|
|
276
|
+
})
|
|
277
|
+
if (!nonCacheable && rendered?.kind === 'html' && res.status === 200 && !cookieStore.hasChanges)
|
|
278
|
+
routeCache.set(cacheKey, rendered.html, withCacheControl(rendered.headers, cacheSpec.cacheControl), cacheSpec)
|
|
279
|
+
else
|
|
280
|
+
routeCache.delete(cacheKey)
|
|
281
|
+
})
|
|
282
|
+
})
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
async function runCompiledRouteLoad(
|
|
286
|
+
deps: CompiledRouteRendererDeps,
|
|
287
|
+
route: CompiledRoute,
|
|
288
|
+
ctx: RequestContext,
|
|
289
|
+
timing?: DevTiming,
|
|
290
|
+
): Promise<{ kind: 'response', response: Response } | { kind: 'data', data: unknown }> {
|
|
291
|
+
const { entry, mod } = route
|
|
292
|
+
let data: unknown
|
|
293
|
+
if (entry.hasLoad && typeof mod.load === 'function') {
|
|
294
|
+
const loadStart = startTimer(timing)
|
|
295
|
+
try {
|
|
296
|
+
data = await mod.load(ctx)
|
|
297
|
+
}
|
|
298
|
+
catch (error) {
|
|
299
|
+
if (isNotFound(error))
|
|
300
|
+
return { kind: 'response', response: await deps.buildNotFoundResponse(ctx.url, ctx.request, ctx, entry.file) }
|
|
301
|
+
throw error
|
|
302
|
+
}
|
|
303
|
+
endTimer(timing, 'load', loadStart)
|
|
304
|
+
}
|
|
305
|
+
if (isNotFound(data))
|
|
306
|
+
return { kind: 'response', response: await deps.buildNotFoundResponse(ctx.url, ctx.request, ctx, entry.file) }
|
|
307
|
+
if (data instanceof Response)
|
|
308
|
+
return { kind: 'response', response: data }
|
|
309
|
+
return { kind: 'data', data }
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
async function resolvePrerenderedOrApi(
|
|
313
|
+
deps: CompiledRouteRendererDeps,
|
|
314
|
+
route: CompiledRoute,
|
|
315
|
+
ctx: RequestContext,
|
|
316
|
+
isRegion: boolean,
|
|
317
|
+
staticPath?: string,
|
|
318
|
+
): Promise<Response | undefined> {
|
|
319
|
+
const { entry, mod } = route
|
|
320
|
+
if (isRead(ctx.request)) {
|
|
321
|
+
const pre = lookupPrerendered(deps.prerenderedMap, staticPath)
|
|
322
|
+
if (pre !== undefined && !isRegion && (ctx.request.method === 'HEAD' || !hasActionErrorCookie(ctx.request)))
|
|
323
|
+
return deps.serveFile(ctx.request, pre)
|
|
324
|
+
if (pre === undefined && entry.prerender === true && entry.prerendered !== undefined && route.dynamic)
|
|
325
|
+
return deps.buildNotFoundResponse(ctx.url, ctx.request, ctx, entry.file)
|
|
326
|
+
}
|
|
327
|
+
const method = ctx.request.method.toUpperCase()
|
|
328
|
+
const methodHandler = routeMethodHandler(mod, method)
|
|
329
|
+
if (methodHandler !== undefined)
|
|
330
|
+
return await methodHandler(ctx)
|
|
331
|
+
if (method === 'HEAD' && typeof mod.GET === 'function')
|
|
332
|
+
return stripHeadBody(await mod.GET(ctx))
|
|
333
|
+
return undefined
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
export function createCompiledRouteRenderer(deps: CompiledRouteRendererDeps) {
|
|
337
|
+
return async function renderCompiledRoute(
|
|
338
|
+
route: CompiledRoute,
|
|
339
|
+
ctx: RequestContext,
|
|
340
|
+
cookieStore: CookieStore,
|
|
341
|
+
disableTimeout?: () => void,
|
|
342
|
+
staticPath?: string,
|
|
343
|
+
timing?: DevTiming,
|
|
344
|
+
middlewares: readonly Middleware[] = route.middlewares,
|
|
345
|
+
): Promise<Response> {
|
|
346
|
+
const { entry, mod, doc } = route
|
|
347
|
+
const isRegion = hasRegionHeader(ctx.request)
|
|
348
|
+
|
|
349
|
+
const early = await resolvePrerenderedOrApi(deps, route, ctx, isRegion, staticPath)
|
|
350
|
+
if (early !== undefined)
|
|
351
|
+
return early
|
|
352
|
+
|
|
353
|
+
if (typeof mod.default !== 'function' || !isRead(ctx.request))
|
|
354
|
+
return methodNotAllowed(allowedMethodsHeader(mod))
|
|
355
|
+
if (isRegion && requestedRegion(ctx.request) === undefined)
|
|
356
|
+
return invalidRegionResponse(ctx.request)
|
|
357
|
+
|
|
358
|
+
const hasFlash = ctx.request.method === 'GET' && !isRegion && hasActionErrorCookie(ctx.request) && hydrateActionErrorFlash(ctx)
|
|
359
|
+
const cacheSpec = deps.routeCache !== undefined && !isRegion && !hasFlash && isCacheableRequest(ctx.request) ? entry.cache : undefined
|
|
360
|
+
const cacheKey = cacheSpec !== undefined ? routeCacheKey(ctx.url) : undefined
|
|
361
|
+
let inflight: PromiseWithResolvers<RouteCacheSingleflightResult | undefined> | undefined
|
|
362
|
+
if (cacheSpec !== undefined && cacheKey !== undefined && deps.routeCache !== undefined) {
|
|
363
|
+
const cached = serveCachedHit(
|
|
364
|
+
deps.routeCache,
|
|
365
|
+
cacheKey,
|
|
366
|
+
ctx.request,
|
|
367
|
+
ctx.requestId,
|
|
368
|
+
deps.compressCache,
|
|
369
|
+
() => revalidateCompiledRoute(deps, route, ctx.url, ctx.params, cacheKey, cacheSpec, middlewares),
|
|
370
|
+
error => deps.notifyError(error, ctx.request, 'route', ctx.requestId),
|
|
371
|
+
)
|
|
372
|
+
if (cached !== undefined)
|
|
373
|
+
return cached
|
|
374
|
+
const existing = deps.routeCache.getInflight(cacheKey)
|
|
375
|
+
if (existing !== undefined) {
|
|
376
|
+
const shared = await existing
|
|
377
|
+
if (shared !== undefined)
|
|
378
|
+
return deps.routeCache.respondMiss(shared.entry, shared.html, shared.headers, ctx.request, ctx.requestId, deps.compressCache)
|
|
379
|
+
}
|
|
380
|
+
else {
|
|
381
|
+
inflight = Promise.withResolvers<RouteCacheSingleflightResult | undefined>()
|
|
382
|
+
void inflight.promise.catch(() => undefined)
|
|
383
|
+
deps.routeCache.trackInflight(cacheKey, inflight.promise)
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
try {
|
|
388
|
+
const privateTracker = cacheSpec !== undefined ? trackPrivateReads(ctx) : undefined
|
|
389
|
+
const loaded = await runCompiledRouteLoad(deps, route, privateTracker?.ctx ?? ctx, timing)
|
|
390
|
+
if (loaded.kind === 'response')
|
|
391
|
+
return loaded.response
|
|
392
|
+
const pageProps: Record<string, unknown> = { params: ctx.params, locals: privateTracker?.locals ?? ctx.locals, url: ctx.url, ...(loaded.data !== undefined ? { data: loaded.data } : {}) }
|
|
393
|
+
|
|
394
|
+
if (isRegion) {
|
|
395
|
+
return respondRegion(
|
|
396
|
+
ctx.request, mod, pageProps, entry.path, deps.htmlHeaders,
|
|
397
|
+
rctx => deps.resolveDocumentHead(rctx, doc.headTags as readonly string[] | undefined, ctx.url, ctx.request, ctx),
|
|
398
|
+
deps.streamTimeout, error => deps.notifyError(error, ctx.request, 'route', ctx.requestId),
|
|
399
|
+
deps.hasHtmlTransformers ? html => deps.applyHtmlTransforms(html, ctx.url, ctx.request, ctx) : undefined,
|
|
400
|
+
)
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
const rendered = await renderPageDocument(deps, route, loaded.data, pageProps, ctx, hasFlash, disableTimeout, timing, privateTracker)
|
|
404
|
+
if (rendered.kind === 'stream')
|
|
405
|
+
return rendered.response
|
|
406
|
+
if (privateTracker?.didReadPrivate())
|
|
407
|
+
return compressedHtmlResponse(rendered.html, 200, withCacheControl(rendered.headers, 'private, no-store'), ctx.request, deps.compressCache)
|
|
408
|
+
if (cacheSpec !== undefined && cacheKey !== undefined && deps.routeCache !== undefined && !cookieStore.hasChanges) {
|
|
409
|
+
const cachedHeaders = withCacheControl(rendered.headers, cacheSpec.cacheControl)
|
|
410
|
+
const stored = deps.routeCache.set(cacheKey, rendered.html, cachedHeaders, cacheSpec)
|
|
411
|
+
inflight?.resolve({ entry: stored, html: rendered.html, headers: cachedHeaders })
|
|
412
|
+
return deps.routeCache.respondMiss(stored, rendered.html, cachedHeaders, ctx.request, ctx.requestId, deps.compressCache)
|
|
413
|
+
}
|
|
414
|
+
return compressedHtmlResponse(rendered.html, 200, rendered.headers, ctx.request, deps.compressCache)
|
|
415
|
+
}
|
|
416
|
+
catch (error) {
|
|
417
|
+
inflight?.reject(error)
|
|
418
|
+
throw error
|
|
419
|
+
}
|
|
420
|
+
finally {
|
|
421
|
+
inflight?.resolve(undefined)
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}
|