feat: token-protected site cache purge route

- POST /api/__journal__/cache/purge removes every Nitro route-cache entry
- 404 without a 32-character token or behind a proxy, 401 on a wrong bearer
  token (constant-time over sha256 digests)
- routeRules keep /api/__journal__/** out of the route cache
- runtimeConfig.journal.cachePurgeToken (NUXT_JOURNAL_CACHE_PURGE_TOKEN)
- fixture API reload(); testing/purge-check.mjs proves a cached page shows
  a change after a purge; the playground caches with swr 300
This commit is contained in:
2026-10-10 18:19:58 +02:00
parent 782aa4da7d
commit a03e076c62
6 changed files with 322 additions and 9 deletions

View File

@@ -18,7 +18,7 @@ const SORT_COLUMNS = new Set(['published_at', 'created_at', 'updated_at', 'title
/**
* @typedef {{ method: string, url: string, xff: string | null }} FixtureRequest
* @typedef {{ url: string, requests: FixtureRequest[], close: () => Promise<void> }} FixtureApi
* @typedef {{ url: string, requests: FixtureRequest[], reload: () => void, close: () => Promise<void> }} FixtureApi
*/
/** @param {string} dir @param {string} name @param {any} fallback */
@@ -118,19 +118,28 @@ function sibling(post) {
*/
export function startFixtureApi({ dir, port = 0 }) {
/** @type {any[]} */
const posts = readJson(dir, 'posts.json', [])
let posts = []
/** @type {any[]} */
const categories = readJson(dir, 'categories.json', [])
let categories = []
/** @type {any[]} */
const tags = readJson(dir, 'tags.json', [])
let tags = []
/** @type {any[]} */
const authors = readJson(dir, 'authors.json', [])
let authors = []
let rss = ''
try {
rss = readFileSync(join(dir, 'rss.xml'), 'utf8')
} catch {
rss = '<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><channel></channel></rss>'
// Reads (or re-reads) the fixtures, so a check can change a fixture file
// while the API runs.
const load = () => {
posts = readJson(dir, 'posts.json', [])
categories = readJson(dir, 'categories.json', [])
tags = readJson(dir, 'tags.json', [])
authors = readJson(dir, 'authors.json', [])
try {
rss = readFileSync(join(dir, 'rss.xml'), 'utf8')
} catch {
rss = '<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><channel></channel></rss>'
}
}
load()
/** @type {FixtureRequest[]} */
const requests = []
@@ -240,6 +249,7 @@ export function startFixtureApi({ dir, port = 0 }) {
resolve({
url: `http://127.0.0.1:${addr.port}`,
requests,
reload: load,
close: () => new Promise((done) => server.close(() => done())),
})
})