Unverified Commit 747e884b authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

test: cover sitemap revalidation in e2e deployments

Co-authored-by: 's avatarNiklas Mischkulnig <4586894+mischnic@users.noreply.github.com>
parent 75718952
import type { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return <p>Sitemap revalidation</p>
}
// Control: a regular route handler with the same revalidation interval.
export const revalidate = 5
export function GET() {
return Response.json({ generatedAt: new Date().toISOString() })
}
import { revalidatePath, revalidateTag } from 'next/cache'
export async function POST(request: Request) {
const type = new URL(request.url).searchParams.get('type')
if (type === 'tag') {
revalidateTag('sitemap-revalidation:tagged', 'max')
} else if (type === 'path') {
revalidatePath('/sitemap-revalidation/tagged/sitemap.xml')
} else {
return new Response('Unknown revalidation type', { status: 400 })
}
return new Response(null, { status: 204 })
}
import type { MetadataRoute } from 'next'
import { unstable_cache } from 'next/cache'
// Only on-demand invalidation should refresh this sitemap.
export const revalidate = false
const getLastModified = unstable_cache(
async () => new Date().toISOString(),
['sitemap-revalidation:tagged'],
{ tags: ['sitemap-revalidation:tagged'], revalidate: false }
)
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
return [
{
url: 'https://example.com/sitemap-revalidation/tagged',
lastModified: await getLastModified(),
},
]
}
import type { MetadataRoute } from 'next'
export const revalidate = 5
export default function sitemap(): MetadataRoute.Sitemap {
return [
{
url: 'https://example.com/sitemap-revalidation',
lastModified: new Date().toISOString(),
},
]
}
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
// This suite exercises route segment revalidate, not Cache Components.
cacheComponents: false,
}
export default nextConfig
import { load } from 'cheerio'
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
// Regression coverage for https://github.com/nextjs/adapter-vercel/issues/150.
// Keep these behavioral checks enabled in deploy mode as well as next start.
describe('sitemap-revalidation', () => {
const { next } = nextTestSetup({
files: __dirname,
})
const taggedSitemap = '/sitemap-revalidation/tagged/sitemap.xml'
async function readLastModified(pathname: string) {
const response = await next.fetch(pathname)
expect(response.status).toBe(200)
expect(response.headers.get('content-type')).toContain('application/xml')
const $ = load(await response.text(), { xmlMode: true })
expect($('urlset > url')).toHaveLength(1)
expect($('urlset > url > loc').text()).toContain(
'https://example.com/sitemap-revalidation'
)
const lastModified = $('urlset > url > lastmod').text()
expect(new Date(lastModified).toISOString()).toBe(lastModified)
return lastModified
}
async function readPlainGeneratedAt() {
const response = await next.fetch('/plain')
expect(response.status).toBe(200)
const { generatedAt } = await response.json()
expect(new Date(generatedAt).toISOString()).toBe(generatedAt)
return generatedAt as string
}
it('serves generated sitemaps', async () => {
await readLastModified('/sitemap.xml')
await readLastModified(taggedSitemap)
})
// Development regenerates on every request, so it cannot verify ISR.
// @force-gate !dev
describe('revalidation', () => {
it('regenerates a sitemap and a regular route handler after their revalidation interval', async () => {
const initialSitemap = await readLastModified('/sitemap.xml')
const initialPlain = await readPlainGeneratedAt()
// Poll through stale-while-revalidate without depending on cache headers
// or an exact boundary between the stale response and regeneration.
await retry(async () => {
expect(await readLastModified('/sitemap.xml')).not.toBe(initialSitemap)
expect(await readPlainGeneratedAt()).not.toBe(initialPlain)
}, 60000)
})
it('regenerates a sitemap after revalidateTag', async () => {
const initial = await readLastModified(taggedSitemap)
expect(await readLastModified(taggedSitemap)).toBe(initial)
const response = await next.fetch('/revalidate?type=tag', {
method: 'POST',
})
expect(response.status).toBe(204)
// This route and its data have no time-based expiry, so a changed value
// proves that tag invalidation reached the cached sitemap response.
await retry(async () => {
expect(await readLastModified(taggedSitemap)).not.toBe(initial)
}, 60000)
})
it('regenerates a sitemap after revalidatePath', async () => {
const initial = await readLastModified(taggedSitemap)
expect(await readLastModified(taggedSitemap)).toBe(initial)
const response = await next.fetch('/revalidate?type=path', {
method: 'POST',
})
expect(response.status).toBe(204)
await retry(async () => {
expect(await readLastModified(taggedSitemap)).not.toBe(initial)
}, 60000)
})
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment