feat: redirect and 410 enforcement, X-Robots-Tag and the resolve cache

- the Nitro middleware answers redirect (301/302, Location from the answer
  only, same-site targets as paths as in v0.1.1) and gone (410) before the
  render and the route cache, sets X-Robots-Tag, and answers 404 for a
  pagination.invalid answer; other not_found answers stay with Nuxt
- resolve answers cached in an LRU (seo.resolveCacheTtl, default 60 s,
  1000 entries) with in-flight de-duplication; a clear detaches loads in
  flight so no answer fetched before a purge is stored after it
- the playground route-caches /blog/** (swr 300)
This commit is contained in:
Jakub Zych
2026-11-04 09:20:20 +01:00
parent 160b53b2f5
commit e3baa7028c
7 changed files with 401 additions and 9 deletions

104
server/lib/lru.ts Normal file
View File

@@ -0,0 +1,104 @@
// A small in-memory LRU with a TTL, a size cap and in-flight
// de-duplication, for the resolve and site answers. Pure TypeScript (no
// Nitro imports), so `node --test` drives it directly.
//
// clear() (the site cache purge) bumps a generation: a load that started
// before it still answers its caller, but its value is never stored, and a
// load after it never joins the older one. So no request after a purge sees
// an answer fetched before it.
export interface LruOptions {
/** Most entries kept; the least recently used goes first. */
max: number
/** How long an entry is fresh, in milliseconds; 0 or less stores nothing. */
ttlMs: number
/** The clock (Date.now); tests pass their own. */
now?: () => number
}
export interface Lru<V> {
get(key: string): V | undefined
set(key: string, value: V): void
delete(key: string): void
/** Empties the cache and detaches the loads in flight. */
clear(): void
readonly size: number
/**
* The cached value for `key`, else the loader's, shared by concurrent
* callers of the same key. The value is stored when `cacheable` accepts
* it (default: always); a rejected loader stores nothing.
*/
load(key: string, loader: () => Promise<V>, cacheable?: (value: V) => boolean): Promise<V>
}
export function createLru<V>({ max, ttlMs, now = Date.now }: LruOptions): Lru<V> {
const entries = new Map<string, { value: V; expires: number }>()
const inflight = new Map<string, Promise<V>>()
let generation = 0
const limit = Math.max(1, Math.floor(max))
const get = (key: string): V | undefined => {
const hit = entries.get(key)
if (!hit) return undefined
if (hit.expires <= now()) {
entries.delete(key)
return undefined
}
// Map keeps insertion order: re-inserting marks the key most recent.
entries.delete(key)
entries.set(key, hit)
return hit.value
}
const set = (key: string, value: V) => {
if (ttlMs <= 0) return
entries.delete(key)
entries.set(key, { value, expires: now() + ttlMs })
while (entries.size > limit) {
const oldest = entries.keys().next().value
if (oldest === undefined) break
entries.delete(oldest)
}
}
return {
get,
set,
delete: (key) => void entries.delete(key),
clear() {
entries.clear()
inflight.clear()
generation++
},
get size() {
for (const [key, e] of entries) if (e.expires <= now()) entries.delete(key)
return entries.size
},
load(key, loader, cacheable = () => true) {
const hit = get(key)
if (hit !== undefined) return Promise.resolve(hit)
const pending = inflight.get(key)
if (pending) return pending
const started = generation
// The loader starts at once; one that throws at once becomes a
// rejection, which settles after `p` is assigned and leaves no entry
// in flight.
let first: Promise<V>
try {
first = Promise.resolve(loader())
} catch (err) {
first = Promise.reject(err)
}
const p: Promise<V> = first
.then((value) => {
if (generation === started && cacheable(value)) set(key, value)
return value
})
.finally(() => {
if (inflight.get(key) === p) inflight.delete(key)
})
inflight.set(key, p)
return p
},
}
}

View File

