Skip to content

Commit

Permalink
Apply metadata next route loader directly to source files
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Jun 10, 2024
1 parent 1cb3a0b commit 16447fb
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 4 deletions.
3 changes: 1 addition & 2 deletions packages/next/src/build/webpack/loaders/next-app-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,8 @@ async function createAppRouteCode({

resolvedPagePath = `next-metadata-route-loader?${stringify({
page,
filePath: resolvedPagePath,
isDynamic: isDynamic ? '1' : '0',
})}!?${WEBPACK_RESOURCE_QUERIES.metadataRoute}`
})}!${resolvedPagePath}?${WEBPACK_RESOURCE_QUERIES.metadataRoute}`
}

const pathname = new AppPathnameNormalizer().normalize(page)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const cacheHeader = {

type MetadataRouteLoaderOptions = {
page: string
filePath: string
isDynamic: '1' | '0'
}

Expand Down Expand Up @@ -254,7 +253,8 @@ ${staticGenerationCode}
// TODO-METADATA: improve the cache control strategy
const nextMetadataRouterLoader: webpack.LoaderDefinitionFunction<MetadataRouteLoaderOptions> =
async function () {
const { page, isDynamic, filePath } = this.getOptions()
const { page, isDynamic } = this.getOptions()
const filePath = this.resourcePath
const { name: fileBaseName } = getFilenameAndExtension(filePath)

let code = ''
Expand Down
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { nextTestSetup } from 'e2e-utils'
import crypto from 'crypto'

function generateMD5(text: string) {
const hash = crypto.createHash('md5')
hash.update(text)
return hash.digest('hex')
}

describe('app dir - metadata static routes cache', () => {
const { next } = nextTestSetup({
files: __dirname,
skipStart: true,
})

it('should generate different content after replace the static metadata file', async () => {
await next.build()

const faviconBuildContent = await next.readFile(
'.next/server/app/favicon.ico.body'
)
const opengrpahImageBuildContent = await next.readFile(
'.next/server/app/opengraph-image.png.body'
)

const faviconMd5 = generateMD5(faviconBuildContent)
const opengraphImageMd5 = generateMD5(opengrpahImageBuildContent)

// Update favicon and opengraph image
const newFaviconContent = await next.readFile('app/favicon.ico.new')
await next.patchFile('app/favicon.ico', newFaviconContent)

const newOpengraphImageContent = await next.readFile(
'app/opengraph-image.png.new'
)
await next.patchFile('app/opengraph-image.png', newOpengraphImageContent)

await next.build()
const faviconBuildContentNew = await next.readFile(
'.next/server/app/favicon.ico.body'
)
const opengrpahImageBuildContentNew = await next.readFile(
'.next/server/app/opengraph-image.png.body'
)

const faviconMd5New = generateMD5(faviconBuildContentNew)
const opengraphImageMd5New = generateMD5(opengrpahImageBuildContentNew)

expect(faviconMd5).not.toBe(faviconMd5New)
expect(opengraphImageMd5).not.toBe(opengraphImageMd5New)
})
})

0 comments on commit 16447fb

Please sign in to comment.