Unverified Commit b6ff1f60 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix adapter outputs for dynamic metadata routes (#91680)

## Summary

This fixes adapter `onBuildComplete` outputs for dynamic metadata
routes.

## Bug

`handleBuildComplete` skipped metadata routes too aggressively via
`isStaticMetadataFile(...)`, so dynamic metadata routes (for example
`robots.txt` / `sitemap.xml` using `connection()`) were omitted from
`outputs.appRoutes`.

## Fix

Only skip metadata routes when they are actually prerendered/static
(present in prerender manifests). Dynamic metadata routes are now
included in adapter `outputs.appRoutes` as expected.

## Tests

Added e2e coverage in `test/e2e/app-dir/adapter-dynamic-metadata`:
- verifies dynamic `robots.txt`, `sitemap.xml`, and `favicon.ico`
functionality
- verifies adapter output classification for non-deploy runs
- skips output-shape verification in deploy mode
- skips the suite in dev mode
parent 546250ec
......@@ -1070,8 +1070,26 @@ export async function handleBuildComplete({
}
const normalizedPage = normalizeAppPath(page)
// Skip static metadata routes - they will be output as static files
if (isStaticMetadataFile(normalizedPage)) {
// Skip static metadata routes only when they are prerendered.
// Dynamic metadata routes (e.g. robots/sitemap using connection())
// should remain app routes in adapter outputs.
const isStaticMetadataRoute = isStaticMetadataFile(normalizedPage)
const isPrerenderedMetadataRoute =
prerenderManifest.routes[normalizedPage] ||
prerenderManifest.dynamicRoutes[normalizedPage] ||
config.i18n?.locales?.some((locale) => {
const localePathname = path.posix.join(
'/',
locale,
normalizedPage.slice(1)
)
return (
prerenderManifest.routes[localePathname] ||
prerenderManifest.dynamicRoutes[localePathname]
)
})
if (isStaticMetadataRoute && isPrerenderedMetadataRoute) {
continue
}
const pageFile = path.join(appDistDir, `${page}.js`)
......
import type { NextAdapter } from 'next'
import { nextTestSetup } from 'e2e-utils'
describe('adapter-dynamic-metadata', () => {
const { next, isNextDev, isNextDeploy } = nextTestSetup({
files: __dirname,
})
if (isNextDev) {
it('should skip next dev', () => {})
return
}
if (!isNextDeploy) {
it('should classify dynamic metadata routes as app routes in adapter outputs', async () => {
const { outputs }: Parameters<NextAdapter['onBuildComplete']>[0] =
await next.readJSON('build-complete.json')
const expectedDynamicMetadataRoutes = [
'/robots.txt',
'/sitemap.xml',
'/favicon.ico',
]
const staticFilePathnames = outputs.staticFiles.map(
(item) => item.pathname
)
const prerenderPathnames = outputs.prerenders.map((item) => item.pathname)
const appRoutePathnames = outputs.appRoutes.map((item) => item.pathname)
expect(staticFilePathnames).toEqual(
expect.not.arrayContaining(expectedDynamicMetadataRoutes)
)
expect(prerenderPathnames).toEqual(
expect.not.arrayContaining(expectedDynamicMetadataRoutes)
)
expect(appRoutePathnames).toEqual(
expect.arrayContaining(expectedDynamicMetadataRoutes)
)
for (const pathname of expectedDynamicMetadataRoutes) {
const appRoute = outputs.appRoutes.find(
(item) => item.pathname === pathname
)
expect(appRoute?.runtime).toBe('nodejs')
}
})
}
it('should serve dynamic metadata routes', async () => {
const robots = await next.fetch('/robots.txt')
expect(robots.status).toBe(200)
expect(robots.headers.get('content-type')).toContain('text/plain')
expect(await robots.text()).toContain('User-Agent: *')
const sitemap = await next.fetch('/sitemap.xml')
expect(sitemap.status).toBe(200)
expect(sitemap.headers.get('content-type')).toBe('application/xml')
expect(await sitemap.text()).toContain('<urlset')
const favicon = await next.fetch('/favicon.ico')
expect(favicon.status).toBe(200)
expect(favicon.headers.get('content-type')).toContain('image/x-icon')
expect(await favicon.text()).toContain('dynamic favicon')
})
})
import { connection } from 'next/server'
export async function GET() {
await connection()
return new Response('dynamic favicon', {
headers: {
'content-type': 'image/x-icon',
},
})
}
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'
import { connection } from 'next/server'
export default async function robots(): Promise<MetadataRoute.Robots> {
await connection()
return {
rules: {
userAgent: '*',
allow: '/',
},
sitemap: 'https://example.com/sitemap.xml',
}
}
import type { MetadataRoute } from 'next'
import { connection } from 'next/server'
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
await connection()
return [
{
url: 'https://example.com',
},
]
}
import fs from 'fs'
/** @type {import('next').NextAdapter} */
const adapter = {
name: 'adapter-dynamic-metadata',
onBuildComplete: async (ctx) => {
await fs.promises.writeFile(
'build-complete.json',
JSON.stringify(ctx, null, 2)
)
},
}
export default adapter
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}
if (!process.env.NEXT_ADAPTER_PATH) {
nextConfig.adapterPath = require.resolve('./my-adapter.mjs')
}
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