Commit 4ea20efa authored by Cursor Agent's avatar Cursor Agent

test: add test case for sitemap index issue with generateSitemaps (#77304)

This test documents the issue where accessing /sitemap.xml returns 404
when using generateSitemaps() in the root app/sitemap.ts file.

The expected behavior is that /sitemap.xml should return a sitemap index
(sitemapindex) that links to all child sitemaps generated by
generateSitemaps(). Currently, only the individual sitemaps (e.g.
/sitemap/pages.xml) are accessible.

The test verifies:
- Individual sitemaps at /sitemap/[id].xml work correctly
- /sitemap.xml returns 404 (documenting the current buggy behavior)
- Non-existing sitemap IDs return 404
- Sitemaps without .xml extension return 404
parent 633e2749
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return <p>hello world</p>
}
import type { MetadataRoute } from 'next'
/**
* Generate multiple sitemaps using generateSitemaps function.
* This should create:
* - /sitemap.xml - Sitemap index (expected but currently returns 404)
* - /sitemap/pages.xml - Static pages sitemap
* - /sitemap/blog.xml - Blog sitemap
*/
export async function generateSitemaps() {
return [{ id: 'pages' }, { id: 'blog' }]
}
export default async function sitemap(props: {
id: Promise<string>
}): Promise<MetadataRoute.Sitemap> {
const id = await props.id
switch (id) {
case 'pages':
return [
{
url: 'https://example.com',
lastModified: '2024-01-01',
changeFrequency: 'weekly',
priority: 1.0,
},
{
url: 'https://example.com/about',
lastModified: '2024-01-01',
changeFrequency: 'monthly',
priority: 0.8,
},
]
case 'blog':
return [
{
url: 'https://example.com/blog/post-1',
lastModified: '2024-01-01',
changeFrequency: 'weekly',
priority: 0.7,
},
{
url: 'https://example.com/blog/post-2',
lastModified: '2024-01-01',
changeFrequency: 'weekly',
priority: 0.7,
},
]
default:
return []
}
}
import { nextTestSetup } from 'e2e-utils'
describe('dynamic-sitemap-index', () => {
const { next } = nextTestSetup({
files: __dirname,
})
describe('generateSitemaps with root sitemap.ts', () => {
// This test verifies that individual sitemaps work correctly
// when using generateSitemaps in the root app directory
it('should serve individual sitemaps at /sitemap/[id].xml', async () => {
// Test pages sitemap
const pagesRes = await next.fetch('/sitemap/pages.xml')
expect(pagesRes.status).toBe(200)
expect(pagesRes.headers.get('content-type')).toBe('application/xml')
const pagesText = await pagesRes.text()
expect(pagesText).toContain('<loc>https://example.com</loc>')
expect(pagesText).toContain('<loc>https://example.com/about</loc>')
// Test blog sitemap
const blogRes = await next.fetch('/sitemap/blog.xml')
expect(blogRes.status).toBe(200)
expect(blogRes.headers.get('content-type')).toBe('application/xml')
const blogText = await blogRes.text()
expect(blogText).toContain('<loc>https://example.com/blog/post-1</loc>')
expect(blogText).toContain('<loc>https://example.com/blog/post-2</loc>')
})
// TODO: This test documents a known issue (https://github.com/vercel/next.js/issues/77304)
// When using generateSitemaps() in the root sitemap.ts, /sitemap.xml should
// return a sitemap index linking to all child sitemaps, but currently returns 404.
// Once this issue is fixed, update this test to expect 200 and a proper sitemapindex.
it('should return 404 for /sitemap.xml when using generateSitemaps (known issue #77304)', async () => {
const res = await next.fetch('/sitemap.xml')
// Current behavior (bug): /sitemap.xml returns 404
// Expected behavior: /sitemap.xml should return a sitemap index (sitemapindex)
// that links to the child sitemaps generated by generateSitemaps()
//
// When this issue is fixed, this test should be updated to:
// - expect(res.status).toBe(200)
// - expect(res.headers.get('content-type')).toBe('application/xml')
// - expect(text).toContain('sitemapindex')
// - expect(text).toContain('/sitemap/pages.xml')
// - expect(text).toContain('/sitemap/blog.xml')
expect(res.status).toBe(404)
})
it('should return 404 for non-existing sitemap id', async () => {
const res = await next.fetch('/sitemap/non-existing.xml')
expect(res.status).toBe(404)
})
it('should return 404 when accessing sitemap without .xml extension', async () => {
const res = await next.fetch('/sitemap/pages')
expect(res.status).toBe(404)
})
})
})
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}
module.exports = nextConfig
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