@@ -1,9 +1,12 @@
// The server side of the resolve call: GET <apiBase>/resolve?url=<path and
// query>, forwarding the visitor's address so the backend's per-IP throttle
// query> through an LRU (path and query as key, seo.resolveCacheTtl seconds,
// default 60, at most 1000 entries, concurrent requests for one key share
// one call), forwarding the visitor's address so the backend's per-IP throttle
// (seo-resolve) keeps one bucket per visitor, also on route-cached renders
// (server/plugins/seo-client-address.ts records it). Any failure (timeout,
// 4xx, 5xx, an unexpected body) answers null and the page renders without
// CMS head tags (fail open); with the layer switched off nothing is called.
// CMS head tags (fail open) and is not cached; with the layer switched off
// nothing is called. The site cache purge clears the cache (clearSeoCaches).
//
// Nitro auto-imports (useRuntimeConfig, $fetch) are used inside the
// functions only, so `node --test` can import this file.
@@ -12,6 +15,26 @@ import type { H3Event } from 'h3'
import { seoAnswer } from '../../app/utils/seo.ts'
import type { SeoResolveAnswer } from '../../app/utils/types.ts'
import { seoServerConfig, type SeoServerConfig } from './config.ts'
import { createLru, type Lru } from './lru.ts'
/** Most resolve answers kept. */
export const RESOLVE_CACHE_MAX = 1000
let cache: Lru<SeoResolveAnswer | null> | null = null
let cacheTtl = -1
function resolveCache(ttlSeconds: number): Lru<SeoResolveAnswer | null> {
if (!cache || cacheTtl !== ttlSeconds) {
cache = createLru<SeoResolveAnswer | null>({ max: RESOLVE_CACHE_MAX, ttlMs: ttlSeconds * 1000 })
cacheTtl = ttlSeconds
}
return cache
}
/** Empties the layer's SEO caches (the resolve answers); the site cache purge calls it. */
export function clearSeoCaches(): void {
cache?.clear()
}
/** The x-forwarded-for value the SEO calls carry for this request. */
export function seoForwardedFor(event: H3Event): Record<string, string> {
@@ -34,5 +57,6 @@ async function fetchResolve(cfg: SeoServerConfig, url: string, headers: Record<s
export async function resolveCached(event: H3Event, url: string): Promise<SeoResolveAnswer | null> {
const cfg = seoServerConfig(useRuntimeConfig(event))
if (!cfg.enabled) return null
return fetchResolve(cfg, url, seoForwardedFor(event))
const headers = seoForwardedFor(event)
return resolveCache(cfg.resolveCacheTtl).load(url, () => fetchResolve(cfg, url, headers), (answer) => answer !== null)
}

View File

@@ -1,10 +1,25 @@
// Resolves every page request through the CMS before Nuxt renders it, so the
// render (and the head) uses one answer and no second call. Skipped: Nuxt's
// and the layers' internal paths (/_nuxt, /__nuxt_error, /_seo …), /api and
// asset files (isPagePath). The answer is left in event.context.seo for the
// app (useSeoResolve reads event.context.seoResolved).
// Resolves every page request through the CMS (resolveCached: LRU and the
// visitor's X-Forwarded-For) and enforces the decision before Nuxt renders
// the page and before Nitro's route cache, so swr-cached pages honour a new
// redirect or 410:
//
// redirect 301 or 302 with Location from the answer (seoRedirectTarget:
// a target on the site's own origin becomes a path, another
// host stays absolute); nothing from the request (Host, path,
// query) ever goes into the Location
// gone 410 with the app's error page
// not_found left to Nuxt's routing, except a pagination.invalid answer
// (?page=abc), which is a 404
//
// X-Robots-Tag follows the answer (staging: noindex, nofollow everywhere;
// else the page's x_robots_tag), never on a redirect. Skipped: Nuxt's and
// the layers' internal paths (/_nuxt, /__nuxt_error, /_seo …), /api and
// asset files (isPagePath). The answer stays in event.context.seo for the
// render, which then makes no second call (useSeoResolve). With the CMS
// unreachable the page renders normally (fail open).
import { isPagePath } from '../../app/utils/pagePath.ts'
import { seoRedirectTarget, seoXRobotsTag } from '../../app/utils/seo.ts'
import { seoServerConfig } from '../lib/config.ts'
import { resolveCached } from '../lib/resolve.ts'
@@ -18,4 +33,15 @@ export default defineEventHandler(async (event) => {
const answer = await resolveCached(event, url)
event.context.seo = answer
event.context.seoResolved = { url, answer }
if (!answer) return
const target = seoRedirectTarget(answer)
if (target) return sendRedirect(event, target.to, target.code)
const robots = seoXRobotsTag(answer)
if (robots) setResponseHeader(event, 'x-robots-tag', robots)
if (answer.action === 'gone') throw createError({ statusCode: 410, statusMessage: 'Gone' })
if (answer.action === 'not_found' && answer.pagination?.invalid === true) {
throw createError({ statusCode: 404, statusMessage: 'Page Not Found' })
}
})