Unverified Commit 75c6b100 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Fix: only apply metadata dynamic image routes convention for app dir (#47367)

We shouldn't detect icon/og/etc. metadata image convention as image dynamic routes under `pages/` dir, they should be only apply in `app/` dir. This PR changed the normalization rule that we only apply them when page is from `app/`. So when you're using `icon.js` under `pages/` it won't get effected.
parent 86bf30a4
...@@ -111,7 +111,8 @@ export function createPagesMapping({ ...@@ -111,7 +111,8 @@ export function createPagesMapping({
) )
) )
const route = normalizeMetadataRoute(pageKey) const route =
pagesType === 'app' ? normalizeMetadataRoute(pageKey) : pageKey
result[route] = normalizedPath result[route] = normalizedPath
return result return result
}, },
......
...@@ -492,9 +492,10 @@ export default class DevServer extends Server { ...@@ -492,9 +492,10 @@ export default class DevServer extends Server {
devPageFiles.add(fileName) devPageFiles.add(fileName)
const rootFile = absolutePathToPage(fileName, { const rootFile = absolutePathToPage(fileName, {
pagesDir: this.dir, dir: this.dir,
extensions: this.nextConfig.pageExtensions, extensions: this.nextConfig.pageExtensions,
keepIndex: false, keepIndex: false,
pagesType: 'root',
}) })
const staticInfo = await getPageStaticInfo({ const staticInfo = await getPageStaticInfo({
...@@ -531,9 +532,10 @@ export default class DevServer extends Server { ...@@ -531,9 +532,10 @@ export default class DevServer extends Server {
} }
let pageName = absolutePathToPage(fileName, { let pageName = absolutePathToPage(fileName, {
pagesDir: isAppPath ? this.appDir! : this.pagesDir!, dir: isAppPath ? this.appDir! : this.pagesDir!,
extensions: this.nextConfig.pageExtensions, extensions: this.nextConfig.pageExtensions,
keepIndex: isAppPath, keepIndex: isAppPath,
pagesType: isAppPath ? 'app' : 'pages',
}) })
if ( if (
......
...@@ -20,12 +20,11 @@ describe('AbsoluteFilenameNormalizer', () => { ...@@ -20,12 +20,11 @@ describe('AbsoluteFilenameNormalizer', () => {
])( ])(
"normalizes '$pathname' to '$expected'", "normalizes '$pathname' to '$expected'",
({ pathname, expected, name }) => { ({ pathname, expected, name }) => {
const normalizer = new AbsoluteFilenameNormalizer(`<root>/${name}`, [ const normalizer = new AbsoluteFilenameNormalizer(
'ts', `<root>/${name}`,
'tsx', ['ts', 'tsx', 'js', 'jsx'],
'js', name as 'app' | 'pages' | 'root'
'jsx', )
])
expect(normalizer.normalize(pathname)).toEqual(expected) expect(normalizer.normalize(pathname)).toEqual(expected)
} }
......
...@@ -14,14 +14,16 @@ export class AbsoluteFilenameNormalizer implements Normalizer { ...@@ -14,14 +14,16 @@ export class AbsoluteFilenameNormalizer implements Normalizer {
*/ */
constructor( constructor(
private readonly dir: string, private readonly dir: string,
private readonly extensions: ReadonlyArray<string> private readonly extensions: ReadonlyArray<string>,
private readonly pagesType: 'pages' | 'app' | 'root'
) {} ) {}
public normalize(pathname: string): string { public normalize(pathname: string): string {
return absolutePathToPage(pathname, { return absolutePathToPage(pathname, {
extensions: this.extensions, extensions: this.extensions,
keepIndex: false, keepIndex: false,
pagesDir: this.dir, dir: this.dir,
pagesType: this.pagesType,
}) })
} }
} }
...@@ -28,7 +28,11 @@ export class DevAppPageRouteMatcherProvider extends FileCacheRouteMatcherProvide ...@@ -28,7 +28,11 @@ export class DevAppPageRouteMatcherProvider extends FileCacheRouteMatcherProvide
// directory. // directory.
this.expression = new RegExp(`[/\\\\]page\\.(?:${extensions.join('|')})$`) this.expression = new RegExp(`[/\\\\]page\\.(?:${extensions.join('|')})$`)
const pageNormalizer = new AbsoluteFilenameNormalizer(appDir, extensions) const pageNormalizer = new AbsoluteFilenameNormalizer(
appDir,
extensions,
'app'
)
this.normalizers = { this.normalizers = {
page: pageNormalizer, page: pageNormalizer,
......
...@@ -24,7 +24,11 @@ export class DevAppRouteRouteMatcherProvider extends FileCacheRouteMatcherProvid ...@@ -24,7 +24,11 @@ export class DevAppRouteRouteMatcherProvider extends FileCacheRouteMatcherProvid
) { ) {
super(appDir, reader) super(appDir, reader)
const pageNormalizer = new AbsoluteFilenameNormalizer(appDir, extensions) const pageNormalizer = new AbsoluteFilenameNormalizer(
appDir,
extensions,
'app'
)
this.normalizers = { this.normalizers = {
page: pageNormalizer, page: pageNormalizer,
......
...@@ -34,7 +34,11 @@ export class DevPagesAPIRouteMatcherProvider extends FileCacheRouteMatcherProvid ...@@ -34,7 +34,11 @@ export class DevPagesAPIRouteMatcherProvider extends FileCacheRouteMatcherProvid
// pages directory. // pages directory.
this.expression = new RegExp(`\\.(?:${extensions.join('|')})$`) this.expression = new RegExp(`\\.(?:${extensions.join('|')})$`)
const pageNormalizer = new AbsoluteFilenameNormalizer(pagesDir, extensions) const pageNormalizer = new AbsoluteFilenameNormalizer(
pagesDir,
extensions,
'pages'
)
const bundlePathNormalizer = new Normalizers([ const bundlePathNormalizer = new Normalizers([
pageNormalizer, pageNormalizer,
......
...@@ -34,7 +34,11 @@ export class DevPagesRouteMatcherProvider extends FileCacheRouteMatcherProvider< ...@@ -34,7 +34,11 @@ export class DevPagesRouteMatcherProvider extends FileCacheRouteMatcherProvider<
// pages directory. // pages directory.
this.expression = new RegExp(`\\.(?:${extensions.join('|')})$`) this.expression = new RegExp(`\\.(?:${extensions.join('|')})$`)
const pageNormalizer = new AbsoluteFilenameNormalizer(pagesDir, extensions) const pageNormalizer = new AbsoluteFilenameNormalizer(
pagesDir,
extensions,
'pages'
)
this.normalizers = { this.normalizers = {
page: pageNormalizer, page: pageNormalizer,
......
...@@ -14,26 +14,27 @@ import { normalizeMetadataRoute } from '../../../lib/metadata/get-metadata-route ...@@ -14,26 +14,27 @@ import { normalizeMetadataRoute } from '../../../lib/metadata/get-metadata-route
* - `/Users/rick/my-project/app/sitemap.js` -> `/sitemap/route` * - `/Users/rick/my-project/app/sitemap.js` -> `/sitemap/route`
* *
* @param filepath Absolute path to the page. * @param filepath Absolute path to the page.
* @param opts.pagesDir Absolute path to the pages folder. * @param opts.dir Absolute path to the pages/app folder.
* @param opts.extensions Extensions allowed for the page. * @param opts.extensions Extensions allowed for the page.
* @param opts.keepIndex When true the trailing `index` kept in the path. * @param opts.keepIndex When true the trailing `index` kept in the path.
* @param opts.pagesType Whether the page is in the pages or app directory.
*/ */
export function absolutePathToPage( export function absolutePathToPage(
pagePath: string, pagePath: string,
options: { options: {
extensions: string[] | readonly string[] extensions: string[] | readonly string[]
keepIndex: boolean keepIndex: boolean
pagesDir: string dir: string
pagesType: 'pages' | 'app' | 'root'
} }
) { ) {
const isAppDir = options.pagesType === 'app'
const page = removePagePathTail( const page = removePagePathTail(
normalizePathSep( normalizePathSep(ensureLeadingSlash(path.relative(options.dir, pagePath))),
ensureLeadingSlash(path.relative(options.pagesDir, pagePath))
),
{ {
extensions: options.extensions, extensions: options.extensions,
keepIndex: options.keepIndex, keepIndex: options.keepIndex,
} }
) )
return normalizeMetadataRoute(page) return isAppDir ? normalizeMetadataRoute(page) : page
} }
...@@ -756,5 +756,12 @@ createNextDescribe( ...@@ -756,5 +756,12 @@ createNextDescribe(
} }
}) })
}) })
it('should not effect metadata images convention like files under pages directory', async () => {
const iconHtml = await next.render('/blog/icon')
const ogHtml = await next.render('/blog/opengraph-image')
expect(iconHtml).toContain('pages-icon-page')
expect(ogHtml).toContain('pages-opengraph-image-page')
})
} }
) )
export default function page() {
return <>pages-icon-page</>
}
export default function page() {
return <>pages-opengraph-image-page</>
}
...@@ -17,7 +17,8 @@ ...@@ -17,7 +17,8 @@
{ {
"name": "next" "name": "next"
} }
] ],
"strictNullChecks": true
}, },
"include": ["next-env.d.ts", ".next/types/**/*.ts", "**/*.ts", "**/*.tsx"], "include": ["next-env.d.ts", ".next/types/**/*.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules", "**/*.test.ts"] "exclude": ["node_modules", "**/*.test.ts"]
......
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