ferst-core 0.16.0 → 0.17.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/integration.mjs CHANGED
@@ -1,253 +1,319 @@
1
- /**
2
- * ferst() — the Astro integration that makes a client site's ROUTING package-owned.
3
- *
4
- * A thin client adds `integrations: [ferst()]` to astro.config.mjs and carries
5
- * ZERO page files: the page catch-all (which also owns the home page), the
6
- * calendar route and the blog routes are injected from ferst-core. A routing fix
7
- * then ships as a package bump + rebuild — the same channel component fixes travel
8
- * through — instead of being hand-copied into every client repo.
9
- *
10
- * Irreducible caveat (Astro 5): content collections must be defined in the CONSUMER,
11
- * so the one-line `src/content/config.ts` re-export stays. "Zero page files" is
12
- * reachable; "zero files" is not. Explicit client `.astro` routes still win over
13
- * these injected ones, so a one-off hand-built page is always possible.
14
- *
15
- * The blog routes (posts index / individual post / tag pages) inject as a set:
16
- * the listing components (NewsList, PostsFilter) now live in core, so `/posts`,
17
- * `/posts/[...slug]` and `/posts/tag/[...tag]` all ship from the package. Turn
18
- * the whole capability off with `blog: false` (then a client wanting posts hand-
19
- * builds its own routes over `ferst-core/lib/posts`).
20
- *
21
- * @param {object} [options]
22
- * @param {boolean} [options.calendar=true] Inject the `/calendar` route.
23
- * @param {boolean} [options.blog=true] Inject the blog routes (`/posts`,
24
- * `/posts/[...slug]`, `/posts/tag/[...tag]`).
25
- * @param {boolean} [options.accessibility=true] Inject `/accessibility`, the accessibility statement
26
- * every footer links to (content/accessibility.ts). Skipped when the site has its own
27
- * `src/pages/accessibility.astro`.
28
- * @param {boolean} [options.cookiePolicy=true] Inject `/cookie-policy` (the page the cookie
29
- * banner links to), generated from the blocks the site uses. Skipped automatically when
30
- * the site has its own `src/pages/cookie-policy.astro`.
31
- * @param {boolean} [options.notFound=true] Inject the "page not found" page (`/404` →
32
- * `404.html`). Cloudflare Pages answers a missing address with it and a real 404 status;
33
- * without one it serves the home page with a 200 (a "soft 404"). Skipped automatically
34
- * when the site has its own `src/pages/404.astro` or `public/404.html`.
35
- * @param {boolean} [options.icons=true] The browser icon (lib/site-icon.mjs): `/favicon.ico` and
36
- * `/apple-touch-icon.png` in every build, from the icon set in the CMS, else a roughly
37
- * square logo, else the site's initial in its brand colour. A site's own files in
38
- * `public/` win.
39
- * @param {boolean} [options.seo=true] Search signals (lib/seo.mjs): `/robots.txt`, and once
40
- * `ferst-site.json` has a `siteUrl`, `/sitemap.xml`, a canonical address on every page,
41
- * structured data and link previews, and (with the blog) `/posts/rss.xml`. Every build's
42
- * internal links to a page point at its own address (`/about-us/`), not at a redirect.
43
- * A test copy (any branch but the production one) is marked noindex on every file. A
44
- * site's own `public/robots.txt` or `public/sitemap.xml` wins.
45
- * @returns {import('astro').AstroIntegration}
46
- */
47
- import { copyFileSync, createReadStream, existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
48
- import { deploymentBranch } from './bin/cms-config.mjs';
49
- import { linksToPageAddresses, seoContext, withNoindexHeader } from './lib/seo.mjs';
50
- import { writeSiteIcons } from './lib/site-icon.mjs';
51
- import { publicImageSizes } from './lib/image-size.mjs';
52
-
53
- /** Every page of a build (its .html files, at any depth), as file URLs. */
54
- function htmlFiles(dir) {
55
- return readdirSync(dir, { withFileTypes: true }).flatMap((entry) =>
56
- entry.isDirectory()
57
- ? htmlFiles(new URL(`${encodeURIComponent(entry.name)}/`, dir))
58
- : entry.name.endsWith('.html')
59
- ? [new URL(encodeURIComponent(entry.name), dir)]
60
- : [],
61
- );
62
- }
63
-
64
- /** The self-hosted fonts the engine's CSS requests (/fonts/*.woff2), shipped in the package. */
65
- const FONTS_DIR = new URL('./fonts/', import.meta.url);
66
- const FONT_URL = /^\/fonts\/([a-z0-9-]+\.woff2)(?:\?.*)?$/;
67
-
68
- /**
69
- * ferstFonts() — serves the engine's self-hosted fonts in `astro dev` and copies them into
70
- * every build's `/fonts/`. Included in ferst(); usable on its own (the showcase). A file
71
- * the site already has in its own `public/fonts/` is never overwritten.
72
- * @returns {import('astro').AstroIntegration}
73
- */
74
- export function ferstFonts() {
75
- return {
76
- name: 'ferst-fonts',
77
- hooks: {
78
- 'astro:server:setup': ({ server }) => {
79
- server.middlewares.use((req, res, next) => {
80
- const m = req.url && req.url.match(FONT_URL);
81
- if (!m) return next();
82
- const file = new URL(m[1], FONTS_DIR);
83
- if (!existsSync(file)) return next();
84
- res.setHeader('Content-Type', 'font/woff2');
85
- res.setHeader('Cache-Control', 'public, max-age=31536000, immutable');
86
- createReadStream(file).pipe(res);
87
- });
88
- },
89
- 'astro:build:done': async ({ dir }) => {
90
- const out = new URL('fonts/', dir);
91
- mkdirSync(out, { recursive: true });
92
- for (const name of readdirSync(FONTS_DIR)) {
93
- if (!name.endsWith('.woff2')) continue;
94
- const dest = new URL(name, out);
95
- if (!existsSync(dest)) copyFileSync(new URL(name, FONTS_DIR), dest);
96
- }
97
- },
98
- },
99
- };
100
- }
101
-
102
- /** The site's `ferst-site.json`, or null (no file, or not JSON). */
103
- function readSiteMeta(root) {
104
- if (!root) return null;
105
- try {
106
- return JSON.parse(readFileSync(new URL('ferst-site.json', root), 'utf8'));
107
- } catch {
108
- return null;
109
- }
110
- }
111
-
112
- /** Does the site ship this file in its own `public/`? (Its own file wins over ours.) */
113
- const hasPublicFile = (root, name) => Boolean(root) && existsSync(new URL(`public/${name}`, root));
114
-
115
- export default function ferst(options = {}) {
116
- const { calendar = true, blog = true, cookiePolicy = true, accessibility = true, notFound = true, icons = true, seo = true } = options;
117
- const fonts = ferstFonts().hooks;
118
- /** This build's search context, decided at setup and used again when the build is done. */
119
- let search = null;
120
- /** The site's folder, for the content and public files the build-done steps read. */
121
- let siteRoot = null;
122
- return {
123
- name: 'ferst',
124
- hooks: {
125
- 'astro:config:setup': ({ injectRoute, updateConfig, config }) => {
126
- siteRoot = config?.root ?? null;
127
- // Pages + home: the reserved `index` entry renders at `/`, every other
128
- // `pages` entry at its own slug (see lib/pages.ts `getSiteRoutes`).
129
- injectRoute({
130
- pattern: '/[...slug]',
131
- entrypoint: 'ferst-core/routes/[...slug].astro',
132
- });
133
- if (calendar) {
134
- injectRoute({
135
- pattern: '/calendar',
136
- entrypoint: 'ferst-core/routes/calendar/index.astro',
137
- });
138
- }
139
- if (blog) {
140
- // The posts capability: index, individual post, and tag pages. Reads
141
- // the `posts`/`tags`/`postsSettings` collections (content/collections.ts).
142
- injectRoute({
143
- pattern: '/posts',
144
- entrypoint: 'ferst-core/routes/posts/index.astro',
145
- });
146
- injectRoute({
147
- pattern: '/posts/[...slug]',
148
- entrypoint: 'ferst-core/routes/posts/[...slug].astro',
149
- });
150
- injectRoute({
151
- pattern: '/posts/tag/[...tag]',
152
- entrypoint: 'ferst-core/routes/posts/tag/[...tag].astro',
153
- });
154
- // Index pages 2+ (page 1 is /posts; tag pages paginate inside their own route).
155
- injectRoute({
156
- pattern: '/posts/page/[page]',
157
- entrypoint: 'ferst-core/routes/posts/page/[page].astro',
158
- });
159
- }
160
- // The cookie banner links here on every page, so it must always resolve. A site's
161
- // own hand-built page wins (Astro would otherwise see two routes for one path).
162
- const bespoke = config?.root && existsSync(new URL('src/pages/cookie-policy.astro', config.root));
163
- if (cookiePolicy && !bespoke) {
164
- injectRoute({
165
- pattern: '/cookie-policy',
166
- entrypoint: 'ferst-core/routes/cookie-policy.astro',
167
- });
168
- }
169
- // Every footer links to the accessibility statement. The site's own page wins.
170
- const ownStatement = config?.root && existsSync(new URL('src/pages/accessibility.astro', config.root));
171
- if (accessibility && !ownStatement) {
172
- injectRoute({ pattern: '/accessibility', entrypoint: 'ferst-core/routes/accessibility.astro' });
173
- }
174
- // A missing address must answer 404, not the home page (Cloudflare Pages serves the
175
- // home page with a 200 when a build has no 404.html). The site's own page wins.
176
- const ownNotFound = config?.root && existsSync(new URL('src/pages/404.astro', config.root));
177
- if (notFound && !ownNotFound && !hasPublicFile(config?.root, '404.html')) {
178
- injectRoute({ pattern: '/404', entrypoint: 'ferst-core/routes/404.astro' });
179
- }
180
- // Which optional routes this site has, for the pages that link to them (the sitemap,
181
- // the "page not found" page).
182
- updateConfig?.({
183
- vite: {
184
- define: {
185
- 'import.meta.env.FERST_ROUTES_BLOG': JSON.stringify(blog),
186
- 'import.meta.env.FERST_ROUTES_CALENDAR': JSON.stringify(calendar),
187
- 'import.meta.env.FERST_ROUTES_ACCESSIBILITY': JSON.stringify(accessibility),
188
- // Each of the site's own images with its width and height, so a page keeps every
189
- // picture's shape before it loads (lib/image-sizes.ts).
190
- 'import.meta.env.FERST_IMAGE_SIZES': JSON.stringify(publicImageSizes(config?.root)),
191
- },
192
- },
193
- });
194
- // Search signals: the site's address and whether this deploy is the live site (the
195
- // production branch) or a test copy, the same rule the CMS uses for its git-proxy.
196
- if (seo) {
197
- const root = config?.root;
198
- search = seoContext({ siteMeta: readSiteMeta(root), branch: deploymentBranch(), env: process.env });
199
- updateConfig?.({
200
- ...(search.siteUrl && !config?.site ? { site: search.siteUrl } : {}),
201
- vite: {
202
- define: {
203
- 'import.meta.env.FERST_SITE_URL': JSON.stringify(search.siteUrl ?? ''),
204
- 'import.meta.env.FERST_SEO_PRODUCTION': JSON.stringify(search.production),
205
- },
206
- },
207
- });
208
- if (!hasPublicFile(root, 'robots.txt')) {
209
- injectRoute({ pattern: '/robots.txt', entrypoint: 'ferst-core/routes/robots.txt.ts' });
210
- }
211
- // A sitemap lists absolute addresses, so it needs the site's address.
212
- if (search.siteUrl && !hasPublicFile(root, 'sitemap.xml')) {
213
- injectRoute({ pattern: '/sitemap.xml', entrypoint: 'ferst-core/routes/sitemap.xml.ts' });
214
- }
215
- // The news feed lists absolute addresses too.
216
- if (search.siteUrl && blog) {
217
- injectRoute({ pattern: '/posts/rss.xml', entrypoint: 'ferst-core/routes/posts/rss.xml.ts' });
218
- }
219
- }
220
- },
221
- // The engine's self-hosted fonts: served in dev, copied into every build.
222
- 'astro:server:setup': fonts['astro:server:setup'],
223
- 'astro:build:done': async (ctx) => {
224
- await fonts['astro:build:done'](ctx);
225
- // Internal links point at each page's one address, never at a redirect (lib/seo.mjs).
226
- if (seo) {
227
- const isPage = (path) => {
228
- try {
229
- return existsSync(new URL(`.${path}index.html`, ctx.dir));
230
- } catch {
231
- return false;
232
- }
233
- };
234
- for (const file of htmlFiles(ctx.dir)) {
235
- const html = readFileSync(file, 'utf8');
236
- const linked = linksToPageAddresses(html, isPage);
237
- if (linked !== html) writeFileSync(file, linked);
238
- }
239
- }
240
- // A test copy is noindex on every file: pages, and the PDFs and images beside them.
241
- if (search && !search.production) {
242
- const file = new URL('_headers', ctx.dir);
243
- writeFileSync(file, withNoindexHeader(existsSync(file) ? readFileSync(file, 'utf8') : ''));
244
- }
245
- // The browser icon: /favicon.ico and /apple-touch-icon.png (sharp comes with Astro).
246
- if (icons && siteRoot) {
247
- const sharp = await import('sharp').then((m) => m.default ?? m).catch(() => null);
248
- await writeSiteIcons({ root: siteRoot, outDir: ctx.dir, sharp, warn: (m) => (ctx.logger ? ctx.logger.warn(m) : console.warn(m)) });
249
- }
250
- },
251
- },
252
- };
253
- }
1
+ /**
2
+ * ferst() — the Astro integration that makes a client site's ROUTING package-owned.
3
+ *
4
+ * A thin client adds `integrations: [ferst()]` to astro.config.mjs and carries
5
+ * ZERO page files: the page catch-all (which also owns the home page), the
6
+ * calendar route and the blog routes are injected from ferst-core. A routing fix
7
+ * then ships as a package bump + rebuild — the same channel component fixes travel
8
+ * through — instead of being hand-copied into every client repo.
9
+ *
10
+ * Irreducible caveat (Astro 5): content collections must be defined in the CONSUMER,
11
+ * so the one-line `src/content/config.ts` re-export stays. "Zero page files" is
12
+ * reachable; "zero files" is not. Explicit client `.astro` routes still win over
13
+ * these injected ones, so a one-off hand-built page is always possible.
14
+ *
15
+ * The blog routes (posts index / individual post / tag pages) inject as a set:
16
+ * the listing components (NewsList, PostsFilter) now live in core, so `/posts`,
17
+ * `/posts/[...slug]` and `/posts/tag/[...tag]` all ship from the package. Turn
18
+ * the whole capability off with `blog: false` (then a client wanting posts hand-
19
+ * builds its own routes over `ferst-core/lib/posts`).
20
+ *
21
+ * @param {object} [options]
22
+ * @param {boolean} [options.calendar=true] Inject the `/calendar` route.
23
+ * @param {boolean} [options.blog=true] Inject the blog routes (`/posts`,
24
+ * `/posts/[...slug]`, `/posts/tag/[...tag]`).
25
+ * @param {boolean} [options.accessibility=true] Inject `/accessibility`, the accessibility statement
26
+ * every footer links to (content/accessibility.ts). Skipped when the site has its own
27
+ * `src/pages/accessibility.astro`.
28
+ * @param {boolean} [options.cookiePolicy=true] Inject `/cookie-policy` (the page the cookie
29
+ * banner links to), generated from the blocks the site uses. Skipped automatically when
30
+ * the site has its own `src/pages/cookie-policy.astro`.
31
+ * @param {boolean} [options.notFound=true] Inject the "page not found" page (`/404` →
32
+ * `404.html`). Cloudflare Pages answers a missing address with it and a real 404 status;
33
+ * without one it serves the home page with a 200 (a "soft 404"). Skipped automatically
34
+ * when the site has its own `src/pages/404.astro` or `public/404.html`.
35
+ * @param {boolean} [options.icons=true] The browser icon (lib/site-icon.mjs): `/favicon.ico` and
36
+ * `/apple-touch-icon.png` in every build, from the icon set in the CMS, else a roughly
37
+ * square logo, else the site's initial in its brand colour. A site's own files in
38
+ * `public/` win.
39
+ * @param {boolean} [options.seo=true] Search signals (lib/seo.mjs): `/robots.txt`, and once
40
+ * `ferst-site.json` has a `siteUrl`, `/sitemap.xml`, a canonical address on every page,
41
+ * structured data and link previews, and (with the blog) `/posts/rss.xml`. Every build's
42
+ * internal links to a page point at its own address (`/about-us/`), not at a redirect.
43
+ * A test copy (any branch but the production one) is marked noindex on every file. A
44
+ * site's own `public/robots.txt` or `public/sitemap.xml` wins.
45
+ * @param {boolean} [options.members] The Ferst Members module on the site: the header's account
46
+ * link and the members' `/account` page (signing in with an emailed code; skipped when the site
47
+ * has its own `src/pages/account.astro`). Left out: on when the
48
+ * platform says the site has Members (`GET /sites/<siteId>/modules`, asked once at setup), and
49
+ * off when the site has no `siteId` or the platform can't say (the build never fails for it).
50
+ * @returns {import('astro').AstroIntegration}
51
+ */
52
+ import { copyFileSync, createReadStream, existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
53
+ import { deploymentBranch } from './bin/cms-config.mjs';
54
+ import { linksToPageAddresses, seoContext, withNoindexHeader } from './lib/seo.mjs';
55
+ import { writeSiteIcons } from './lib/site-icon.mjs';
56
+ import { publicImageSizes } from './lib/image-size.mjs';
57
+ import react from '@astrojs/react';
58
+ import { resolveGitBase, resolveSiteId } from './lib/git-base.mjs';
59
+ import { fileURLToPath } from 'node:url';
60
+
61
+ /** Every page of a build (its .html files, at any depth), as file URLs. */
62
+ function htmlFiles(dir) {
63
+ return readdirSync(dir, { withFileTypes: true }).flatMap((entry) =>
64
+ entry.isDirectory()
65
+ ? htmlFiles(new URL(`${encodeURIComponent(entry.name)}/`, dir))
66
+ : entry.name.endsWith('.html')
67
+ ? [new URL(encodeURIComponent(entry.name), dir)]
68
+ : [],
69
+ );
70
+ }
71
+
72
+ /**
73
+ * ferstIslands() — the React renderer for the engine's island blocks (docs/BLOCK-ARCHITECTURE.md
74
+ * §8: app-like blocks, such as the Book block's month, may be React islands). Adds
75
+ * `@astrojs/react` unless the site already has it, so a client site needs no change. React's
76
+ * code reaches the browser only on a page with an island block. Included in ferst(); usable on
77
+ * its own (the showcase).
78
+ * @returns {import('astro').AstroIntegration}
79
+ */
80
+ export function ferstIslands() {
81
+ return {
82
+ name: 'ferst-islands',
83
+ hooks: {
84
+ 'astro:config:setup': ({ config, updateConfig }) => {
85
+ const has = (config?.integrations ?? []).some((i) => i?.name === '@astrojs/react');
86
+ if (!has) updateConfig?.({ integrations: [react()] });
87
+ },
88
+ },
89
+ };
90
+ }
91
+
92
+ /** The self-hosted fonts the engine's CSS requests (/fonts/*.woff2), shipped in the package. */
93
+ const FONTS_DIR = new URL('./fonts/', import.meta.url);
94
+ const FONT_URL = /^\/fonts\/([a-z0-9-]+\.woff2)(?:\?.*)?$/;
95
+
96
+ /**
97
+ * ferstFonts() — serves the engine's self-hosted fonts in `astro dev` and copies them into
98
+ * every build's `/fonts/`. Included in ferst(); usable on its own (the showcase). A file
99
+ * the site already has in its own `public/fonts/` is never overwritten.
100
+ * @returns {import('astro').AstroIntegration}
101
+ */
102
+ export function ferstFonts() {
103
+ return {
104
+ name: 'ferst-fonts',
105
+ hooks: {
106
+ 'astro:server:setup': ({ server }) => {
107
+ server.middlewares.use((req, res, next) => {
108
+ const m = req.url && req.url.match(FONT_URL);
109
+ if (!m) return next();
110
+ const file = new URL(m[1], FONTS_DIR);
111
+ if (!existsSync(file)) return next();
112
+ res.setHeader('Content-Type', 'font/woff2');
113
+ res.setHeader('Cache-Control', 'public, max-age=31536000, immutable');
114
+ createReadStream(file).pipe(res);
115
+ });
116
+ },
117
+ 'astro:build:done': async ({ dir }) => {
118
+ const out = new URL('fonts/', dir);
119
+ mkdirSync(out, { recursive: true });
120
+ for (const name of readdirSync(FONTS_DIR)) {
121
+ if (!name.endsWith('.woff2')) continue;
122
+ const dest = new URL(name, out);
123
+ if (!existsSync(dest)) copyFileSync(new URL(name, FONTS_DIR), dest);
124
+ }
125
+ },
126
+ },
127
+ };
128
+ }
129
+
130
+ /** The site's `ferst-site.json`, or null (no file, or not JSON). */
131
+ function readSiteMeta(root) {
132
+ if (!root) return null;
133
+ try {
134
+ return JSON.parse(readFileSync(new URL('ferst-site.json', root), 'utf8'));
135
+ } catch {
136
+ return null;
137
+ }
138
+ }
139
+
140
+ /**
141
+ * Whether the site has the Ferst Members module. An explicit `members` option wins; otherwise the
142
+ * platform says (`modules.members` in GET /sites/:id/modules). No site id, no answer, or a slow
143
+ * one: off, with a warning, so a build never waits on or fails for it.
144
+ */
145
+ export async function membersOn(option, { siteId, base, fetch: fetchFn = fetch, timeoutMs = 5000, warn = () => {} } = {}) {
146
+ if (typeof option === 'boolean') return option;
147
+ if (!siteId || !base) return false;
148
+ const url = `${base}/sites/${encodeURIComponent(siteId)}/modules`;
149
+ try {
150
+ const res = await fetchFn(url, { headers: { Accept: 'application/json' }, signal: AbortSignal.timeout(timeoutMs) });
151
+ if (!res.ok) {
152
+ warn(`ferst: the platform answered ${res.status} for ${url}; the members' account link is off for this build.`);
153
+ return false;
154
+ }
155
+ const data = await res.json();
156
+ return data?.modules?.members === true;
157
+ } catch (e) {
158
+ warn(`ferst: couldn't ask the platform whether this site has Members (${e instanceof Error ? e.message : e}); the account link is off for this build.`);
159
+ return false;
160
+ }
161
+ }
162
+
163
+ /** Does the site ship this file in its own `public/`? (Its own file wins over ours.) */
164
+ const hasPublicFile = (root, name) => Boolean(root) && existsSync(new URL(`public/${name}`, root));
165
+
166
+ export default function ferst(options = {}) {
167
+ const { calendar = true, blog = true, cookiePolicy = true, accessibility = true, notFound = true, icons = true, seo = true } = options;
168
+ const fonts = ferstFonts().hooks;
169
+ /** This build's search context, decided at setup and used again when the build is done. */
170
+ let search = null;
171
+ /** The site's folder, for the content and public files the build-done steps read. */
172
+ let siteRoot = null;
173
+ return {
174
+ name: 'ferst',
175
+ hooks: {
176
+ 'astro:config:setup': async ({ injectRoute, updateConfig, config, logger }) => {
177
+ siteRoot = config?.root ?? null;
178
+ // Island blocks (the Book block's month): React, registered by the engine.
179
+ ferstIslands().hooks['astro:config:setup']({ config, updateConfig });
180
+ // Pages + home: the reserved `index` entry renders at `/`, every other
181
+ // `pages` entry at its own slug (see lib/pages.ts `getSiteRoutes`).
182
+ injectRoute({
183
+ pattern: '/[...slug]',
184
+ entrypoint: 'ferst-core/routes/[...slug].astro',
185
+ });
186
+ if (calendar) {
187
+ injectRoute({
188
+ pattern: '/calendar',
189
+ entrypoint: 'ferst-core/routes/calendar/index.astro',
190
+ });
191
+ }
192
+ if (blog) {
193
+ // The posts capability: index, individual post, and tag pages. Reads
194
+ // the `posts`/`tags`/`postsSettings` collections (content/collections.ts).
195
+ injectRoute({
196
+ pattern: '/posts',
197
+ entrypoint: 'ferst-core/routes/posts/index.astro',
198
+ });
199
+ injectRoute({
200
+ pattern: '/posts/[...slug]',
201
+ entrypoint: 'ferst-core/routes/posts/[...slug].astro',
202
+ });
203
+ injectRoute({
204
+ pattern: '/posts/tag/[...tag]',
205
+ entrypoint: 'ferst-core/routes/posts/tag/[...tag].astro',
206
+ });
207
+ // Index pages 2+ (page 1 is /posts; tag pages paginate inside their own route).
208
+ injectRoute({
209
+ pattern: '/posts/page/[page]',
210
+ entrypoint: 'ferst-core/routes/posts/page/[page].astro',
211
+ });
212
+ }
213
+ // The cookie banner links here on every page, so it must always resolve. A site's
214
+ // own hand-built page wins (Astro would otherwise see two routes for one path).
215
+ const bespoke = config?.root && existsSync(new URL('src/pages/cookie-policy.astro', config.root));
216
+ if (cookiePolicy && !bespoke) {
217
+ injectRoute({
218
+ pattern: '/cookie-policy',
219
+ entrypoint: 'ferst-core/routes/cookie-policy.astro',
220
+ });
221
+ }
222
+ // Every footer links to the accessibility statement. The site's own page wins.
223
+ const ownStatement = config?.root && existsSync(new URL('src/pages/accessibility.astro', config.root));
224
+ if (accessibility && !ownStatement) {
225
+ injectRoute({ pattern: '/accessibility', entrypoint: 'ferst-core/routes/accessibility.astro' });
226
+ }
227
+ // A missing address must answer 404, not the home page (Cloudflare Pages serves the
228
+ // home page with a 200 when a build has no 404.html). The site's own page wins.
229
+ const ownNotFound = config?.root && existsSync(new URL('src/pages/404.astro', config.root));
230
+ if (notFound && !ownNotFound && !hasPublicFile(config?.root, '404.html')) {
231
+ injectRoute({ pattern: '/404', entrypoint: 'ferst-core/routes/404.astro' });
232
+ }
233
+ // Which optional routes this site has, for the pages that link to them (the sitemap,
234
+ // the "page not found" page).
235
+ updateConfig?.({
236
+ vite: {
237
+ define: {
238
+ 'import.meta.env.FERST_ROUTES_BLOG': JSON.stringify(blog),
239
+ 'import.meta.env.FERST_ROUTES_CALENDAR': JSON.stringify(calendar),
240
+ 'import.meta.env.FERST_ROUTES_ACCESSIBILITY': JSON.stringify(accessibility),
241
+ // Each of the site's own images with its width and height, so a page keeps every
242
+ // picture's shape before it loads (lib/image-sizes.ts).
243
+ 'import.meta.env.FERST_IMAGE_SIZES': JSON.stringify(publicImageSizes(config?.root)),
244
+ },
245
+ },
246
+ });
247
+ // Search signals: the site's address and whether this deploy is the live site (the
248
+ // production branch) or a test copy, the same rule the CMS uses for its git-proxy.
249
+ if (seo) {
250
+ const root = config?.root;
251
+ search = seoContext({ siteMeta: readSiteMeta(root), branch: deploymentBranch(), env: process.env });
252
+ updateConfig?.({
253
+ ...(search.siteUrl && !config?.site ? { site: search.siteUrl } : {}),
254
+ vite: {
255
+ define: {
256
+ 'import.meta.env.FERST_SITE_URL': JSON.stringify(search.siteUrl ?? ''),
257
+ 'import.meta.env.FERST_SEO_PRODUCTION': JSON.stringify(search.production),
258
+ },
259
+ },
260
+ });
261
+ if (!hasPublicFile(root, 'robots.txt')) {
262
+ injectRoute({ pattern: '/robots.txt', entrypoint: 'ferst-core/routes/robots.txt.ts' });
263
+ }
264
+ // A sitemap lists absolute addresses, so it needs the site's address.
265
+ if (search.siteUrl && !hasPublicFile(root, 'sitemap.xml')) {
266
+ injectRoute({ pattern: '/sitemap.xml', entrypoint: 'ferst-core/routes/sitemap.xml.ts' });
267
+ }
268
+ // The news feed lists absolute addresses too.
269
+ if (search.siteUrl && blog) {
270
+ injectRoute({ pattern: '/posts/rss.xml', entrypoint: 'ferst-core/routes/posts/rss.xml.ts' });
271
+ }
272
+ }
273
+ // Members (last: it may ask the platform): the header's account link.
274
+ const siteDir = config?.root ? fileURLToPath(config.root) : undefined;
275
+ const members = await membersOn(options.members, {
276
+ siteId: resolveSiteId(siteDir),
277
+ base: resolveGitBase(siteDir),
278
+ warn: (m) => (logger ? logger.warn(m) : console.warn(m)),
279
+ });
280
+ updateConfig?.({ vite: { define: { 'import.meta.env.FERST_MEMBERS': JSON.stringify(members) } } });
281
+ // The members' account page: signing in with an emailed code. The site's own page wins.
282
+ const ownAccount = config?.root && existsSync(new URL('src/pages/account.astro', config.root));
283
+ if (members && !ownAccount) {
284
+ injectRoute({ pattern: '/account', entrypoint: 'ferst-core/routes/account.astro' });
285
+ }
286
+ },
287
+ // The engine's self-hosted fonts: served in dev, copied into every build.
288
+ 'astro:server:setup': fonts['astro:server:setup'],
289
+ 'astro:build:done': async (ctx) => {
290
+ await fonts['astro:build:done'](ctx);
291
+ // Internal links point at each page's one address, never at a redirect (lib/seo.mjs).
292
+ if (seo) {
293
+ const isPage = (path) => {
294
+ try {
295
+ return existsSync(new URL(`.${path}index.html`, ctx.dir));
296
+ } catch {
297
+ return false;
298
+ }
299
+ };
300
+ for (const file of htmlFiles(ctx.dir)) {
301
+ const html = readFileSync(file, 'utf8');
302
+ const linked = linksToPageAddresses(html, isPage);
303
+ if (linked !== html) writeFileSync(file, linked);
304
+ }
305
+ }
306
+ // A test copy is noindex on every file: pages, and the PDFs and images beside them.
307
+ if (search && !search.production) {
308
+ const file = new URL('_headers', ctx.dir);
309
+ writeFileSync(file, withNoindexHeader(existsSync(file) ? readFileSync(file, 'utf8') : ''));
310
+ }
311
+ // The browser icon: /favicon.ico and /apple-touch-icon.png (sharp comes with Astro).
312
+ if (icons && siteRoot) {
313
+ const sharp = await import('sharp').then((m) => m.default ?? m).catch(() => null);
314
+ await writeSiteIcons({ root: siteRoot, outDir: ctx.dir, sharp, warn: (m) => (ctx.logger ? ctx.logger.warn(m) : console.warn(m)) });
315
+ }
316
+ },
317
+ },
318
+ };
319
+ }
@@ -269,6 +269,7 @@ const hasFeed = Boolean(siteUrl) && import.meta.env.FERST_ROUTES_BLOG !== false;
269
269
  primaryNav={navigation.primary}
270
270
  dropdown={navigation.dropdown}
271
271
  cta={navigation.cta}
272
+ account={import.meta.env.FERST_MEMBERS === true}
272
273
  />
273
274
 
274
275
  <main id="main" tabindex="-1" class:list={['site-main', mainClass]} data-content-align={align}>