Files
vue-seo-layer/server/plugins/seo-client-address.ts

18 lines
846 B
TypeScript

// Keeps the visitor's address available to route-cached SSR renders.
//
// An app rule such as `'/**': { swr }` renders cached pages from a copy of
// the request without headers. The SEO API throttles per client address,
// so forwarding only the Nuxt peer would put every cache miss in one bucket.
// The event context is shared with that copy, so the incoming
// x-forwarded-for plus the peer address is recorded here, before the cached
// handler runs, and resolveSeo and the 404 report forward it.
export default defineNitroPlugin((nitroApp) => {
nitroApp.hooks.hook('request', (event) => {
const forwarded = [getRequestHeader(event, 'x-forwarded-for'), event.node.req.socket?.remoteAddress]
.filter((v): v is string => typeof v === 'string' && v !== '')
.join(', ')
event.context.seoForwardedFor = forwarded
})
})