@nuasite/nua 0.58.2 → 0.60.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
@@ -90,3 +90,23 @@ export default defineConfig({
90
90
  integrations: [nua()],
91
91
  })
92
92
  ```
93
+
94
+ ## Redirects
95
+
96
+ A site can declare redirects in three places, and the build merges them into one `dist/_redirects`:
97
+
98
+ | Source | Written by | Precedence |
99
+ | ------------------------------- | ------------------------ | ---------- |
100
+ | `src/_redirects` | editors, through the CMS | highest |
101
+ | `public/_redirects` | hand | middle |
102
+ | `redirects` in the Astro config | code | lowest |
103
+
104
+ - **One rule per path.** When two sources redirect the same path, the one with higher precedence is kept and the build logs the rule it overrode. Cloudflare Pages rejects a file that lists a path twice, together with the whole deploy.
105
+ - **Higher precedence comes first.** Each source's rules are written as a block, in their own order, ahead of the sources they override. A host applies the first rule that matches, so a CMS rule wins over code whether it is an exact path or a pattern, and the order within a hand-written file is kept. An exact rule that a pattern from higher up reaches first is logged as never reached.
106
+ - **Lines it does not read are kept as written.** A line that is not `source destination [status] [conditions]`, such as a Netlify role gate (`/admin/* 200! Role=admin`) or one with a trailing comment, stays where it was and is not merged.
107
+ - **Netlify conditions are kept apart.** Rules for one path that differ by condition (`Language=cs`) or query (`id=:id`) are different rules.
108
+ - **An untouched file stays as written.** When nothing is merged into `public/_redirects`, its copy keeps its comments and layout.
109
+
110
+ **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.
111
+
112
+ 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).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nuasite/nua",
3
- "version": "0.58.2",
3
+ "version": "0.60.0",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "module": "src/index.ts",
@@ -38,12 +38,12 @@
38
38
  "@astrojs/check": "0.9.8",
39
39
  "@astrojs/mdx": "5.0.3",
40
40
  "@astrojs/sitemap": "3.7.2",
41
- "@nuasite/checks": "0.58.2",
42
- "@nuasite/llm-enhancements": "0.58.2",
43
- "@nuasite/cli": "0.58.2",
44
- "@nuasite/cms": "0.58.2",
45
- "@nuasite/components": "0.58.2",
46
- "@nuasite/core": "0.58.2",
41
+ "@nuasite/checks": "0.60.0",
42
+ "@nuasite/llm-enhancements": "0.60.0",
43
+ "@nuasite/cli": "0.60.0",
44
+ "@nuasite/cms": "0.60.0",
45
+ "@nuasite/components": "0.60.0",
46
+ "@nuasite/core": "0.60.0",
47
47
  "@tailwindcss/vite": "^4.2.2",
48
48
  "@tailwindcss/typography": "^0.5.19",
49
49
  "astro": "6.1.4",
@@ -1,22 +1,15 @@
1
1
  import mdx from '@astrojs/mdx'
2
2
  import sitemap from '@astrojs/sitemap'
3
3
  import checks from '@nuasite/checks'
4
- import cms, { cmsRemarkPlugins } from '@nuasite/cms'
4
+ import cms, { cmsRemarkPlugins, mergeConfigRedirectsInto } from '@nuasite/cms'
5
5
  import tailwindcss from '@tailwindcss/vite'
6
- import type { AstroIntegration } from 'astro'
7
- import { writeFile } from 'node:fs/promises'
6
+ import type { AstroConfig, AstroIntegration } from 'astro'
8
7
  import pageMarkdown from '../../llm-enhancements/src'
9
8
  import { type NuaIntegrationOptions, resolveOptions } from './types'
10
9
 
11
- interface NormalizedRedirect {
12
- from: string
13
- to: string
14
- code: number
15
- }
16
-
17
10
  export default function nua(options: NuaIntegrationOptions = {}): AstroIntegration {
18
11
  const resolved = resolveOptions(options)
19
- let capturedRedirects: NormalizedRedirect[] = []
12
+ let capturedRedirects: AstroConfig['redirects'] = {}
20
13
 
21
14
  return {
22
15
  name: '@nuasite/nua',
@@ -77,15 +70,9 @@ export default function nua(options: NuaIntegrationOptions = {}): AstroIntegrati
77
70
  )
78
71
  }
79
72
 
80
- // Capture and strip Astro redirects to generate _redirects file instead of meta-refresh HTML
81
- const astroRedirects = config.redirects ?? {}
82
- capturedRedirects = Object.entries(astroRedirects).map(([from, value]) => {
83
- const destination = typeof value === 'string' ? value : value.destination
84
- const code = typeof value === 'string' ? 307 : (value.status ?? 307)
85
- const normalizedFrom = from.replace(/\[\.\.\.[\w]+\]/g, '*')
86
- const normalizedTo = destination.replace(/\[\.\.\.[\w]+\]/g, ':splat')
87
- return { from: normalizedFrom, to: normalizedTo, code }
88
- })
73
+ // Captured for `_redirects`. The `redirects: {}` passed to `updateConfig` below does not clear
74
+ // them: Astro deep-merges config updates, so it still renders its own meta-refresh pages.
75
+ capturedRedirects = config.redirects ?? {}
89
76
 
90
77
  // Inject Vite plugins and integrations.
91
78
  // CMS list-style directives (`:::list{.class}`) need `remark-directive`
@@ -114,12 +101,9 @@ export default function nua(options: NuaIntegrationOptions = {}): AstroIntegrati
114
101
  })
115
102
  },
116
103
  'astro:build:done': async ({ dir, logger }) => {
117
- if (capturedRedirects.length === 0) return
118
- const lines = capturedRedirects.map(r => `${r.from} ${r.to} ${r.code}`)
119
- const content = lines.join('\n') + '\n'
120
- const filePath = new URL('_redirects', dir)
121
- await writeFile(filePath, content, 'utf-8')
122
- logger.info(`Generated _redirects with ${capturedRedirects.length} rules`)
104
+ // Merged into the copy Astro took of `public/_redirects`, never written over it. The CMS
105
+ // merges its own rules after this and checks the limits on the final file.
106
+ await mergeConfigRedirectsInto(dir, logger, capturedRedirects, resolved.cms === false)
123
107
  },
124
108
  },
125
109
  }