Skip to content

Commit 7fa4946

Browse files
authored
Apply basePath for metadata image (#49226)
Fixes #49162 When user configures the `basePath` in `next.config.js`, we need to apply it to metadata image path resolving fix NEXT-1087
1 parent 7f6e8db commit 7fa4946

File tree

7 files changed

+26
-4
lines changed

7 files changed

+26
-4
lines changed

packages/next/src/build/entries.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,7 @@ export async function createEntrypoints(
553553
appDir,
554554
appPaths: matchedAppPaths,
555555
pageExtensions,
556+
basePath: config.basePath,
556557
assetPrefix: config.assetPrefix,
557558
nextConfigOutput: config.output,
558559
preferredRegion: staticInfo.preferredRegion,
@@ -580,6 +581,7 @@ export async function createEntrypoints(
580581
appDir: appDir!,
581582
appPaths: matchedAppPaths,
582583
pageExtensions,
584+
basePath: config.basePath,
583585
assetPrefix: config.assetPrefix,
584586
nextConfigOutput: config.output,
585587
// This isn't used with edge as it needs to be set on the entry module, which will be the `edgeServerEntry` instead.

packages/next/src/build/webpack/loaders/metadata/discover.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,14 @@ export async function createStaticMetadataFromRoute(
6464
isRootLayoutOrRootPage,
6565
loaderContext,
6666
pageExtensions,
67+
basePath,
6768
}: {
6869
segment: string
6970
resolvePath: (pathname: string) => Promise<string>
7071
isRootLayoutOrRootPage: boolean
7172
loaderContext: webpack.LoaderContext<any>
7273
pageExtensions: string[]
74+
basePath: string
7375
}
7476
) {
7577
let hasStaticMetadataFiles = false
@@ -124,6 +126,7 @@ export async function createStaticMetadataFromRoute(
124126
{
125127
type,
126128
segment,
129+
basePath,
127130
pageExtensions,
128131
}
129132
)}!${filepath}${METADATA_RESOURCE_QUERY}`

packages/next/src/build/webpack/loaders/next-app-loader.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export type AppLoaderOptions = {
3333
rootDir?: string
3434
tsconfigPath?: string
3535
isDev?: boolean
36+
basePath: string
3637
nextConfigOutput?: NextConfig['output']
3738
}
3839
type AppLoader = webpack.LoaderDefinitionFunction<AppLoaderOptions>
@@ -176,6 +177,7 @@ async function createTreeCodeFromPath(
176177
resolveParallelSegments,
177178
loaderContext,
178179
pageExtensions,
180+
basePath,
179181
}: {
180182
resolver: PathResolver
181183
resolvePath: (pathname: string) => Promise<string>
@@ -184,6 +186,7 @@ async function createTreeCodeFromPath(
184186
) => [key: string, segment: string | string[]][]
185187
loaderContext: webpack.LoaderContext<AppLoaderOptions>
186188
pageExtensions: string[]
189+
basePath: string
187190
}
188191
) {
189192
const splittedPath = pagePath.split(/[\\/]/)
@@ -258,6 +261,7 @@ async function createTreeCodeFromPath(
258261

259262
if (resolvedRouteDir) {
260263
metadata = await createStaticMetadataFromRoute(resolvedRouteDir, {
264+
basePath,
261265
segment: segmentPath,
262266
resolvePath,
263267
isRootLayoutOrRootPage,
@@ -427,6 +431,7 @@ const nextAppLoader: AppLoader = async function nextAppLoader() {
427431
isDev,
428432
nextConfigOutput,
429433
preferredRegion,
434+
basePath,
430435
} = loaderOptions
431436

432437
const buildInfo = getModuleBuildInfo((this as any)._module)
@@ -535,6 +540,7 @@ const nextAppLoader: AppLoader = async function nextAppLoader() {
535540
resolveParallelSegments,
536541
loaderContext: this,
537542
pageExtensions,
543+
basePath,
538544
})
539545

540546
if (!rootLayout) {
@@ -599,7 +605,7 @@ const nextAppLoader: AppLoader = async function nextAppLoader() {
599605
export { renderToReadableStream, decodeReply, decodeAction } from 'react-server-dom-webpack/server.edge'
600606
export const __next_app_webpack_require__ = __webpack_require__
601607
export { preloadStyle, preloadFont, preconnect } from 'next/dist/server/app-render/rsc/preloads'
602-
608+
603609
export const originalPathname = "${page}"
604610
`
605611

packages/next/src/build/webpack/loaders/next-metadata-image-loader.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ interface Options {
1717
segment: string
1818
type: PossibleImageFileNameConvention
1919
pageExtensions: string[]
20+
basePath: string
2021
}
2122

2223
async function nextMetadataImageLoader(this: any, content: Buffer) {
2324
const options: Options = this.getOptions()
24-
const { type, segment, pageExtensions } = options
25+
const { type, segment, pageExtensions, basePath } = options
2526
const numericSizes = type === 'twitter' || type === 'openGraph'
2627
const { resourcePath, rootContext: context } = this
2728
const { name: fileNameBase, ext } = path.parse(resourcePath)
@@ -48,6 +49,7 @@ async function nextMetadataImageLoader(this: any, content: Buffer) {
4849
const isDynamicResource = pageExtensions.includes(extension)
4950
const pageSegment = isDynamicResource ? fileNameBase : interpolatedName
5051
const hashQuery = contentHash ? '?' + contentHash : ''
52+
const pathnamePrefix = path.join(basePath, segment)
5153

5254
if (isDynamicResource) {
5355
// re-export and spread as `exportedImageData` to avoid non-exported error
@@ -59,7 +61,7 @@ async function nextMetadataImageLoader(this: any, content: Buffer) {
5961
export default async function (props) {
6062
const { __metadata_id__: _, ...params } = props.params
6163
const imageUrl = fillMetadataSegment(${JSON.stringify(
62-
segment
64+
pathnamePrefix
6365
)}, params, ${JSON.stringify(pageSegment)})
6466
6567
const { generateImageMetadata } = imageModule
@@ -136,7 +138,7 @@ async function nextMetadataImageLoader(this: any, content: Buffer) {
136138
export default (props) => {
137139
const imageData = ${JSON.stringify(imageData)}
138140
const imageUrl = fillMetadataSegment(${JSON.stringify(
139-
segment
141+
pathnamePrefix
140142
)}, props.params, ${JSON.stringify(pageSegment)})
141143
142144
return [{

packages/next/src/server/dev/hot-reloader.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,7 @@ export default class HotReloader {
755755
rootDir: this.dir,
756756
isDev: true,
757757
tsconfigPath: this.config.typescript.tsconfigPath,
758+
basePath: this.config.basePath,
758759
assetPrefix: this.config.assetPrefix,
759760
nextConfigOutput: this.config.output,
760761
preferredRegion: staticInfo.preferredRegion,
@@ -840,6 +841,7 @@ export default class HotReloader {
840841
rootDir: this.dir,
841842
isDev: true,
842843
tsconfigPath: this.config.typescript.tsconfigPath,
844+
basePath: this.config.basePath,
843845
assetPrefix: this.config.assetPrefix,
844846
nextConfigOutput: this.config.output,
845847
preferredRegion: staticInfo.preferredRegion,
Loading

test/e2e/app-dir/app-basepath/index.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,12 @@ createNextDescribe(
2727
.text()
2828
).toBe(`Page 2`)
2929
})
30+
31+
it('should prefix metadata og image with basePath', async () => {
32+
const $ = await next.render$('/base/another')
33+
const ogImageHref = $('meta[property="og:image"]').attr('content')
34+
35+
expect(ogImageHref).toContain('/base/another/opengraph-image.png')
36+
})
3037
}
3138
)

0 commit comments

Comments
 (0)