fix: forward the visitor address on route-cached renders

Nitro's cached handler renders from a header-less copy of the request, so
under swr route rules useJournalApi, /rss.xml and the sitemap source
forwarded only the Nuxt peer and every cache miss shared one Journal API
rate-limit bucket (deferred in Phase 16). A Nitro plugin records
x-forwarded-for plus the peer in event.context.journalForwardedFor on the
request hook; the three callers forward it. Same key as the host app's
workaround, which keeps working. The acme SSR smoke's x-forwarded-for check
on the now route-cached playground is the test.
This commit is contained in:
2026-10-10 18:40:25 +02:00
parent 4f55511ece
commit 03a90e952d
5 changed files with 42 additions and 10 deletions

View File

@@ -2,7 +2,7 @@
// the internal base (runtimeConfig.journal.apiBase) and forwards the visitor's
// x-forwarded-for, plus the address of the peer that reached Nuxt, so the Go
// rate limiter keeps one bucket per visitor instead of one for the Nuxt
// server. In the browser it calls the same-origin public base. The base is
// server, also on route-cached renders. In the browser it calls the same-origin public base. The base is
// always runtime config, never request input.
import { createJournalClient, type JournalClient, type JournalFetcher } from '../utils/client.ts'
@@ -12,9 +12,13 @@ export function useJournalApi(): JournalClient {
if (import.meta.server) {
const base = (config.journal as { apiBase: string }).apiBase
// Under route caching the request copy has no headers; the address the
// layer's server/plugins/journal-client-address.ts recorded comes first.
const event = useRequestEvent()
const recorded = event?.context.journalForwardedFor as string | undefined
const incoming = useRequestHeaders(['x-forwarded-for'])['x-forwarded-for']
const peer = useRequestEvent()?.node?.req?.socket?.remoteAddress
const forwarded = [incoming, peer].filter((v): v is string => typeof v === 'string' && v !== '').join(', ')
const peer = event?.node?.req?.socket?.remoteAddress
const forwarded = recorded || [incoming, peer].filter((v): v is string => typeof v === 'string' && v !== '').join(', ')
const headers: Record<string, string> = forwarded ? { 'x-forwarded-for': forwarded } : {}
const fetcher: JournalFetcher = (path, query) => $fetch(path, { baseURL: base, query, headers })
return createJournalClient(fetcher)