核心价值使用资源更新时间作为一致性依据,客户端携带 `If-Modified-Since` 可获得 304,降低冗余传输。与 `Cache-Control` 协同,在过期后进行条件再验证以保证新鲜度。Route Handler 实现export const runtime = 'edge'
const updatedAt = new Date('2025-11-25T00:00:00Z')
export async function GET(req: Request) {
const ims = req.headers.get('if-modified-since')
const last = updatedAt.toUTCString()
if (ims) {
const since = new Date(ims)
if (!Number.isNaN(since.valueOf()) && updatedAt <= since) {
return new Response(null, {
status: 304,
headers: {
'Last-Modified': last,
'Cache-Control': 'public, max-age=0, must-revalidate, s-maxage=300',
},
})
}
}
const payload = { id: 1, title: 'Post', updatedAt: updatedAt.toISOString() }
const body = JSON.stringify(payload)
return new Response(body, {
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Last-Modified': last,
'Cache-Control': 'public, max-age=0, must-revalidate, s-maxage=300',
},
})
}
要点`Last-Modified` 返回资源更新时间(UTC),客户端在缓存过期后携带 `If-Modified-Since` 进行协商。当服务端更新时间不晚于 `If-Modified-Since` 即返回 304,保持缓存副本有效。与应用协同export default async function Page() {
const res = await fetch('https://example.com/api/posts/1', {
cache: 'force-cache',
next: { revalidate: 120 },
})
const data = await res.json()
return <pre>{JSON.stringify(data, null, 2)}</pre>
}
结论Last-Modified/If-Modified-Since 与 ETag/If-None-Match 可互为补充,配合 Next.js 缓存策略在高并发场景下有效降低响应体传输并保障一致性。

发表评论 取消回复