@tokenoftrust/storefront-runner 2.2.84 → 2.2.86
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/agency-kit/tools/lib/redirect-declaration.mjs +627 -0
- package/apps/storefront/src/components/Seo.astro +12 -0
- package/apps/storefront/src/components/blog/BlogIndexView.astro +140 -0
- package/apps/storefront/src/layouts/Layout.astro +4 -0
- package/apps/storefront/src/lib/blog/pagination.ts +78 -0
- package/apps/storefront/src/lib/blog/provider.ts +34 -1
- package/apps/storefront/src/lib/blog/types.ts +18 -0
- package/apps/storefront/src/pages/blog/index.astro +17 -91
- package/apps/storefront/src/pages/blog/page/[n].astro +39 -0
- package/apps/storefront/src/pages/sitemap.xml.ts +10 -1
- package/package.json +1 -1
- package/packages/public-runtime/src/publication-redirects.d.mts +23 -4
- package/packages/public-runtime/src/publication-redirects.mjs +4 -378
- package/packages/public-runtime/src/static-bundle.ts +59 -11
- package/packages/public-runtime/tests/fixtures/redirect-match-cases.json +34 -0
|
@@ -0,0 +1,627 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// The pure core that turns a tenant's DECLARED redirects into a substrate-neutral redirect set
|
|
3
|
+
// the publish pipeline can emit.
|
|
4
|
+
//
|
|
5
|
+
// This module is the one copy of the redirect contract: the declaration format, the edge budget
|
|
6
|
+
// and the reference matcher. It lives in the agency kit so a store developer's `redirect-map.mjs`
|
|
7
|
+
// validates a map exactly as a publish will, with nothing but the kit installed; the platform
|
|
8
|
+
// imports this file rather than carrying a copy. It imports nothing.
|
|
9
|
+
//
|
|
10
|
+
// A tenant's public site is served CDN -> static object (ADR 0013), so a redirect cannot be a
|
|
11
|
+
// request-time decision in the Worker: it must exist in the published artifact. Both substrates
|
|
12
|
+
// express it the same way — a precomputed set consulted by the single permitted edge function —
|
|
13
|
+
// so the set below is substrate-neutral and the adapters only serialize it.
|
|
14
|
+
//
|
|
15
|
+
// Redirects are DECLARED, never derived from a tenant id. Three declaration sources normalize into
|
|
16
|
+
// the one `redirect-308` contract `PublicationSpaceAliasV1` already defines:
|
|
17
|
+
// - publication-space `routes.aliases` entries whose behavior is `redirect-308`;
|
|
18
|
+
// - a content collection's `redirects.json` (old slug -> new slug), which becomes
|
|
19
|
+
// `/<collection>/<old>/` -> `/<collection>/<new>/`;
|
|
20
|
+
// - the tenant-wide `content/redirects.json` (relative path -> relative path or an explicit
|
|
21
|
+
// HTTPS destination), for legacy URLs that belong to no single collection.
|
|
22
|
+
// Adding a tenant is a declaration, not a code change.
|
|
23
|
+
//
|
|
24
|
+
// Two kinds of entry:
|
|
25
|
+
// - EXACT: `/old/` -> `/new/` (or an explicit `https:` URL). Matches one path.
|
|
26
|
+
// - RULE: `/blogs/news/*` -> `/blog/*`. Matches every path under the directory prefix, the
|
|
27
|
+
// directory itself included. A `*` at the end of the target receives the rest of the request
|
|
28
|
+
// path; a target without `*` sends the whole family to one page. A rule's target is always a
|
|
29
|
+
// same-origin path, and `*` is only ever the trailing segment of either side.
|
|
30
|
+
// Precedence at the edge: an exact entry beats any rule; among rules the longest prefix wins. The
|
|
31
|
+
// query string is forwarded to a same-origin target and dropped for an external one.
|
|
32
|
+
// `resolveRedirect` below is the reference matcher; the Cloudflare static plane
|
|
33
|
+
// (`packages/public-runtime/src/static-bundle.ts`) and the CloudFront function
|
|
34
|
+
// (`scripts/publish/cloudfront/live-pointer-rewrite.mjs`) implement the same semantics in their own
|
|
35
|
+
// runtimes and are held to it by the shared cases in
|
|
36
|
+
// `packages/public-runtime/tests/fixtures/redirect-match-cases.json`.
|
|
37
|
+
//
|
|
38
|
+
// 308 rather than 301 throughout: both pass ranking signal, and 308 additionally preserves the
|
|
39
|
+
// request method, so a redirected non-GET is not silently rewritten. The declared alias contract
|
|
40
|
+
// already specifies 308; collection renames join it rather than introducing a second status.
|
|
41
|
+
//
|
|
42
|
+
// Fails closed. An ambiguous declaration (one `from`, two targets), a cycle, a rule whose target
|
|
43
|
+
// another rule would redirect again, or a set that exceeds the substrate's ceiling REFUSES rather
|
|
44
|
+
// than emitting a partial set — a redirect that silently does not exist is the failure this
|
|
45
|
+
// mechanism exists to prevent.
|
|
46
|
+
|
|
47
|
+
/** The status every materialized redirect carries. */
|
|
48
|
+
export const REDIRECT_STATUS = 308;
|
|
49
|
+
|
|
50
|
+
/** Bump only when the meaning of a whole-site delivery manifest changes without changing its values. */
|
|
51
|
+
export const WHOLE_SITE_DELIVERY_CONTRACT = "whole-site-delivery-v1";
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* CloudFront KeyValueStore quotas (AWS CloudFront quota table, read 2026-10-06). The CloudFront
|
|
55
|
+
* plane stores each entry as its own key, `redirect:<namespace>:<from>` -> `<to>`, so these — not
|
|
56
|
+
* the function's 10 KB code limit — bound the set. The Cloudflare plane reads the same set from
|
|
57
|
+
* the bundle manifest in R2, which has no tighter ceiling.
|
|
58
|
+
*/
|
|
59
|
+
export const KVS_STORE_LIMIT_BYTES = 5 * 1024 * 1024;
|
|
60
|
+
export const KVS_KEY_LIMIT_BYTES = 512;
|
|
61
|
+
export const KVS_VALUE_LIMIT_BYTES = 1024;
|
|
62
|
+
/** Room kept for the store's own keys (`live`, `preview`, `promotions`). */
|
|
63
|
+
export const KVS_RESERVED_BYTES = 64 * 1024;
|
|
64
|
+
/**
|
|
65
|
+
* Bytes available to one redirect set. A publish stages the new namespace before it prunes the
|
|
66
|
+
* old one, so two complete sets share the store for the length of a flip.
|
|
67
|
+
*/
|
|
68
|
+
export const REDIRECT_MAP_BUDGET_BYTES = Math.floor((KVS_STORE_LIMIT_BYTES - KVS_RESERVED_BYTES) / 2);
|
|
69
|
+
/** `redirect:` + a sha256 hex namespace + `:` — the key bytes every entry carries before its path. */
|
|
70
|
+
export const REDIRECT_KEY_OVERHEAD_BYTES = "redirect:".length + 64 + 1;
|
|
71
|
+
/**
|
|
72
|
+
* The CloudFront function reads one key per distinct rule depth on a request no exact entry
|
|
73
|
+
* matched, so the number of distinct depths is the per-request cost of rules. Bounded here.
|
|
74
|
+
*/
|
|
75
|
+
export const REDIRECT_RULE_MAX_DEPTHS = 8;
|
|
76
|
+
|
|
77
|
+
const encoder = new TextEncoder();
|
|
78
|
+
/** @param {string} value */
|
|
79
|
+
const byteLength = (value) => encoder.encode(value).byteLength;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* @typedef {Object} DeclaredRedirect
|
|
83
|
+
* @property {string} from root-absolute request path, or a `/<dir>/*` rule
|
|
84
|
+
* @property {string} to root-absolute destination path (a rule may end in `/*`)
|
|
85
|
+
* @property {string} source which declaration produced it, for the report
|
|
86
|
+
*/
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* @typedef {Object} RedirectSet
|
|
90
|
+
* @property {boolean} ok
|
|
91
|
+
* @property {{from: string, to: string}[]} redirects normalized, deduped, chain-collapsed
|
|
92
|
+
* @property {string[]} violations why it refused; empty when ok
|
|
93
|
+
* @property {number} bytes storage the set occupies at the edge (KVS keys + values)
|
|
94
|
+
* @property {number} budget bytes available to the set
|
|
95
|
+
* @property {number} rules how many entries are `/*` rules
|
|
96
|
+
*/
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Normalize a declared path to the form the edge function compares against: root-absolute, no
|
|
100
|
+
* query or fragment, with a trailing slash unless the last segment names a file.
|
|
101
|
+
* Returns null when the value cannot be a same-origin request path.
|
|
102
|
+
* @param {string} raw
|
|
103
|
+
* @returns {string | null}
|
|
104
|
+
*/
|
|
105
|
+
export function normalizeRedirectPath(raw) {
|
|
106
|
+
if (typeof raw !== "string") return null;
|
|
107
|
+
const trimmed = raw.trim();
|
|
108
|
+
if (!trimmed.startsWith("/")) return null;
|
|
109
|
+
// Protocol-relative (`//host/...`) would leave the origin.
|
|
110
|
+
if (trimmed.startsWith("//")) return null;
|
|
111
|
+
const withoutSuffix = trimmed.split("?")[0].split("#")[0];
|
|
112
|
+
if (withoutSuffix.includes("..") || withoutSuffix.includes("\\")) return null;
|
|
113
|
+
const collapsed = withoutSuffix.replace(/\/{2,}/g, "/");
|
|
114
|
+
if (collapsed === "/") return "/";
|
|
115
|
+
// The last NON-EMPTY segment: a trailing slash otherwise hides the filename from this test.
|
|
116
|
+
const segments = collapsed.split("/").filter(Boolean);
|
|
117
|
+
const last = segments[segments.length - 1] ?? "";
|
|
118
|
+
const namesFile = last.includes(".");
|
|
119
|
+
if (namesFile) return collapsed.replace(/\/$/, "");
|
|
120
|
+
return collapsed.endsWith("/") ? collapsed : `${collapsed}/`;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Is this normalized entry a `/<dir>/*` rule rather than an exact path? */
|
|
124
|
+
export function isRedirectRule(/** @type {string} */ from) {
|
|
125
|
+
return typeof from === "string" && from.endsWith("/*");
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* The directory a rule covers: `/blogs/news/*` -> `/blogs/news/`. Null unless `raw` is a
|
|
130
|
+
* root-absolute directory followed by exactly one trailing `*`.
|
|
131
|
+
* @param {string} raw
|
|
132
|
+
* @returns {string | null}
|
|
133
|
+
*/
|
|
134
|
+
export function ruleDirectory(raw) {
|
|
135
|
+
if (typeof raw !== "string") return null;
|
|
136
|
+
const trimmed = raw.trim();
|
|
137
|
+
if (!trimmed.endsWith("/*") || trimmed.indexOf("*") !== trimmed.length - 1) return null;
|
|
138
|
+
const dir = trimmed.slice(0, -1);
|
|
139
|
+
if (!dir.startsWith("/") || dir.startsWith("//")) return null;
|
|
140
|
+
if (/[?#\\]/.test(dir) || dir.split("/").includes("..")) return null;
|
|
141
|
+
return dir.replace(/\/{2,}/g, "/");
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Normalize a declared SOURCE: an exact path, or a `/<dir>/*` rule. A rule over the whole site
|
|
146
|
+
* (`/*`) is refused — it would shadow every page, which is a domain move, not a redirect.
|
|
147
|
+
* @param {string} raw
|
|
148
|
+
* @returns {string | null}
|
|
149
|
+
*/
|
|
150
|
+
export function normalizeRedirectSource(raw) {
|
|
151
|
+
if (typeof raw !== "string") return null;
|
|
152
|
+
if (!raw.includes("*")) return normalizeRedirectPath(raw);
|
|
153
|
+
const dir = ruleDirectory(raw);
|
|
154
|
+
if (dir === null || dir === "/") return null;
|
|
155
|
+
return `${dir}*`;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Normalize a same-origin path or a deliberately external HTTPS destination.
|
|
160
|
+
*
|
|
161
|
+
* A source is always same-origin; a TARGET may leave the origin, but only when the declaration
|
|
162
|
+
* says so in full: `https:` only, no credentials, no query, no fragment. Anything else — including
|
|
163
|
+
* every shape that merely looks absolute — is not a usable destination and refuses.
|
|
164
|
+
* @param {string} raw
|
|
165
|
+
* @returns {string | null}
|
|
166
|
+
*/
|
|
167
|
+
function normalizeRedirectDestination(/** @type {string} */ raw) {
|
|
168
|
+
if (typeof raw !== "string" || raw.includes("*")) return null;
|
|
169
|
+
const sameOrigin = normalizeRedirectPath(raw);
|
|
170
|
+
if (sameOrigin !== null) return sameOrigin;
|
|
171
|
+
try {
|
|
172
|
+
const url = new URL(raw);
|
|
173
|
+
if (url.protocol !== "https:" || url.username || url.password || url.search || url.hash) {
|
|
174
|
+
return null;
|
|
175
|
+
}
|
|
176
|
+
return url.toString().replace(/\/$/, url.pathname === "/" ? "/" : "");
|
|
177
|
+
} catch {
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* A rule's target: a same-origin path, or a same-origin directory ending in `*` that receives the
|
|
184
|
+
* rest of the request path. Never off-origin: a captured remainder must never pick a host.
|
|
185
|
+
* @param {string} raw
|
|
186
|
+
* @returns {string | null}
|
|
187
|
+
*/
|
|
188
|
+
function normalizeRuleTarget(raw) {
|
|
189
|
+
if (typeof raw !== "string") return null;
|
|
190
|
+
if (!raw.includes("*")) return normalizeRedirectPath(raw);
|
|
191
|
+
const dir = ruleDirectory(raw);
|
|
192
|
+
return dir === null ? null : `${dir}*`;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* The path form both edges compare a request against: a directory request gains its trailing
|
|
197
|
+
* slash, a file request is left alone.
|
|
198
|
+
* @param {string} pathname
|
|
199
|
+
* @returns {string}
|
|
200
|
+
*/
|
|
201
|
+
export function redirectRequestPath(pathname) {
|
|
202
|
+
if (pathname === "/") return pathname;
|
|
203
|
+
const last = pathname.split("/").pop() ?? "";
|
|
204
|
+
if (last.includes(".")) return pathname;
|
|
205
|
+
return pathname.endsWith("/") ? pathname : `${pathname}/`;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Where a rule sends a request whose path continues `capture` past the rule's directory. Null
|
|
210
|
+
* when the result would not be a same-origin path — a capture such as `/evil.example` under a
|
|
211
|
+
* root target would otherwise become `//evil.example`.
|
|
212
|
+
* @param {string} to
|
|
213
|
+
* @param {string} capture
|
|
214
|
+
* @returns {string | null}
|
|
215
|
+
*/
|
|
216
|
+
export function ruleLocation(to, capture) {
|
|
217
|
+
const location = to.endsWith("/*") ? `${to.slice(0, -1)}${capture}` : to;
|
|
218
|
+
return location.startsWith("/") && !location.startsWith("//") && !location.includes("\\")
|
|
219
|
+
? location
|
|
220
|
+
: null;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* The reference matcher: which entry answers `pathname`, and where it sends it.
|
|
225
|
+
* Exact entries are compared against the request as sent and in its trailing-slash form; a rule
|
|
226
|
+
* matches when the trailing-slash form starts with its directory, and the longest directory wins.
|
|
227
|
+
* @param {readonly {from: string, to: string}[]} redirects
|
|
228
|
+
* @param {string} pathname the request path, without query
|
|
229
|
+
* @returns {{from: string, to: string, location: string} | null}
|
|
230
|
+
*/
|
|
231
|
+
export function resolveRedirect(redirects, pathname) {
|
|
232
|
+
const path = redirectRequestPath(pathname);
|
|
233
|
+
/** @type {{from: string, to: string} | null} */
|
|
234
|
+
let rule = null;
|
|
235
|
+
for (const entry of redirects ?? []) {
|
|
236
|
+
if (isRedirectRule(entry.from)) {
|
|
237
|
+
const dir = entry.from.slice(0, -1);
|
|
238
|
+
if (path.startsWith(dir) && (rule === null || dir.length > rule.from.length - 1)) rule = entry;
|
|
239
|
+
} else if (entry.from === pathname || entry.from === path) {
|
|
240
|
+
return { from: entry.from, to: entry.to, location: entry.to };
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
if (rule === null) return null;
|
|
244
|
+
const location = ruleLocation(rule.to, path.slice(rule.from.length - 1));
|
|
245
|
+
return location === null ? null : { from: rule.from, to: rule.to, location };
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* The distinct directory depths of a set's rules, deepest first: `/blogs/news/*` is depth 2.
|
|
250
|
+
* The CloudFront function probes exactly these prefixes, so it reads nothing for a set without
|
|
251
|
+
* rules and one key per depth for a set with them.
|
|
252
|
+
* @param {readonly {from: string}[]} redirects
|
|
253
|
+
* @returns {number[]}
|
|
254
|
+
*/
|
|
255
|
+
export function redirectRuleDepths(redirects) {
|
|
256
|
+
const depths = new Set();
|
|
257
|
+
for (const { from } of redirects ?? []) {
|
|
258
|
+
if (isRedirectRule(from)) depths.add(from.split("/").filter(Boolean).length - 1);
|
|
259
|
+
}
|
|
260
|
+
return [...depths].sort((a, b) => b - a);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Turn a content collection's `redirects.json` (old slug -> new slug) into declared redirects
|
|
265
|
+
* under that collection's route prefix.
|
|
266
|
+
* @param {string} collection e.g. "blog"
|
|
267
|
+
* @param {Record<string, unknown>} rows
|
|
268
|
+
* @returns {DeclaredRedirect[]}
|
|
269
|
+
*/
|
|
270
|
+
export function redirectsFromCollection(collection, rows) {
|
|
271
|
+
if (!rows || typeof rows !== "object") return [];
|
|
272
|
+
/** @type {DeclaredRedirect[]} */
|
|
273
|
+
const out = [];
|
|
274
|
+
for (const [oldSlug, newSlug] of Object.entries(rows)) {
|
|
275
|
+
if (typeof newSlug !== "string" || !oldSlug || !newSlug) continue;
|
|
276
|
+
out.push({
|
|
277
|
+
from: `/${collection}/${oldSlug}/`,
|
|
278
|
+
to: `/${collection}/${newSlug}/`,
|
|
279
|
+
source: `content/${collection}/redirects.json`,
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
return out;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Turn the tenant-wide `content/redirects.json` map into root-relative declarations.
|
|
287
|
+
* Its keys and same-origin values are repository paths without a leading slash; an
|
|
288
|
+
* explicit HTTPS destination remains absolute and is validated by `buildRedirectSet`.
|
|
289
|
+
* A key ending in `/*` declares a rule (`"blogs/news/*": "blog/*"`).
|
|
290
|
+
* @param {Record<string, unknown>} rows
|
|
291
|
+
* @returns {DeclaredRedirect[]}
|
|
292
|
+
*/
|
|
293
|
+
export function redirectsFromSiteMap(rows) {
|
|
294
|
+
if (!rows || typeof rows !== "object" || Array.isArray(rows)) {
|
|
295
|
+
throw new TypeError(
|
|
296
|
+
"content/redirects.json must be an object mapping relative paths to destinations",
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
/** @type {DeclaredRedirect[]} */
|
|
300
|
+
const out = [];
|
|
301
|
+
for (const [from, rawTo] of Object.entries(rows)) {
|
|
302
|
+
if (
|
|
303
|
+
!from || from.startsWith("/") || typeof rawTo !== "string" || !rawTo || rawTo.startsWith("/")
|
|
304
|
+
) {
|
|
305
|
+
throw new TypeError(
|
|
306
|
+
"content/redirects.json keys and same-origin destinations must be non-empty paths without a leading slash",
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
const rootedFrom = `/${from}`;
|
|
310
|
+
const isExternal = /^[A-Za-z][A-Za-z0-9+.-]*:/.test(rawTo);
|
|
311
|
+
const rootedTo = isExternal ? rawTo : `/${rawTo}`;
|
|
312
|
+
const rule = rootedFrom.includes("*");
|
|
313
|
+
out.push({
|
|
314
|
+
from: normalizeRedirectSource(rootedFrom) ?? rootedFrom,
|
|
315
|
+
to: isExternal ? rawTo : (rule ? normalizeRuleTarget(rootedTo) : normalizeRedirectPath(rootedTo)) ?? rootedTo,
|
|
316
|
+
source: SITE_REDIRECTS_PATH,
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
return out;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* A content collection's redirect declaration, as it appears in a composed bundle's file list.
|
|
324
|
+
* `content/blog/redirects.json` declares redirects for the `blog` collection.
|
|
325
|
+
*/
|
|
326
|
+
export const COLLECTION_REDIRECTS_PATH = /^content\/([A-Za-z0-9][A-Za-z0-9_-]*)\/redirects\.json$/;
|
|
327
|
+
|
|
328
|
+
/** The tenant-wide redirect declaration, for legacy URLs that belong to no single collection. */
|
|
329
|
+
export const SITE_REDIRECTS_PATH = "content/redirects.json";
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Does this composed file declare redirects the composition lifts into `routes.aliases`?
|
|
333
|
+
* @param {string} path
|
|
334
|
+
* @returns {boolean}
|
|
335
|
+
*/
|
|
336
|
+
export function isRedirectDeclarationPath(path) {
|
|
337
|
+
return path === SITE_REDIRECTS_PATH || COLLECTION_REDIRECTS_PATH.test(path ?? "");
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Declared redirects for every content collection present in a composed revision.
|
|
342
|
+
*
|
|
343
|
+
* Reads the COMPOSED files rather than a working tree, so the redirects are bound to the same
|
|
344
|
+
* revision as the pages they point at — a rename and its redirect can never publish apart.
|
|
345
|
+
*
|
|
346
|
+
* @param {{path: string, json: unknown}[]} entries composed files, already parsed
|
|
347
|
+
* @returns {DeclaredRedirect[]}
|
|
348
|
+
*/
|
|
349
|
+
export function declaredRedirectsFromContent(entries) {
|
|
350
|
+
/** @type {DeclaredRedirect[]} */
|
|
351
|
+
const out = [];
|
|
352
|
+
for (const entry of entries ?? []) {
|
|
353
|
+
if (entry?.path === SITE_REDIRECTS_PATH) {
|
|
354
|
+
out.push(...redirectsFromSiteMap(/** @type {Record<string, unknown>} */ (entry.json)));
|
|
355
|
+
continue;
|
|
356
|
+
}
|
|
357
|
+
const match = COLLECTION_REDIRECTS_PATH.exec(entry?.path ?? "");
|
|
358
|
+
if (!match) continue;
|
|
359
|
+
out.push(
|
|
360
|
+
...redirectsFromCollection(
|
|
361
|
+
/** @type {string} */ (match[1]),
|
|
362
|
+
/** @type {Record<string, unknown>} */ (entry.json),
|
|
363
|
+
),
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
return out;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Turn publication-space aliases into declared redirects. Only `redirect-308` aliases produce a
|
|
371
|
+
* redirect; a `render` alias is served, not redirected.
|
|
372
|
+
* @param {{from?: string, behavior?: string, to?: string}[]} aliases
|
|
373
|
+
* @param {string} [source]
|
|
374
|
+
* @returns {DeclaredRedirect[]}
|
|
375
|
+
*/
|
|
376
|
+
export function redirectsFromAliases(aliases, source = "publication-space routes.aliases") {
|
|
377
|
+
if (!Array.isArray(aliases)) return [];
|
|
378
|
+
/** @type {DeclaredRedirect[]} */
|
|
379
|
+
const out = [];
|
|
380
|
+
for (const alias of aliases) {
|
|
381
|
+
if (!alias || alias.behavior !== "redirect-308") continue;
|
|
382
|
+
if (typeof alias.from !== "string" || typeof alias.to !== "string") continue;
|
|
383
|
+
out.push({ from: alias.from, to: alias.to, source });
|
|
384
|
+
}
|
|
385
|
+
return out;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* `resolveRedirect` over a whole set at once: exact entries in a map, rules in a list (a set has
|
|
390
|
+
* few rules and may have thousands of exact entries).
|
|
391
|
+
* @param {readonly {from: string, to: string}[]} entries
|
|
392
|
+
* @returns {(pathname: string) => {from: string, to: string, location: string} | null}
|
|
393
|
+
*/
|
|
394
|
+
export function redirectResolver(entries) {
|
|
395
|
+
/** @type {Map<string, string>} */
|
|
396
|
+
const exact = new Map();
|
|
397
|
+
/** @type {{from: string, to: string}[]} */
|
|
398
|
+
const rules = [];
|
|
399
|
+
for (const entry of entries ?? []) {
|
|
400
|
+
if (isRedirectRule(entry.from)) rules.push(entry);
|
|
401
|
+
else exact.set(entry.from, entry.to);
|
|
402
|
+
}
|
|
403
|
+
return (pathname) => {
|
|
404
|
+
for (const from of [pathname, redirectRequestPath(pathname)]) {
|
|
405
|
+
const to = exact.get(from);
|
|
406
|
+
if (to !== undefined) return { from, to, location: to };
|
|
407
|
+
}
|
|
408
|
+
return resolveRedirect(rules, pathname);
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Follow a location through the declared set until nothing redirects it again, so a chain
|
|
414
|
+
* (a -> b, b -> c, or a -> a path a rule covers) resolves to one hop. A chain costs an extra round
|
|
415
|
+
* trip on every visit and dilutes the signal the redirect exists to preserve. A cycle is
|
|
416
|
+
* unresolvable and refuses.
|
|
417
|
+
* @param {(pathname: string) => string | null} resolve
|
|
418
|
+
* @param {string} from
|
|
419
|
+
* @param {string} to
|
|
420
|
+
* @returns {{to: string} | {cycle: string[]}}
|
|
421
|
+
*/
|
|
422
|
+
function collapseChain(resolve, from, to) {
|
|
423
|
+
const seen = [from];
|
|
424
|
+
let location = to;
|
|
425
|
+
while (location.startsWith("/")) {
|
|
426
|
+
const next = resolve(location);
|
|
427
|
+
if (next === null) break;
|
|
428
|
+
if (seen.includes(location)) return { cycle: [...seen, location] };
|
|
429
|
+
seen.push(location);
|
|
430
|
+
location = next;
|
|
431
|
+
}
|
|
432
|
+
if (seen.includes(location)) return { cycle: [...seen, location] };
|
|
433
|
+
return { to: location };
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* The KVS bytes one entry occupies on the CloudFront plane: its key and its value.
|
|
438
|
+
* @param {{from: string, to: string}} entry
|
|
439
|
+
*/
|
|
440
|
+
function entryBytes(entry) {
|
|
441
|
+
return { key: REDIRECT_KEY_OVERHEAD_BYTES + byteLength(entry.from), value: byteLength(entry.to) };
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* Build the substrate-neutral redirect set from every declared source.
|
|
446
|
+
* @param {DeclaredRedirect[]} declared
|
|
447
|
+
* @param {number} [budget]
|
|
448
|
+
* @returns {RedirectSet}
|
|
449
|
+
*/
|
|
450
|
+
export function buildRedirectSet(declared, budget = REDIRECT_MAP_BUDGET_BYTES) {
|
|
451
|
+
/** @type {string[]} */
|
|
452
|
+
const violations = [];
|
|
453
|
+
/** @type {Map<string, string>} */
|
|
454
|
+
const direct = new Map();
|
|
455
|
+
/** @type {Map<string, string>} */
|
|
456
|
+
const origin = new Map();
|
|
457
|
+
|
|
458
|
+
for (const entry of declared) {
|
|
459
|
+
const from = normalizeRedirectSource(entry.from);
|
|
460
|
+
if (from === null) {
|
|
461
|
+
violations.push(
|
|
462
|
+
`${entry.source}: redirect source is not a usable request path (a rule is "<dir>/*", never the whole site)`,
|
|
463
|
+
);
|
|
464
|
+
continue;
|
|
465
|
+
}
|
|
466
|
+
const rule = isRedirectRule(from);
|
|
467
|
+
const to = rule ? normalizeRuleTarget(entry.to) : normalizeRedirectDestination(entry.to);
|
|
468
|
+
if (to === null) {
|
|
469
|
+
violations.push(
|
|
470
|
+
rule
|
|
471
|
+
? `${entry.source}: rule "${from}" needs a same-origin target path, optionally ending in "/*"`
|
|
472
|
+
: `${entry.source}: redirect target is not a usable destination path`,
|
|
473
|
+
);
|
|
474
|
+
continue;
|
|
475
|
+
}
|
|
476
|
+
if (from === to) {
|
|
477
|
+
violations.push(`${entry.source}: "${from}" redirects to itself`);
|
|
478
|
+
continue;
|
|
479
|
+
}
|
|
480
|
+
const existing = direct.get(from);
|
|
481
|
+
if (existing !== undefined && existing !== to) {
|
|
482
|
+
violations.push(
|
|
483
|
+
`"${from}" is declared twice with different targets ("${existing}" in ${origin.get(from)}, "${to}" in ${entry.source})`,
|
|
484
|
+
);
|
|
485
|
+
continue;
|
|
486
|
+
}
|
|
487
|
+
direct.set(from, to);
|
|
488
|
+
origin.set(from, entry.source);
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
const declaredEntries = [...direct].map(([from, to]) => ({ from, to }));
|
|
492
|
+
const rules = declaredEntries.filter((entry) => isRedirectRule(entry.from));
|
|
493
|
+
|
|
494
|
+
// A capturing rule's target directory must not sit under a rule's directory: every capture would
|
|
495
|
+
// then take a second hop, and a rule into its own directory never terminates. (A fixed target
|
|
496
|
+
// is collapsed through the rules below like any other chain.)
|
|
497
|
+
for (const rule of rules) {
|
|
498
|
+
if (!rule.to.endsWith("/*")) continue;
|
|
499
|
+
const target = rule.to.slice(0, -1);
|
|
500
|
+
for (const other of rules) {
|
|
501
|
+
if (target.startsWith(other.from.slice(0, -1))) {
|
|
502
|
+
violations.push(
|
|
503
|
+
`rule "${rule.from}" -> "${rule.to}" lands under rule "${other.from}", so every match would redirect twice`,
|
|
504
|
+
);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
// An exact entry under a capturing rule's TARGET would make that capture a second hop
|
|
510
|
+
// (`/blogs/news/old/` -> `/blog/old/` -> `/blog/new/`). The composed one-hop entry is added
|
|
511
|
+
// beside it, so every request still takes exactly one redirect. A rule into the site root
|
|
512
|
+
// composes nothing: every exact entry is under `/`, and the paths it would add (`/pages/<any
|
|
513
|
+
// legacy path>/`) are not URLs anyone published.
|
|
514
|
+
/** @type {Map<string, string>} */
|
|
515
|
+
const composed = new Map();
|
|
516
|
+
for (const rule of rules) {
|
|
517
|
+
if (!rule.to.endsWith("/*") || rule.to === "/*") continue;
|
|
518
|
+
const target = rule.to.slice(0, -1);
|
|
519
|
+
const dir = rule.from.slice(0, -1);
|
|
520
|
+
for (const entry of declaredEntries) {
|
|
521
|
+
if (isRedirectRule(entry.from) || !entry.from.startsWith(target)) continue;
|
|
522
|
+
const from = `${dir}${entry.from.slice(target.length)}`;
|
|
523
|
+
if (!direct.has(from)) composed.set(from, entry.to);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
const entries = [...declaredEntries, ...[...composed].map(([from, to]) => ({ from, to }))];
|
|
527
|
+
const resolver = redirectResolver(entries);
|
|
528
|
+
/** @param {string} pathname */
|
|
529
|
+
const resolve = (pathname) => resolver(pathname)?.location ?? null;
|
|
530
|
+
|
|
531
|
+
/** @type {{from: string, to: string}[]} */
|
|
532
|
+
const redirects = [];
|
|
533
|
+
for (const entry of [...entries].sort((a, b) => a.from.localeCompare(b.from))) {
|
|
534
|
+
if (isRedirectRule(entry.from) && entry.to.endsWith("/*")) {
|
|
535
|
+
redirects.push(entry);
|
|
536
|
+
continue;
|
|
537
|
+
}
|
|
538
|
+
const resolved = collapseChain(resolve, entry.from, entry.to);
|
|
539
|
+
if ("cycle" in resolved) {
|
|
540
|
+
violations.push(`redirect cycle: ${resolved.cycle.join(" -> ")}`);
|
|
541
|
+
continue;
|
|
542
|
+
}
|
|
543
|
+
redirects.push({ from: entry.from, to: resolved.to });
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
const depths = redirectRuleDepths(redirects);
|
|
547
|
+
if (depths.length > REDIRECT_RULE_MAX_DEPTHS) {
|
|
548
|
+
violations.push(
|
|
549
|
+
`rules span ${depths.length} directory depths; the edge reads one key per depth, so at most ${REDIRECT_RULE_MAX_DEPTHS} are allowed`,
|
|
550
|
+
);
|
|
551
|
+
}
|
|
552
|
+
let bytes = 0;
|
|
553
|
+
for (const entry of redirects) {
|
|
554
|
+
const size = entryBytes(entry);
|
|
555
|
+
if (size.key > KVS_KEY_LIMIT_BYTES || size.value > KVS_VALUE_LIMIT_BYTES) {
|
|
556
|
+
violations.push(
|
|
557
|
+
`"${entry.from}" is too long for one edge key-value entry (key ${size.key}/${KVS_KEY_LIMIT_BYTES} bytes, target ${size.value}/${KVS_VALUE_LIMIT_BYTES} bytes)`,
|
|
558
|
+
);
|
|
559
|
+
}
|
|
560
|
+
bytes += size.key + size.value;
|
|
561
|
+
}
|
|
562
|
+
if (bytes > budget) {
|
|
563
|
+
violations.push(
|
|
564
|
+
`redirect set is ${bytes} bytes, over the ${budget}-byte edge budget (${redirects.length} redirects) — collapse a URL family into one "<dir>/*" rule`,
|
|
565
|
+
);
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
return {
|
|
569
|
+
ok: violations.length === 0,
|
|
570
|
+
redirects,
|
|
571
|
+
violations,
|
|
572
|
+
bytes,
|
|
573
|
+
budget,
|
|
574
|
+
rules: redirects.filter((entry) => isRedirectRule(entry.from)).length,
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
/**
|
|
579
|
+
* The declared set as publication-space aliases — the shape `routes.aliases` already carries and
|
|
580
|
+
* `publicationRedirects()` (scripts/publish/lib/publication-space-release.mjs) already consumes on
|
|
581
|
+
* its way into the live pointer. Emitting this rather than a parallel structure is what keeps
|
|
582
|
+
* redirects ONE contract: the edge function, the pointer value, and the declaration all agree
|
|
583
|
+
* because there is only one of them.
|
|
584
|
+
*
|
|
585
|
+
* An alias is a mount: it redirects its path and everything under it, appending the remainder to
|
|
586
|
+
* the target. A capturing rule (`/x/*` -> `/y/*`) is exactly that mount (`/x` -> `/y`). A rule
|
|
587
|
+
* that sends a family to one page, or captures into the site root, has no mount form and throws.
|
|
588
|
+
*
|
|
589
|
+
* Returns an empty array when the set refused, so a caller that ignores `ok` cannot publish a
|
|
590
|
+
* half-built topology.
|
|
591
|
+
* @param {RedirectSet} set
|
|
592
|
+
* @returns {{from: string, behavior: "redirect-308", to: string}[]}
|
|
593
|
+
*/
|
|
594
|
+
export function aliasesFromRedirectSet(set) {
|
|
595
|
+
if (!set.ok) return [];
|
|
596
|
+
return set.redirects.map(({ from, to }) => {
|
|
597
|
+
if (!isRedirectRule(from)) {
|
|
598
|
+
return { from, behavior: /** @type {"redirect-308"} */ ("redirect-308"), to };
|
|
599
|
+
}
|
|
600
|
+
if (!to.endsWith("/*") || to === "/*") {
|
|
601
|
+
throw new TypeError(`rule "${from}" -> "${to}" has no publication-space alias form`);
|
|
602
|
+
}
|
|
603
|
+
return {
|
|
604
|
+
from: from.slice(0, -2),
|
|
605
|
+
behavior: /** @type {"redirect-308"} */ ("redirect-308"),
|
|
606
|
+
to: to.slice(0, -2),
|
|
607
|
+
};
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
/**
|
|
612
|
+
* @param {RedirectSet} set
|
|
613
|
+
* @returns {string}
|
|
614
|
+
*/
|
|
615
|
+
export function formatRedirectSetReport(set) {
|
|
616
|
+
const lines = [];
|
|
617
|
+
lines.push(
|
|
618
|
+
`== Redirect materialization == ${set.redirects.length} redirect(s) (${set.rules} rule(s)), ${set.bytes}/${set.budget} bytes`,
|
|
619
|
+
);
|
|
620
|
+
for (const violation of set.violations) lines.push(` REFUSE: ${violation}`);
|
|
621
|
+
lines.push(
|
|
622
|
+
set.ok
|
|
623
|
+
? `OK — every declared redirect materializes at ${REDIRECT_STATUS}.`
|
|
624
|
+
: `REFUSED — ${set.violations.length} violation(s); nothing emitted.`,
|
|
625
|
+
);
|
|
626
|
+
return lines.join("\n");
|
|
627
|
+
}
|
|
@@ -23,6 +23,9 @@ interface Props {
|
|
|
23
23
|
jsonLd?: JsonLd | JsonLd[];
|
|
24
24
|
/** Alternate RSS/Atom feeds this page advertises (e.g. a collection's feed). */
|
|
25
25
|
feeds?: { title: string; href: string }[];
|
|
26
|
+
/** A paginated collection's siblings — path-only (like `path`), joined to the
|
|
27
|
+
* same canonical base. Absent fields emit no link (first/last page). */
|
|
28
|
+
pagination?: { prev?: string; next?: string };
|
|
26
29
|
// --- B1.6 article SEO hardening (all additive/optional) ---
|
|
27
30
|
/** Open Graph `article:*` meta; emitted only when `ogType === "article"`. */
|
|
28
31
|
article?: ArticleMeta;
|
|
@@ -51,6 +54,7 @@ const {
|
|
|
51
54
|
noindex = false,
|
|
52
55
|
jsonLd,
|
|
53
56
|
feeds = [],
|
|
57
|
+
pagination,
|
|
54
58
|
article,
|
|
55
59
|
imageAlt,
|
|
56
60
|
imageWidth,
|
|
@@ -84,6 +88,10 @@ const articleMeta = articleSeoMetaTags({
|
|
|
84
88
|
|
|
85
89
|
const graphs = jsonLd ? (Array.isArray(jsonLd) ? jsonLd : [jsonLd]) : [];
|
|
86
90
|
const alternateLinks = withXDefault(alternates);
|
|
91
|
+
const paginationLinks = [
|
|
92
|
+
...(pagination?.prev ? [{ rel: "prev", href: new URL(pagination.prev, canonicalBase + "/").toString() }] : []),
|
|
93
|
+
...(pagination?.next ? [{ rel: "next", href: new URL(pagination.next, canonicalBase + "/").toString() }] : []),
|
|
94
|
+
];
|
|
87
95
|
---
|
|
88
96
|
|
|
89
97
|
<title>{fullTitle}</title>
|
|
@@ -91,6 +99,10 @@ const alternateLinks = withXDefault(alternates);
|
|
|
91
99
|
<link rel="canonical" href={canonical} />
|
|
92
100
|
{noindex && <meta name="robots" content="noindex,nofollow" />}
|
|
93
101
|
|
|
102
|
+
{
|
|
103
|
+
paginationLinks.map((l) => <link rel={l.rel} href={l.href} />)
|
|
104
|
+
}
|
|
105
|
+
|
|
94
106
|
{
|
|
95
107
|
feeds.map((f) => (
|
|
96
108
|
<link rel="alternate" type="application/rss+xml" title={f.title} href={f.href} />
|