Unverified Commit 4a6319cd authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Respect the original exported route segment config for sitemap (#56007)

Re-export the original route segment config in metadata routes loader, so they can be picked up by app route module wrapper

Closes #54057
Closes NEXT-1645
parent ec9a87af
......@@ -36,7 +36,10 @@ function getContentType(resourcePath: string) {
}
// Strip metadata resource query string from `import.meta.url` to make sure the fs.readFileSync get the right path.
async function getStaticRouteCode(resourcePath: string, fileBaseName: string) {
async function getStaticAssetRouteCode(
resourcePath: string,
fileBaseName: string
) {
const cache =
fileBaseName === 'favicon'
? 'public, max-age=0, must-revalidate'
......@@ -93,9 +96,9 @@ export async function GET() {
function getDynamicImageRouteCode(resourcePath: string) {
return `\
import { NextResponse } from 'next/server'
import * as _imageModule from ${JSON.stringify(resourcePath)}
import * as userland from ${JSON.stringify(resourcePath)}
const imageModule = { ..._imageModule }
const imageModule = { ...userland }
const handler = imageModule.default
const generateImageMetadata = imageModule.generateImageMetadata
......@@ -148,15 +151,18 @@ export async function generateStaticParams() {
const code = `\
import { NextResponse } from 'next/server'
import * as _sitemapModule from ${JSON.stringify(resourcePath)}
import * as userland from ${JSON.stringify(resourcePath)}
import { resolveRouteData } from 'next/dist/build/webpack/loaders/metadata/resolve-route-data'
const sitemapModule = { ..._sitemapModule }
const sitemapModule = { ...userland }
const handler = sitemapModule.default
const generateSitemaps = sitemapModule.generateSitemaps
const contentType = ${JSON.stringify(getContentType(resourcePath))}
const fileType = ${JSON.stringify(getFilenameAndExtension(resourcePath).name)}
${'' /* re-export the userland route configs */}
export * from ${JSON.stringify(resourcePath)}
export async function GET(_, ctx) {
const { __metadata_id__ = [], ...params } = ctx.params || {}
const targetId = __metadata_id__[0]
......@@ -213,7 +219,7 @@ const nextMetadataRouterLoader: webpack.LoaderDefinitionFunction<MetadataRouteLo
code = getDynamicImageRouteCode(resourcePath)
}
} else {
code = await getStaticRouteCode(resourcePath, fileBaseName)
code = await getStaticAssetRouteCode(resourcePath, fileBaseName)
}
return code
......
import 'server-only'
import { MetadataRoute } from 'next'
export default function sitemap(): MetadataRoute.Sitemap {
return [
{
url: 'https://example.com',
lastModified: '2021-01-01',
changeFrequency: 'weekly',
priority: 0.5,
},
{
url: 'https://example.com/about',
lastModified: '2021-01-01',
},
]
}
export const dynamic = 'force-dynamic'
......@@ -280,6 +280,25 @@ createNextDescribe(
})
})
if (isNextStart) {
describe('route segment config', () => {
it('should generate dynamic route if dynamic config is force-dynamic', async () => {
const dynamicRoute = '/route-config/sitemap.xml'
expect(
await next.hasFile(`.next/server/app${dynamicRoute}/route.js`)
).toBe(true)
// dynamic routes should not have body and meta files
expect(
await next.hasFile(`.next/server/app${dynamicRoute}.body`)
).toBe(false)
expect(
await next.hasFile(`.next/server/app${dynamicRoute}.meta`)
).toBe(false)
})
})
}
it('should generate unique path for image routes under group routes', async () => {
const $ = await next.render$('/blog')
const ogImageUrl = $('meta[property="og:image"]').attr('content')
......
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