@nuasite/nua 0.65.3 → 0.67.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 CHANGED
@@ -129,4 +129,6 @@ A site can declare redirects in three places, and the build merges them into one
129
129
 
130
130
  **Editors redirect; code guards.** A rule from the CMS may only redirect (301, 302, 303, 307 or 308, no conditions, so no proxying), and it may not touch a path a guard in code covers: a rule with conditions such as a role gate (`Role=admin`), a 4xx, or a line not read as a rule. Listed first, an editor's rule could otherwise take access control off a path. The build leaves such a rule out with a warning, and the CMS editor refuses to save a rule the build would not read or allow.
131
131
 
132
+ A static build ships the config's `redirects` only as `_redirects` rules: Astro builds no meta-refresh page for them, so a source that looks like a file (`/a.pdf`) does not become a directory. The dev server, which has no `_redirects`, still answers them itself, and so does an adapter build, where the adapter may serve them as routes. The dev server leaves out, with a warning, a redirect Astro's router refuses, such as a source with a param name other than letters, digits, `_` and `$` (`/blog-old/[post-id]`).
133
+
132
134
  Paths in the config's `redirects` are root-relative, as Astro reads a key: `'old'` and `'/old'` both redirect `/old`, and a destination written without a slash gets one. Route params become the host's placeholders: `[slug]` is `:slug`, `[...rest]` is `*` (`:splat` in the destination).
