Files
vue-seo-layer/server/plugins/seo-cache-purge.ts
Jakub Zych 691fa09b0f feat: shared site cache purge route clears the SEO caches
- POST /api/__site__/cache/purge, its library and the site config plugin
  moved from vue-journal-layer with the same contract (token rules, 404 for
  proxied requests unless allowProxied, deprecated siteCache names); the
  route also clears the resolve cache
- server/plugins/seo-cache-purge.ts clears the SEO caches after any 200
  purge, so an app whose journal, menu or FAQ layer copy of the route wins
  keeps fresh SEO answers until those copies are removed
- cachePurge and siteCache runtime keys; /api/__site__/** never cached
- testing/purge-check.mjs (CMS answer change, plus a redirect added for a
  route-cached page applying at once) and purge-token-check.mjs
2026-11-04 09:23:15 +01:00

23 lines
1.0 KiB
TypeScript

// Clears the SEO layer's caches after every successful site cache purge,
// whichever layer's copy of POST /api/__site__/cache/purge answered it.
//
// vue-journal-layer, vue-menu-layer and vue-faq-layer ship their own copy of
// the shared purge route until they extend this layer, and Nitro keeps one
// handler per route and method, so in an app that extends several of them
// this layer's route (which clears the caches itself) may not be the one
// that runs. The journal's deprecated POST /api/__journal__/cache/purge
// purges the route cache too, so it clears them as well. Clearing twice is
// harmless.
import { clearSeoCaches } from '../lib/resolve.ts'
const PURGE_PATHS = new Set(['/api/__site__/cache/purge', '/api/__journal__/cache/purge'])
export default defineNitroPlugin((nitroApp) => {
nitroApp.hooks.hook('afterResponse', (event) => {
if (event.method !== 'POST') return
if (!PURGE_PATHS.has((event.path.split('?')[0] ?? '').replace(/\/+$/, ''))) return
if (event.node.res.statusCode === 200) clearSeoCaches()
})
})