@opentf/afw 0.37.0 → 0.39.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/core/route.d.ts +33 -0
- package/core/route.js +12 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/jsx-runtime.d.ts +22 -1
- package/package.json +5 -1
- package/runtime/dom.js +23 -4
- package/runtime/error-boundary.d.ts +1 -1
- package/runtime/error-boundary.js +11 -1
- package/runtime/head.js +10 -1
- package/runtime/hmr.js +29 -0
- package/runtime/index.d.ts +4 -0
- package/runtime/index.js +5 -0
- package/runtime/lazy.d.ts +8 -0
- package/runtime/lazy.js +135 -0
- package/runtime/pending-scope.d.ts +6 -0
- package/runtime/pending-scope.js +39 -0
- package/runtime/pending.d.ts +31 -0
- package/runtime/pending.js +276 -0
- package/runtime/resource.d.ts +6 -0
- package/runtime/resource.js +65 -5
- package/runtime/route-data.js +33 -1
- package/runtime/route-errors.d.ts +3 -0
- package/runtime/route-errors.js +16 -0
- package/runtime/route-match.js +97 -12
- package/runtime/router.d.ts +44 -3
- package/runtime/router.js +470 -49
- package/runtime/scroll.js +130 -0
- package/runtime/transition-hooks.js +10 -0
- package/runtime/transition.d.ts +43 -0
- package/runtime/transition.js +187 -0
- package/server/api.js +3 -3
- package/server/builtins.js +29 -1
- package/server/index.d.ts +20 -1
- package/server/index.js +3 -2
- package/server/loader.js +73 -7
- package/server/middleware.js +9 -5
- package/server/render.js +85 -16
package/server/middleware.js
CHANGED
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
// middleware), mirroring the loader registry (loader.js `match`).
|
|
20
20
|
|
|
21
21
|
import { DATA_FILE } from "../runtime/route-data.js";
|
|
22
|
-
import { compareRoutes,
|
|
22
|
+
import { compareRoutes, normalizePath as normalize, scopeMatcher, stripAppDir } from "../runtime/route-match.js";
|
|
23
23
|
|
|
24
24
|
/** The folder route a `_middleware.{js,ts}` file governs: `app/api/_middleware.js`
|
|
25
25
|
* → `/api` (applies to `/api` and everything nested under it); `app/_middleware.js`
|
|
@@ -44,22 +44,26 @@ export function outermostFirst(a, b) {
|
|
|
44
44
|
*
|
|
45
45
|
* @param {Record<string, object>} middlewareModules the middleware is each
|
|
46
46
|
* module's default (or named `middleware`) export.
|
|
47
|
-
* @param {{ appDir?: string, i18n?: { locales?: string[], defaultLocale?: string }
|
|
47
|
+
* @param {{ appDir?: string, i18n?: { locales?: string[], defaultLocale?: string },
|
|
48
|
+
* groupScopes?: Record<string, { scopes?: string[], paths?: string[] }> }} [options]
|
|
48
49
|
* `appDir` pins exact scope derivation; `i18n` lets scope matching strip a
|
|
49
|
-
* leading non-default locale prefix (mirroring the loader registry)
|
|
50
|
+
* leading non-default locale prefix (mirroring the loader registry);
|
|
51
|
+
* `groupScopes` gives each middleware folder inside a route group what it covers.
|
|
50
52
|
* @returns {{ size: number, scopes: string[],
|
|
51
53
|
* run: (request: Request,
|
|
52
54
|
* terminal: (req: Request, context: object) => Response | Promise<Response>,
|
|
53
55
|
* extras?: { env?: unknown, ctx?: unknown }) => Promise<Response> }}
|
|
54
56
|
*/
|
|
55
|
-
export function createMiddleware(middlewareModules = {}, { appDir, i18n } = {}) {
|
|
57
|
+
export function createMiddleware(middlewareModules = {}, { appDir, i18n, groupScopes = {} } = {}) {
|
|
56
58
|
const entries = [];
|
|
57
59
|
for (const file in middlewareModules) {
|
|
58
60
|
const mod = middlewareModules[file];
|
|
59
61
|
const fn = mod.default ?? mod.middleware;
|
|
60
62
|
if (typeof fn === "function") {
|
|
61
63
|
const scope = middlewareScopeFromPath(file, appDir);
|
|
62
|
-
|
|
64
|
+
// A folder inside a route group covers what the group holds (`groupScopes`, from
|
|
65
|
+
// the build), not a URL prefix: see `scopeMatcher`.
|
|
66
|
+
entries.push({ scope, matcher: scopeMatcher(scope, groupScopes[scope]), fn });
|
|
63
67
|
}
|
|
64
68
|
}
|
|
65
69
|
entries.sort(outermostFirst);
|
package/server/render.js
CHANGED
|
@@ -7,9 +7,17 @@
|
|
|
7
7
|
// client router; here we resolve a route's page + layout chain to string
|
|
8
8
|
// renderers and concatenate them, passing `children` as an HTML string.
|
|
9
9
|
|
|
10
|
+
import { DEV } from "../core/dev.js";
|
|
11
|
+
import { urlOf } from "../runtime/route-match.js";
|
|
12
|
+
import { routeErrorPayload } from "../runtime/route-data.js";
|
|
13
|
+
import { isClientRoute } from "../runtime/route-match.js";
|
|
10
14
|
import {
|
|
15
|
+
clientSplit,
|
|
11
16
|
layoutChain,
|
|
17
|
+
layoutFolders,
|
|
18
|
+
layoutWraps,
|
|
12
19
|
matchRoute,
|
|
20
|
+
routeErrorSite,
|
|
13
21
|
resolveModule,
|
|
14
22
|
routes,
|
|
15
23
|
setRouteState,
|
|
@@ -30,29 +38,63 @@ import { withRenderContext } from "./render-context.js";
|
|
|
30
38
|
* prerender own the loader bundle) and is exposed to the page as `router.data`.
|
|
31
39
|
* Returns `null` if there's no match and no 404 page.
|
|
32
40
|
*/
|
|
33
|
-
export async function renderRoute(pathname, params = null, search = "", { data } = {}) {
|
|
41
|
+
export async function renderRoute(pathname, params = null, search = "", { data, error } = {}) {
|
|
34
42
|
return withRenderContext({ route: {}, hydration: null }, () =>
|
|
35
|
-
renderInContext(pathname, params, search, data));
|
|
43
|
+
renderInContext(pathname, params, search, data, error));
|
|
36
44
|
}
|
|
37
45
|
|
|
38
|
-
|
|
46
|
+
/**
|
|
47
|
+
* The error a page's `error.jsx` receives after a server-side failure. Production
|
|
48
|
+
* shows a generic message, so a loader's internals (a query, a hostname) never reach
|
|
49
|
+
* the page; development shows the real one.
|
|
50
|
+
*/
|
|
51
|
+
function publicError(error) {
|
|
52
|
+
const status = Number.isInteger(error?.status) && error.status >= 400 ? error.status : 500;
|
|
53
|
+
const message = DEV && error?.message ? String(error.message) : "Something went wrong";
|
|
54
|
+
const out = Object.assign(new Error(message), { status });
|
|
55
|
+
if (error?.afwLayoutRoute) out.afwLayoutRoute = error.afwLayoutRoute;
|
|
56
|
+
return out;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async function renderInContext(pathname, params, search, data, failure) {
|
|
39
60
|
const real = matchRoute(pathname);
|
|
40
|
-
|
|
61
|
+
let match =
|
|
41
62
|
real || (routes.notFound ? { entry: routes.notFound, params: {}, route: null } : null);
|
|
42
63
|
if (!match) return null;
|
|
43
64
|
if (params) match.params = params;
|
|
65
|
+
// A failed loader renders an `error.jsx` with status 500: the page's nearest one, or —
|
|
66
|
+
// when a layout's loader failed — the one above that layout, inside the layouts above
|
|
67
|
+
// it. No error file: the caller's error stands.
|
|
68
|
+
let error = null;
|
|
69
|
+
let broken = null;
|
|
70
|
+
if (failure !== undefined) {
|
|
71
|
+
broken = failure?.afwLayoutRoute ?? null;
|
|
72
|
+
const entry = routeErrorSite(match.route, broken).entry;
|
|
73
|
+
if (!entry) throw failure;
|
|
74
|
+
error = publicError(failure);
|
|
75
|
+
match = { ...match, entry };
|
|
76
|
+
}
|
|
44
77
|
|
|
45
78
|
// Let a page reading `router.params`/`pathname`/`query`/`data` resolve to this route.
|
|
46
79
|
setRouteState({ pathname, search, params: match.params, data });
|
|
47
80
|
|
|
48
81
|
const query = Object.fromEntries(new URLSearchParams(search));
|
|
49
|
-
const props =
|
|
82
|
+
const props = error
|
|
83
|
+
? { params: match.params, query, error, retry: () => {} }
|
|
84
|
+
: { params: match.params, query };
|
|
50
85
|
|
|
51
86
|
// Import modules before collecting: module-level JSX must keep its inline
|
|
52
87
|
// props rather than capture hydration ids belonging to just the first request.
|
|
53
|
-
const chain =
|
|
88
|
+
const chain = broken
|
|
89
|
+
? layoutFolders(match.route).filter((f) => f !== broken && layoutWraps(f, broken)).map((f) => routes.layouts[f])
|
|
90
|
+
: layoutChain(match.route);
|
|
91
|
+
// A client route renders the layouts above its first client entry; that region stays
|
|
92
|
+
// empty for the browser to build (`hydrateClientRoute`). Client entries have no module.
|
|
93
|
+
const split = error ? -1 : clientSplit(match.entry, chain);
|
|
94
|
+
const serverChain = split >= 0 ? chain.slice(0, split) : chain;
|
|
54
95
|
const [pageModule, ...layoutModules] = await Promise.all(
|
|
55
|
-
[match.entry, ...
|
|
96
|
+
[split >= 0 ? null : match.entry, ...serverChain].map(async entry => {
|
|
97
|
+
if (!entry) return null;
|
|
56
98
|
const module = await resolveModule(entry);
|
|
57
99
|
// A loader can return a bare factory. Keep it distinguishable from a
|
|
58
100
|
// loader when these resolved entries are passed to metadata resolution.
|
|
@@ -68,7 +110,7 @@ async function renderInContext(pathname, params, search, data) {
|
|
|
68
110
|
let html, hydration;
|
|
69
111
|
beginHydrationCollect();
|
|
70
112
|
try {
|
|
71
|
-
html = page(props);
|
|
113
|
+
html = split >= 0 ? "" : page(props);
|
|
72
114
|
// Wrap with layouts, most-specific inward to root outermost.
|
|
73
115
|
for (let i = layouts.length - 1; i >= 0; i--) {
|
|
74
116
|
html = layouts[i]({ ...props, children: html });
|
|
@@ -79,14 +121,19 @@ async function renderInContext(pathname, params, search, data) {
|
|
|
79
121
|
|
|
80
122
|
const metadata = await resolveMetadata({
|
|
81
123
|
route: match.route,
|
|
82
|
-
entry: pageModule,
|
|
124
|
+
entry: pageModule ?? {},
|
|
83
125
|
layouts: layoutModules,
|
|
84
126
|
params: match.params,
|
|
85
127
|
query,
|
|
86
128
|
});
|
|
87
129
|
// `route` is the matched *pattern* ("/docs/[slug]", null for the 404 fallback) — the
|
|
88
130
|
// toolchain keys its per-route chunk manifest by it to emit `<link rel="modulepreload">`.
|
|
89
|
-
|
|
131
|
+
if (error) {
|
|
132
|
+
// The client reads this marker from the inline data and shows the same error view.
|
|
133
|
+
const dataJson = routeErrorPayload(error);
|
|
134
|
+
return { html, metadata, status: error.status, hydration, route: match.route, dataJson, error };
|
|
135
|
+
}
|
|
136
|
+
return { html, metadata, status: real ? 200 : 404, hydration, route: match.route, ...(split >= 0 ? { client: true } : {}) };
|
|
90
137
|
}
|
|
91
138
|
|
|
92
139
|
/** Back-compat / convenience: render just the `#app` markup for `pathname`. */
|
|
@@ -113,7 +160,11 @@ function fillRoute(route, params) {
|
|
|
113
160
|
if (!params || typeof params !== "object" || Array.isArray(params)) {
|
|
114
161
|
throw new Error(`getStaticPaths for ${route}: params must be an object.`);
|
|
115
162
|
}
|
|
116
|
-
|
|
163
|
+
// The URL, not the folder: route groups drop out. An optional segment
|
|
164
|
+
// (`[[lang]]`, `[[...rest]]`) left out of `params` (or undefined) drops out too.
|
|
165
|
+
const url = urlOf(route).replace(/\/\[\[(\.\.\.)?([^\]]+)\]\]/g, (whole, rest, key) =>
|
|
166
|
+
params[key] === undefined || (rest && Array.isArray(params[key]) && params[key].length === 0) ? "" : `/[${rest ?? ""}${key}]`);
|
|
167
|
+
return (url.replace(/\[(\.\.\.)?([^\]]+)\]/g, (_, rest, key) => {
|
|
117
168
|
if (!Object.prototype.hasOwnProperty.call(params, key)) {
|
|
118
169
|
throw new Error(`getStaticPaths for ${route}: missing parameter "${key}".`);
|
|
119
170
|
}
|
|
@@ -124,7 +175,7 @@ function fillRoute(route, params) {
|
|
|
124
175
|
return segments.map(segment => encodedSegment(segment, route, key)).join("/");
|
|
125
176
|
}
|
|
126
177
|
return encodedSegment(value, route, key);
|
|
127
|
-
});
|
|
178
|
+
})) || "/";
|
|
128
179
|
}
|
|
129
180
|
|
|
130
181
|
/**
|
|
@@ -136,6 +187,19 @@ function fillRoute(route, params) {
|
|
|
136
187
|
* (the chunk manifest behind `<link rel="modulepreload">`). Dynamic routes without
|
|
137
188
|
* `getStaticPaths` are collected as `skipped`.
|
|
138
189
|
*/
|
|
190
|
+
/** Each static route's `paths` from its folder's `_config.js`, keyed by route pattern. */
|
|
191
|
+
const staticPaths = new Map();
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Register the `paths` functions of `_config.js` files (route pattern → function); the
|
|
195
|
+
* generated server module calls this. They list a dynamic static route's pages, like a
|
|
196
|
+
* page's `getStaticPaths`, which they take precedence over.
|
|
197
|
+
*/
|
|
198
|
+
export function registerStaticPaths(map) {
|
|
199
|
+
staticPaths.clear();
|
|
200
|
+
for (const [route, fn] of Object.entries(map ?? {})) if (typeof fn === "function") staticPaths.set(route, fn);
|
|
201
|
+
}
|
|
202
|
+
|
|
139
203
|
export async function collectRoutePaths() {
|
|
140
204
|
const paths = [];
|
|
141
205
|
const skipped = [];
|
|
@@ -148,12 +212,17 @@ export async function collectRoutePaths() {
|
|
|
148
212
|
}
|
|
149
213
|
for (const route in routes.pages) {
|
|
150
214
|
if (!route.includes("[")) {
|
|
151
|
-
add(route, {}, route);
|
|
215
|
+
add(urlOf(route), {}, route);
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
// A dynamic page that renders in the browser has no module here to list its pages.
|
|
219
|
+
if (isClientRoute(routes.pages[route]) && !staticPaths.has(route)) {
|
|
220
|
+
skipped.push(route);
|
|
152
221
|
continue;
|
|
153
222
|
}
|
|
154
|
-
const ns = await resolveModule(routes.pages[route]);
|
|
155
|
-
const getStaticPaths =
|
|
156
|
-
ns && (ns.getStaticPaths || (ns.default && ns.default.getStaticPaths));
|
|
223
|
+
const ns = staticPaths.has(route) ? null : await resolveModule(routes.pages[route]);
|
|
224
|
+
const getStaticPaths = staticPaths.get(route) ??
|
|
225
|
+
(ns && (ns.getStaticPaths || (ns.default && ns.default.getStaticPaths)));
|
|
157
226
|
if (typeof getStaticPaths === "function") {
|
|
158
227
|
const entries = await getStaticPaths();
|
|
159
228
|
if (!Array.isArray(entries)) throw new Error(`getStaticPaths for ${route} must return an array.`);
|