@unbase-app/pages 0.3.0 → 0.4.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/README.md +36 -3
- package/astro.d.mts +14 -1
- package/astro.mjs +93 -5
- package/core.mjs +793 -8
- package/node.mjs +24 -2
- package/package.json +3 -2
- package/vite.d.mts +20 -1
- package/vite.mjs +234 -13
package/README.md
CHANGED
|
@@ -75,10 +75,43 @@ dynamic route, a page another file imports, content it cannot attribute to the p
|
|
|
75
75
|
`unbase.pages.json`, or a draft's text still in the output. Mark the page as ready, or fix the
|
|
76
76
|
reason, and build again.
|
|
77
77
|
|
|
78
|
+
## Page actions and redirects
|
|
79
|
+
|
|
80
|
+
Unbase renames, duplicates and deletes pages by one rule, the core's `planPage`: the same files
|
|
81
|
+
always give the same changes, in Unbase's editor and with `npx @unbase-app/cli page …` alike. A
|
|
82
|
+
page that was on the live site leaves a redirect when it is renamed or deleted, in the same file:
|
|
83
|
+
|
|
84
|
+
```json
|
|
85
|
+
{ "pages": { "/financiering": { "name": "Financiering" } }, "redirects": { "/funding": "/financiering" } }
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
A source that ends in `/[...path]` sends everything under it on, the rest of the address kept
|
|
89
|
+
(`/over-ons/[...path]` → `/about/[...path]`): a renamed page's pages under it.
|
|
90
|
+
|
|
91
|
+
From 0.4.0 the build applies them, in the dev server and every build alike:
|
|
92
|
+
|
|
93
|
+
- **Astro**: as Astro redirects, permanent (308). In a prerendered site (Astro's default) a
|
|
94
|
+
redirect for everything under an address becomes one redirect per page under the new address,
|
|
95
|
+
because Astro only takes `[...path]` there when a dynamic route is at its destination.
|
|
96
|
+
- **Vite + React Router**: as routes in the router file's `<Routes>`, before the catch-all: an
|
|
97
|
+
address sent on renders `<Navigate to replace />`, everything under one a small component that
|
|
98
|
+
keeps the rest of the address, its query and its hash. A single-page app has no server status
|
|
99
|
+
to give, so this is a client redirect, and it works on any host.
|
|
100
|
+
|
|
101
|
+
The site's own redirects win: one Astro's config already has, to the same place, stays the
|
|
102
|
+
site's; the same address sent somewhere else stops the build. A redirect whose address is a page
|
|
103
|
+
again is not applied (the page wins), and the build says so. An invalid `unbase.pages.json` stops
|
|
104
|
+
a build; the dev server warns and goes on.
|
|
105
|
+
|
|
106
|
+
Unbase only renames or deletes a live page on a site whose package applies redirects
|
|
107
|
+
(`REDIRECTS_SINCE` in the core: this package 0.4.0, `@unbase-app/astro`, `/vite` and `/next`
|
|
108
|
+
0.3.0), so an old address never breaks silently.
|
|
109
|
+
|
|
78
110
|
## Removing it
|
|
79
111
|
|
|
80
112
|
Publish or delete the drafts first: without the package, a draft in `unbase.pages.json` is
|
|
81
|
-
simply built.
|
|
113
|
+
simply built. Its redirects go with it too: move them into the site's own config first. Then
|
|
114
|
+
remove the package, its line in the config, and `unbase.pages.json`.
|
|
82
115
|
|
|
83
116
|
## Exports
|
|
84
117
|
|
|
@@ -86,7 +119,7 @@ simply built. Then remove the package, its line in the config, and `unbase.pages
|
|
|
86
119
|
|---|---|
|
|
87
120
|
| `@unbase-app/pages/astro` | The Astro integration (default export, and `unbasePages`) |
|
|
88
121
|
| `@unbase-app/pages/vite` | The Vite plugin (`unbasePages`) |
|
|
89
|
-
| `@unbase-app/pages` | The core: discovering a site's pages
|
|
122
|
+
| `@unbase-app/pages` | The core: discovering a site's pages, checking a draft, and planning a page action (`planPage`, `pageActions`, `redirectsFor`), as pure functions on the app's files |
|
|
90
123
|
|
|
91
124
|
Node 18.17 or newer. MIT licence.
|
|
92
125
|
|
|
@@ -99,7 +132,7 @@ discovery and the draft check; Unbase's backend and the Vite and Next.js package
|
|
|
99
132
|
supabase/functions/_shared/pages-core.mjs`, `node vite/scripts/sync-pages.mjs` and `cp
|
|
100
133
|
pages/core.mjs next/core.mjs`.
|
|
101
134
|
|
|
102
|
-
- `node --test test
|
|
135
|
+
- `node --test test/*.test.mjs`: the core (`core.test.mjs`), the page actions (`actions.test.mjs`) and the redirects in Astro and Vite (`redirects.test.mjs`).
|
|
103
136
|
- A11, the acceptance test, on lab sites only: `node test/a11.mjs vibe` (the Lovable-shaped
|
|
104
137
|
fixture in `test/fixtures/lab-vibe`) and `node test/a11.mjs astro --dir <lab copy>` (a git
|
|
105
138
|
repository without a remote). It needs `playwright-core` (`PLAYWRIGHT_CORE=<path>`) and Chrome
|
package/astro.d.mts
CHANGED
|
@@ -2,7 +2,8 @@ import type { AstroIntegration } from "astro";
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* The Astro integration: in a production build every draft in unbase.pages.json is kept out, or
|
|
5
|
-
* the build stops
|
|
5
|
+
* the build stops; in every command the addresses its `redirects` send on become Astro redirects
|
|
6
|
+
* (308). Put it after sitemap() and comment-modus in `integrations`.
|
|
6
7
|
*/
|
|
7
8
|
declare function unbasePages(): AstroIntegration;
|
|
8
9
|
export default unbasePages;
|
|
@@ -14,3 +15,15 @@ export declare function withoutDraftEntries(
|
|
|
14
15
|
owned: Set<string>,
|
|
15
16
|
devalue: { unflatten(value: unknown): unknown; stringify(value: unknown): string },
|
|
16
17
|
): { code: string; removed: string[] };
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* unbase.pages.json's redirects as Astro's `redirects` takes them, or null when it has none. Throws
|
|
21
|
+
* when one conflicts with the site's own (`own`) or the file is not valid (used by the integration;
|
|
22
|
+
* exported for its tests).
|
|
23
|
+
*/
|
|
24
|
+
export declare function astroRedirects(input: {
|
|
25
|
+
root: string;
|
|
26
|
+
own?: Record<string, string | { destination: string }> | null;
|
|
27
|
+
prerender?: boolean;
|
|
28
|
+
warn?: (line: string) => void;
|
|
29
|
+
}): Record<string, { status: 308; destination: string }> | null;
|
package/astro.mjs
CHANGED
|
@@ -8,6 +8,11 @@
|
|
|
8
8
|
// the output is checked for the page's text. Previews and the dev server show
|
|
9
9
|
// drafts as they are. Fails closed: a draft it cannot keep out stops the build.
|
|
10
10
|
//
|
|
11
|
+
// The addresses unbase.pages.json sends on (`redirects`: a page Unbase renamed
|
|
12
|
+
// or deleted while it was live) become Astro redirects, permanent (308), in
|
|
13
|
+
// the dev server and every build alike. One the site's own `redirects` sends
|
|
14
|
+
// somewhere else stops the build.
|
|
15
|
+
//
|
|
11
16
|
// It goes after @astrojs/sitemap and comment-modus in `integrations`, so the
|
|
12
17
|
// lists they write exist when it cleans them; the build stops otherwise.
|
|
13
18
|
//
|
|
@@ -16,16 +21,25 @@
|
|
|
16
21
|
// integrations: [..., sitemap(), unbasePages()]
|
|
17
22
|
// ============================================================================
|
|
18
23
|
|
|
24
|
+
import { existsSync } from "node:fs";
|
|
19
25
|
import { createRequire } from "node:module";
|
|
20
26
|
import { join } from "node:path";
|
|
21
27
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
22
|
-
import
|
|
28
|
+
import * as core from "./core.mjs";
|
|
29
|
+
import { finishOutput, pageRedirects, planBuild, removePageHtml } from "./node.mjs";
|
|
23
30
|
|
|
24
31
|
const NAME = "@unbase-app/pages";
|
|
25
32
|
/** Integrations that write a page list in astro:build:done. */
|
|
26
33
|
const LISTS = ["@astrojs/sitemap", "comment-modus"];
|
|
27
34
|
|
|
28
35
|
const clean = (id) => id.replace(/\?.*$/, "");
|
|
36
|
+
/**
|
|
37
|
+
* Where a host's adapter copies the client output and serves it from. The Vercel adapter (11, with
|
|
38
|
+
* Astro 7) copies dist/client to .vercel/output/static in its own astro:build:done, which can run
|
|
39
|
+
* before this integration's: a draft removed from dist/client only would still be served (found
|
|
40
|
+
* on a copy of a real site, 10-11-2026). So the copy is cleaned and checked as well.
|
|
41
|
+
*/
|
|
42
|
+
const HOST_COPIES = [".vercel/output/static"];
|
|
29
43
|
/** Astro's content store as one module: every collection's entries, also in the server's bundle. */
|
|
30
44
|
const DATA_STORE = "\0astro:data-layer-content";
|
|
31
45
|
|
|
@@ -53,18 +67,88 @@ export function withoutDraftEntries(code, owned, devalue) {
|
|
|
53
67
|
return { code: `export default JSON.parse(${JSON.stringify(devalue.stringify(store))})`, removed };
|
|
54
68
|
}
|
|
55
69
|
|
|
70
|
+
/** A redirect's address that carries what is under it: `/old/[...path]`. */
|
|
71
|
+
const REST = /\/\[\.\.\.[A-Za-z_]\w*\]$/;
|
|
72
|
+
/** An address as a redirect source is compared: no trailing slash, a rest parameter whatever its name. */
|
|
73
|
+
const sameAs = (p) => core.normalisePath(p).replace(/\[\.\.\.[A-Za-z_]\w*\]/g, "[...]");
|
|
74
|
+
const within = (p, at) => p === at || (at === "/" ? p !== "/" : p.startsWith(`${at}/`));
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The page file's redirects in the form Astro's `redirects` takes, permanent (308), or null when
|
|
78
|
+
* there are none. `own` is the site's own `redirects`: a source it already sends to the same place
|
|
79
|
+
* stays the site's, and one it sends somewhere else stops the build (fail closed: which of the two
|
|
80
|
+
* a visitor gets would depend on Astro's merge).
|
|
81
|
+
*
|
|
82
|
+
* `prerender`: whether the site's pages are prerendered (Astro's default unless `output:
|
|
83
|
+
* "server"`). Astro only takes a redirect for everything under an address (`/old/[...path]`) in a
|
|
84
|
+
* prerendered site when a dynamic route is at its destination, and refuses the config otherwise.
|
|
85
|
+
* So there it becomes one redirect per page under the new address, and per address the page file
|
|
86
|
+
* sends there: the same addresses, each known at build.
|
|
87
|
+
* @param {{ root: string, own?: Record<string, string | { destination: string }> | null, prerender?: boolean, warn?: (line: string) => void }} input
|
|
88
|
+
* @returns {Record<string, { status: 308, destination: string }> | null}
|
|
89
|
+
*/
|
|
90
|
+
export function astroRedirects({ root, own = {}, prerender = true, warn = () => {} }) {
|
|
91
|
+
const found = pageRedirects({ root, framework: "astro" });
|
|
92
|
+
if (!found) return null;
|
|
93
|
+
for (const s of found.skipped) warn(`${s} is a page now, so the redirect ${core.PAGE_FILE} has for it is not applied: the page wins.`);
|
|
94
|
+
const pages = new Set(found.found.pages.map((p) => p.path));
|
|
95
|
+
/** @type {Map<string, string>} source → destination, the page file's own first */
|
|
96
|
+
const list = new Map();
|
|
97
|
+
for (const r of found.list) if (!(prerender && REST.test(r.source))) list.set(r.source, r.destination);
|
|
98
|
+
if (prerender) {
|
|
99
|
+
const targets = [...new Set([...pages, ...Object.keys(found.redirects).filter((s) => !REST.test(s))])].sort();
|
|
100
|
+
for (const r of found.list) {
|
|
101
|
+
if (!REST.test(r.source)) continue;
|
|
102
|
+
const base = core.normalisePath(r.source.replace(REST, ""));
|
|
103
|
+
const to = core.normalisePath(r.destination.replace(REST, ""));
|
|
104
|
+
for (const t of targets) {
|
|
105
|
+
if (!within(t, to)) continue;
|
|
106
|
+
const source = t === to ? base : core.normalisePath(`${base}${t.slice(to === "/" ? 0 : to.length)}`);
|
|
107
|
+
// A page at the old address is a page again, and Astro would put the redirect in its place.
|
|
108
|
+
if (source !== t && !pages.has(source) && !list.has(source)) list.set(source, t);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const theirs = new Map(Object.entries(own ?? {}).map(([source, to]) => [sameAs(source), typeof to === "string" ? to : to?.destination]));
|
|
113
|
+
const conflicts = [];
|
|
114
|
+
/** @type {Record<string, { status: 308, destination: string }>} */
|
|
115
|
+
const out = {};
|
|
116
|
+
for (const [source, destination] of list) {
|
|
117
|
+
const have = theirs.get(sameAs(source));
|
|
118
|
+
if (have === undefined) out[source] = { status: 308, destination };
|
|
119
|
+
else if (typeof have !== "string" || sameAs(have) !== sameAs(destination)) conflicts.push(` ${source}: astro.config sends it to ${have}, ${core.PAGE_FILE} to ${destination}`);
|
|
120
|
+
}
|
|
121
|
+
if (conflicts.length) throw new Error(`[unbase-pages] astro.config's redirects and ${core.PAGE_FILE} send the same address to different places, so the build stops:\n${conflicts.join("\n")}\nKeep one of the two.`);
|
|
122
|
+
return Object.keys(out).length ? out : null;
|
|
123
|
+
}
|
|
124
|
+
|
|
56
125
|
/** @returns {import("astro").AstroIntegration} */
|
|
57
126
|
export default function unbasePages() {
|
|
58
127
|
/** @type {ReturnType<typeof planBuild>} */
|
|
59
128
|
let plan = null;
|
|
60
129
|
/** Where the build writes, client and server, for the leak check. */
|
|
61
130
|
let roots = [];
|
|
131
|
+
/** The project's folder, where a host adapter keeps its copy of the output. */
|
|
132
|
+
let root = "";
|
|
62
133
|
return {
|
|
63
134
|
name: NAME,
|
|
64
135
|
hooks: {
|
|
65
136
|
"astro:config:setup": async ({ command, config, updateConfig, logger }) => {
|
|
66
|
-
if (command !== "build") return;
|
|
67
137
|
const root = fileURLToPath(config.root);
|
|
138
|
+
// Redirects in every command, so the dev server sends an old address on as Live does. A
|
|
139
|
+
// page file that cannot be read stops a build; the dev server says so and goes on.
|
|
140
|
+
let redirects = null;
|
|
141
|
+
try {
|
|
142
|
+
redirects = astroRedirects({ root, own: config.redirects, prerender: config.output !== "server", warn: (l) => logger.warn(l) });
|
|
143
|
+
} catch (e) {
|
|
144
|
+
if (command === "build") throw e;
|
|
145
|
+
logger.warn(/** @type {Error} */ (e).message);
|
|
146
|
+
}
|
|
147
|
+
if (redirects) {
|
|
148
|
+
updateConfig({ redirects });
|
|
149
|
+
logger.info(`redirects from ${core.PAGE_FILE}: ${Object.entries(redirects).map(([from, to]) => `${from} → ${to.destination}`).join(", ")}`);
|
|
150
|
+
}
|
|
151
|
+
if (command !== "build") return;
|
|
68
152
|
plan = planBuild({ root, framework: "astro", command: "build", log: (l) => logger.info(l) });
|
|
69
153
|
if (!plan) return;
|
|
70
154
|
const stubs = new Set(plan.drafts.map((d) => d.file));
|
|
@@ -94,6 +178,7 @@ export default function unbasePages() {
|
|
|
94
178
|
},
|
|
95
179
|
"astro:config:done": ({ config }) => {
|
|
96
180
|
if (!plan) return;
|
|
181
|
+
root = fileURLToPath(config.root);
|
|
97
182
|
const all = [config.outDir, config.build.client, config.build.server].map((u) => fileURLToPath(u));
|
|
98
183
|
roots = all.filter((r, i) => !all.some((o, j) => j !== i && r !== o && r.startsWith(o)) && all.indexOf(r) === i);
|
|
99
184
|
const names = config.integrations.map((i) => i.name);
|
|
@@ -103,9 +188,12 @@ export default function unbasePages() {
|
|
|
103
188
|
"astro:build:done": ({ dir, logger }) => {
|
|
104
189
|
if (!plan) return;
|
|
105
190
|
const out = fileURLToPath(dir);
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
191
|
+
const copies = root ? HOST_COPIES.map((p) => join(root, p)).filter((p) => existsSync(p)) : [];
|
|
192
|
+
for (const at of [out, ...copies]) for (const d of plan.drafts) for (const f of removePageHtml(at, d.path)) logger.info(`removed ${f.slice((at === out ? out : root).length)}`);
|
|
193
|
+
// Lists are cleaned where the host serves them, its adapter's copy too; the leak check reads
|
|
194
|
+
// the server's bundle and that copy as well.
|
|
195
|
+
for (const copy of copies) finishOutput(copy, plan, (l) => logger.info(`${l} in ${copy.slice(root.length)}`), []);
|
|
196
|
+
finishOutput(out, plan, (l) => logger.info(l), [...(roots.length ? roots : [out]), ...copies]);
|
|
109
197
|
},
|
|
110
198
|
},
|
|
111
199
|
};
|