Unverified Commit 958150d1 authored by Yamagishi Kazutoshi's avatar Yamagishi Kazutoshi Committed by GitHub

Add suffix to static metadata images (#48202)

### What?

The change in #47985 breaks the URLs of static image files like
`/(group)/opengraph-image.png` to `/opengraph-image.png-012345`.
References from `/` are also broken.

### Why?

This is because only `opengraph-image.ts` and `opengraph-image.tsx` are
considered.

### How?

In this Pull Request, we are trying to solve the problem by including
similar support for `opengraph-image.png` and `opengraph-image.jpeg`.

---------
Co-authored-by: 's avatarJiachi Liu <inbox@huozhi.im>
parent 3003bff6
...@@ -114,19 +114,24 @@ async function nextMetadataImageLoader(this: any, content: Buffer) { ...@@ -114,19 +114,24 @@ async function nextMetadataImageLoader(this: any, content: Buffer) {
import path from 'next/dist/shared/lib/isomorphic/path' import path from 'next/dist/shared/lib/isomorphic/path'
import { interpolateDynamicPath } from 'next/dist/server/server-utils' import { interpolateDynamicPath } from 'next/dist/server/server-utils'
import { getNamedRouteRegex } from 'next/dist/shared/lib/router/utils/route-regex' import { getNamedRouteRegex } from 'next/dist/shared/lib/router/utils/route-regex'
import { getMetadataRouteSuffix } from 'next/dist/lib/metadata/get-metadata-route'
export default (props) => { export default (props) => {
const pathname = ${JSON.stringify(route)} const pathname = ${JSON.stringify(route)}
const routeRegex = getNamedRouteRegex(pathname, false) const routeRegex = getNamedRouteRegex(pathname, false)
const segment = ${JSON.stringify(segment)}
const route = interpolateDynamicPath(pathname, props.params, routeRegex) const route = interpolateDynamicPath(pathname, props.params, routeRegex)
const suffix = getMetadataRouteSuffix(segment)
const routeSuffix = suffix ? \`-\${suffix}\` : ''
const { name, ext } = path.parse(${JSON.stringify(pageRoute)})
const imageData = ${JSON.stringify(imageData)}; const imageData = ${JSON.stringify(imageData)};
return { return {
...imageData, ...imageData,
url: path.join(route, ${JSON.stringify(pageRoute)} + ${JSON.stringify( url: path.join(route, name + routeSuffix + ext + ${JSON.stringify(
hashQuery hashQuery
)}), )}),
} }
}` }`
} }
......
...@@ -47,7 +47,13 @@ export function normalizeMetadataRoute(page: string) { ...@@ -47,7 +47,13 @@ export function normalizeMetadataRoute(page: string) {
// Support both /<metadata-route.ext> and custom routes /<metadata-route>/route.ts. // Support both /<metadata-route.ext> and custom routes /<metadata-route>/route.ts.
// If it's a metadata file route, we need to append /route to the page. // If it's a metadata file route, we need to append /route to the page.
if (!route.endsWith('/route')) { if (!route.endsWith('/route')) {
route = `${route}${suffix ? `-${suffix}` : ''}/route` const { dir, name, ext } = path.parse(route)
route = path.join(
dir,
`${name}${suffix ? `-${suffix}` : ''}${ext}`,
'route'
)
} }
} }
return route return route
......
export default function page() {
return <>static image</>
}
...@@ -137,6 +137,15 @@ createNextDescribe( ...@@ -137,6 +137,15 @@ createNextDescribe(
expect([sizeBig.width, sizeBig.height]).toEqual([1200, 630]) expect([sizeBig.width, sizeBig.height]).toEqual([1200, 630])
expect([sizeSmall.width, sizeSmall.height]).toEqual([600, 315]) expect([sizeSmall.width, sizeSmall.height]).toEqual([600, 315])
}) })
it('should fill params into routes groups url of static images', async () => {
const $ = await next.render$('/static')
const ogImageUrl = $('meta[property="og:image"]').attr('content')
expect(ogImageUrl).toMatch(hashRegex)
expect(ogImageUrl).toMatch('/static/opengraph-image')
// should already normalize the parallel routes segment to url
expect(ogImageUrl).not.toContain('(group)')
})
}) })
describe('icon image routes', () => { describe('icon image routes', () => {
......
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