@@ -1 +1 @@
1
- {"version":3,"file":"integration.d.ts","sourceRoot":"","sources":["../src/integration.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAe,gBAAgB,EAAE,MAAM,OAAO,CAAA;AAC1D,OAAO,EAAE,KAAK,qBAAqB,EAAkB,MAAM,YAAY,CAAA;AAEvE,MAAM,CAAC,OAAO,UAAU,GAAG,CAAC,OAAO,GAAE,qBAA0B,GAAG,gBAAgB,CA8GjF"}
1
+ {"version":3,"file":"integration.d.ts","sourceRoot":"","sources":["../src/integration.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAe,gBAAgB,EAAE,MAAM,OAAO,CAAA;AAE1D,OAAO,EAAE,KAAK,qBAAqB,EAAkB,MAAM,YAAY,CAAA;AAEvE,MAAM,CAAC,OAAO,UAAU,GAAG,CAAC,OAAO,GAAE,qBAA0B,GAAG,gBAAgB,CAgHjF"}
@@ -4,6 +4,7 @@ import checks from '@nuasite/checks';
4
4
  import cms, { addMarkdownPlugins, cmsRemarkPlugins, inlineNuasitePackages, mergeConfigRedirectsInto, userExternals } from '@nuasite/cms';
5
5
  import pageMarkdown from '@nuasite/llm-enhancements';
6
6
  import tailwindcss from '@tailwindcss/vite';
7
+ import { redirectsAstroServes } from "./redirects.js";
7
8
  import { resolveOptions } from "./types.js";
8
9
  export default function nua(options = {}) {
9
10
  const resolved = resolveOptions(options);
@@ -58,9 +59,14 @@ export default function nua(options = {}) {
58
59
  }
59
60
  `);
60
61
  }
61
- // Captured for `_redirects`. The `redirects: {}` passed to `updateConfig` below does not clear
62
- // them: Astro deep-merges config updates, so it still renders its own meta-refresh pages.
62
+ // A static build ships them as `_redirects` rules only. `updateConfig` deep-merges, so it cannot
63
+ // clear them; the config Astro reads them from is the one passed here. An adapter may serve them
64
+ // as routes, so Astro keeps them; dev has no `_redirects`, so Astro keeps the ones it accepts.
63
65
  capturedRedirects = config.redirects ?? {};
66
+ if (command === 'build' && config.adapter === undefined)
67
+ config.redirects = {};
68
+ if (command === 'dev')
69
+ config.redirects = redirectsAstroServes(capturedRedirects, message => logger.warn(message));
64
70
  // CMS list-style directives (`:::list{.class}`) need `remark-directive`
65
71
  // in the markdown pipeline, both to render `<ul class="class">` and to
66
72
  // stop MDX from parsing the `{.class}` attributes as a JS expression
@@ -74,7 +80,6 @@ export default function nua(options = {}) {
74
80
  }
75
81
  // Inject Vite plugins and integrations.
76
82
  updateConfig({
77
- redirects: {},
78
83
  vite: {
79
84
  plugins: vitePlugins,
80
85
  ssr: {
@@ -0,0 +1,13 @@
1
+ import type { AstroConfig } from 'astro';
2
+ /**
3
+ * Why Astro's route manifest refuses the redirect from `from` to `destination`, or `null` when it passes
4
+ * the checks that need no routes. A source with params whose destination is not a page is refused too,
5
+ * but the routes are not known while the config is set up.
6
+ */
7
+ export declare function astroRedirectRejection(from: string, destination: string): string | null;
8
+ /**
9
+ * The config's redirects without the ones Astro's route manifest refuses, each of which is reported.
10
+ * The dev server answers redirects through that manifest, and a single refused one keeps it from starting.
11
+ */
12
+ export declare function redirectsAstroServes(redirects: AstroConfig['redirects'], warn: (message: string) => void): AstroConfig['redirects'];
13
+ //# sourceMappingURL=redirects.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redirects.d.ts","sourceRoot":"","sources":["../src/redirects.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,OAAO,CAAA;AAQxC;;;;GAIG;AACH,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAYvF;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,SAAS,EAAE,WAAW,CAAC,WAAW,CAAC,EAAE,IAAI,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,GAAG,WAAW,CAAC,WAAW,CAAC,CAQnI"}
@@ -0,0 +1,42 @@
1
+ // Astro's own checks on a redirect that need no routes (astro/dist/core/routing: `createRedirectRoutes`
2
+ // and `getParts` in create-manifest.js, `validateSegment` in segment.js). One that fails them stops
3
+ // the dev server, as Astro builds its route manifest from every redirect.
4
+ const ROUTE_DYNAMIC_SPLIT = /\[([^[\]()]+(?:\([^)]+\))?)\]/;
5
+ const PARAM_NAME = /^(?:\.\.\.)?[\w$]+$/;
6
+ /**
7
+ * Why Astro's route manifest refuses the redirect from `from` to `destination`, or `null` when it passes
8
+ * the checks that need no routes. A source with params whose destination is not a page is refused too,
9
+ * but the routes are not known while the config is set up.
10
+ */
11
+ export function astroRedirectRejection(from, destination) {
12
+ for (const segment of from.split('/').filter(Boolean)) {
13
+ if (segment.includes(']['))
14
+ return 'its parameters must be separated';
15
+ if (segment.split('[').length !== segment.split(']').length)
16
+ return 'its brackets are unbalanced';
17
+ const parts = segment.split(ROUTE_DYNAMIC_SPLIT);
18
+ for (let i = 1; i < parts.length; i += 2) {
19
+ const name = /([^(]+)$/.exec(parts[i] ?? '')?.[1];
20
+ if (name === undefined || !PARAM_NAME.test(name))
21
+ return `its parameter "[${parts[i]}]" is not named with letters, digits, _ or $`;
22
+ }
23
+ }
24
+ if (URL.canParse(destination) && !/^https?:\/\//.test(destination))
25
+ return `its destination "${destination}" is not an http(s) URL`;
26
+ return null;
27
+ }
28
+ /**
29
+ * The config's redirects without the ones Astro's route manifest refuses, each of which is reported.
30
+ * The dev server answers redirects through that manifest, and a single refused one keeps it from starting.
31
+ */
32
+ export function redirectsAstroServes(redirects, warn) {
33
+ const served = {};
34
+ for (const [from, value] of Object.entries(redirects)) {
35
+ const rejection = astroRedirectRejection(from, typeof value === 'string' ? value : value.destination);
36
+ if (rejection === null)
37
+ served[from] = value;
38
+ else
39
+ warn(`The dev server leaves out the redirect from "${from}", which Astro rejects: ${rejection}.`);
40
+ }
41
+ return served;
42
+ }