Skip to content

Commit 5ef597a

Browse files
committed
Create automation for easier to read CSP diffs
1 parent 8f6d2cf commit 5ef597a

File tree

9 files changed

+809
-2
lines changed

9 files changed

+809
-2
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,38 @@ when files change.
8585
hugo server
8686
```
8787

88+
#### HTTP Headers Configuration
89+
90+
The `static/_headers` file is automatically generated from
91+
`scripts/_headers.config.ts`. **Do not edit `static/_headers` directly** - your
92+
changes will be overwritten.
93+
94+
##### Making Changes to Headers
95+
96+
1. Edit `scripts/_headers.config.ts` (the source of truth with readable format
97+
and TypeScript type safety)
98+
2. Generate the headers file:
99+
```sh
100+
npm run build:headers
101+
```
102+
3. Commit both files - a git pre-commit hook will automatically regenerate
103+
`static/_headers` when you modify `scripts/_headers.config.ts`
104+
105+
##### Automatic Synchronization
106+
107+
The files stay in sync automatically through:
108+
109+
- **Pre-commit hook** - Regenerates `_headers` when `_headers.config.ts` changes
110+
- **Build process** - `build.sh` regenerates before deployment
111+
- **Manual generation** - Run `npm run build:headers` anytime
112+
113+
The TypeScript config format provides:
114+
115+
- Readable multi-line arrays for CSP directives
116+
- Native TypeScript type safety and IDE support
117+
- Easy-to-review diffs in pull requests
118+
- Zero dependencies for parsing
119+
88120
### Updating Release Notes for the New Year
89121

90122
Whenever you create your first release note for a product category for a new

build.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22

33
set -eu
44

5+
# Generate _headers file from TypeScript configuration
6+
npx tsx scripts/generate-headers.ts
7+
58
hugo --gc --minify -b "$CF_PAGES_URL"

git/hooks/pre-commit

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,20 @@ function timed_run() {
2424
fi
2525
}
2626

27+
# Regenerate static/_headers from _headers.config.ts if it changed
28+
if git diff --cached --name-only | grep -q "scripts/_headers.config.ts"; then
29+
echo "Detected changes to scripts/_headers.config.ts, regenerating static/_headers..."
30+
npx tsx scripts/generate-headers.ts
31+
git add static/_headers
32+
echo "✅ Updated static/_headers and added to commit"
33+
fi
34+
35+
# Also regenerate if the generator script itself changed
36+
if git diff --cached --name-only | grep -q "scripts/generate-headers.ts"; then
37+
echo "Detected changes to generator script, regenerating static/_headers..."
38+
npx tsx scripts/generate-headers.ts
39+
git add static/_headers
40+
echo "✅ Updated static/_headers and added to commit"
41+
fi
42+
2743
timed_run "precious lint" "precious lint --staged"

0 commit comments

Comments
 (0)