Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply basePath for metadata image #49226

Merged
merged 2 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/next/src/build/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ export async function createEntrypoints(
appDir,
appPaths: matchedAppPaths,
pageExtensions,
basePath: config.basePath,
assetPrefix: config.assetPrefix,
nextConfigOutput: config.output,
preferredRegion: staticInfo.preferredRegion,
Expand Down Expand Up @@ -580,6 +581,7 @@ export async function createEntrypoints(
appDir: appDir!,
appPaths: matchedAppPaths,
pageExtensions,
basePath: config.basePath,
assetPrefix: config.assetPrefix,
nextConfigOutput: config.output,
// This isn't used with edge as it needs to be set on the entry module, which will be the `edgeServerEntry` instead.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ export async function createStaticMetadataFromRoute(
isRootLayoutOrRootPage,
loaderContext,
pageExtensions,
basePath,
}: {
segment: string
resolvePath: (pathname: string) => Promise<string>
isRootLayoutOrRootPage: boolean
loaderContext: webpack.LoaderContext<any>
pageExtensions: string[]
basePath: string
}
) {
let hasStaticMetadataFiles = false
Expand Down Expand Up @@ -124,6 +126,7 @@ export async function createStaticMetadataFromRoute(
{
type,
segment,
basePath,
pageExtensions,
}
)}!${filepath}${METADATA_RESOURCE_QUERY}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export type AppLoaderOptions = {
rootDir?: string
tsconfigPath?: string
isDev?: boolean
basePath: string
nextConfigOutput?: NextConfig['output']
}
type AppLoader = webpack.LoaderDefinitionFunction<AppLoaderOptions>
Expand Down Expand Up @@ -176,6 +177,7 @@ async function createTreeCodeFromPath(
resolveParallelSegments,
loaderContext,
pageExtensions,
basePath,
}: {
resolver: PathResolver
resolvePath: (pathname: string) => Promise<string>
Expand All @@ -184,6 +186,7 @@ async function createTreeCodeFromPath(
) => [key: string, segment: string | string[]][]
loaderContext: webpack.LoaderContext<AppLoaderOptions>
pageExtensions: string[]
basePath: string
}
) {
const splittedPath = pagePath.split(/[\\/]/)
Expand Down Expand Up @@ -258,6 +261,7 @@ async function createTreeCodeFromPath(

if (resolvedRouteDir) {
metadata = await createStaticMetadataFromRoute(resolvedRouteDir, {
basePath,
segment: segmentPath,
resolvePath,
isRootLayoutOrRootPage,
Expand Down Expand Up @@ -427,6 +431,7 @@ const nextAppLoader: AppLoader = async function nextAppLoader() {
isDev,
nextConfigOutput,
preferredRegion,
basePath,
} = loaderOptions

const buildInfo = getModuleBuildInfo((this as any)._module)
Expand Down Expand Up @@ -535,6 +540,7 @@ const nextAppLoader: AppLoader = async function nextAppLoader() {
resolveParallelSegments,
loaderContext: this,
pageExtensions,
basePath,
})

if (!rootLayout) {
Expand Down Expand Up @@ -599,7 +605,7 @@ const nextAppLoader: AppLoader = async function nextAppLoader() {
export { renderToReadableStream, decodeReply, decodeAction } from 'react-server-dom-webpack/server.edge'
export const __next_app_webpack_require__ = __webpack_require__
export { preloadStyle, preloadFont, preconnect } from 'next/dist/server/app-render/rsc/preloads'

export const originalPathname = "${page}"
`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ interface Options {
segment: string
type: PossibleImageFileNameConvention
pageExtensions: string[]
basePath: string
}

async function nextMetadataImageLoader(this: any, content: Buffer) {
const options: Options = this.getOptions()
const { type, segment, pageExtensions } = options
const { type, segment, pageExtensions, basePath } = options
const numericSizes = type === 'twitter' || type === 'openGraph'
const { resourcePath, rootContext: context } = this
const { name: fileNameBase, ext } = path.parse(resourcePath)
Expand All @@ -48,6 +49,7 @@ async function nextMetadataImageLoader(this: any, content: Buffer) {
const isDynamicResource = pageExtensions.includes(extension)
const pageSegment = isDynamicResource ? fileNameBase : interpolatedName
const hashQuery = contentHash ? '?' + contentHash : ''
const pathnamePrefix = path.join(basePath, segment)

if (isDynamicResource) {
// re-export and spread as `exportedImageData` to avoid non-exported error
Expand All @@ -59,7 +61,7 @@ async function nextMetadataImageLoader(this: any, content: Buffer) {
export default async function (props) {
const { __metadata_id__: _, ...params } = props.params
const imageUrl = fillMetadataSegment(${JSON.stringify(
segment
pathnamePrefix
)}, params, ${JSON.stringify(pageSegment)})

const { generateImageMetadata } = imageModule
Expand Down Expand Up @@ -136,7 +138,7 @@ async function nextMetadataImageLoader(this: any, content: Buffer) {
export default (props) => {
const imageData = ${JSON.stringify(imageData)}
const imageUrl = fillMetadataSegment(${JSON.stringify(
segment
pathnamePrefix
)}, props.params, ${JSON.stringify(pageSegment)})

return [{
Expand Down
2 changes: 2 additions & 0 deletions packages/next/src/server/dev/hot-reloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,7 @@ export default class HotReloader {
rootDir: this.dir,
isDev: true,
tsconfigPath: this.config.typescript.tsconfigPath,
basePath: this.config.basePath,
assetPrefix: this.config.assetPrefix,
nextConfigOutput: this.config.output,
preferredRegion: staticInfo.preferredRegion,
Expand Down Expand Up @@ -840,6 +841,7 @@ export default class HotReloader {
rootDir: this.dir,
isDev: true,
tsconfigPath: this.config.typescript.tsconfigPath,
basePath: this.config.basePath,
assetPrefix: this.config.assetPrefix,
nextConfigOutput: this.config.output,
preferredRegion: staticInfo.preferredRegion,
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions test/e2e/app-dir/app-basepath/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,12 @@ createNextDescribe(
.text()
).toBe(`Page 2`)
})

it('should prefix metadata og image with basePath', async () => {
const $ = await next.render$('/base/another')
const ogImageHref = $('meta[property="og:image"]').attr('content')

expect(ogImageHref).toContain('/base/another/opengraph-image.png')
})
}
)