Unverified Commit 5cabe5e2 authored by Juny's avatar Juny Committed by GitHub

fix: set empty document files for amp first (#16934)

Fixes: https://github.com/vercel/next.js/issues/16926

I do not know this is the right way.
Please review it.
parent 64c1f726
...@@ -45,10 +45,13 @@ type DocumentFiles = { ...@@ -45,10 +45,13 @@ type DocumentFiles = {
function getDocumentFiles( function getDocumentFiles(
buildManifest: BuildManifest, buildManifest: BuildManifest,
pathname: string pathname: string,
inAmpMode: boolean
): DocumentFiles { ): DocumentFiles {
const sharedFiles: readonly string[] = getPageFiles(buildManifest, '/_app') const sharedFiles: readonly string[] = getPageFiles(buildManifest, '/_app')
const pageFiles: readonly string[] = getPageFiles(buildManifest, pathname) const pageFiles: readonly string[] = inAmpMode
? []
: getPageFiles(buildManifest, pathname)
return { return {
sharedFiles, sharedFiles,
...@@ -453,8 +456,10 @@ export class Head extends Component< ...@@ -453,8 +456,10 @@ export class Head extends Component<
const files: DocumentFiles = getDocumentFiles( const files: DocumentFiles = getDocumentFiles(
this.context.buildManifest, this.context.buildManifest,
this.context.__NEXT_DATA__.page this.context.__NEXT_DATA__.page,
inAmpMode
) )
return ( return (
<head {...this.props}> <head {...this.props}>
{this.context.isDevelopment && ( {this.context.isDevelopment && (
...@@ -769,8 +774,10 @@ export class NextScript extends Component<OriginProps> { ...@@ -769,8 +774,10 @@ export class NextScript extends Component<OriginProps> {
const files: DocumentFiles = getDocumentFiles( const files: DocumentFiles = getDocumentFiles(
this.context.buildManifest, this.context.buildManifest,
this.context.__NEXT_DATA__.page this.context.__NEXT_DATA__.page,
inAmpMode
) )
return ( return (
<> <>
{!disableRuntimeJS && buildManifest.devFiles {!disableRuntimeJS && buildManifest.devFiles
......
...@@ -29,8 +29,15 @@ const context = {} ...@@ -29,8 +29,15 @@ const context = {}
describe('AMP Usage', () => { describe('AMP Usage', () => {
describe('production mode', () => { describe('production mode', () => {
let output = ''
beforeAll(async () => { beforeAll(async () => {
await nextBuild(appDir) const result = await nextBuild(appDir, undefined, {
stdout: true,
stderr: true,
})
output = result.stdout + result.stderr
app = nextServer({ app = nextServer({
dir: join(__dirname, '../'), dir: join(__dirname, '../'),
dev: false, dev: false,
...@@ -42,6 +49,11 @@ describe('AMP Usage', () => { ...@@ -42,6 +49,11 @@ describe('AMP Usage', () => {
}) })
afterAll(() => stopApp(server)) afterAll(() => stopApp(server))
it('should not contain missing files warning', async () => {
expect(output).toContain('Compiled successfully')
expect(output).not.toContain('Could not find files for')
})
describe('With basic usage', () => { describe('With basic usage', () => {
it('should render the page', async () => { it('should render the page', async () => {
const html = await renderViaHTTP(appPort, '/') const html = await renderViaHTTP(appPort, '/')
...@@ -259,10 +271,18 @@ describe('AMP Usage', () => { ...@@ -259,10 +271,18 @@ describe('AMP Usage', () => {
describe('AMP dev mode', () => { describe('AMP dev mode', () => {
let dynamicAppPort let dynamicAppPort
let ampDynamic let ampDynamic
let output = ''
beforeAll(async () => { beforeAll(async () => {
dynamicAppPort = await findPort() dynamicAppPort = await findPort()
ampDynamic = await launchApp(join(__dirname, '../'), dynamicAppPort) ampDynamic = await launchApp(join(__dirname, '../'), dynamicAppPort, {
onStdout(msg) {
output += msg
},
onStderr(msg) {
output += msg
},
})
}) })
afterAll(() => killApp(ampDynamic)) afterAll(() => killApp(ampDynamic))
...@@ -490,5 +510,11 @@ describe('AMP Usage', () => { ...@@ -490,5 +510,11 @@ describe('AMP Usage', () => {
await browser.close() await browser.close()
} }
}) })
it('should not contain missing files warning', async () => {
expect(output).toContain('compiled successfully')
expect(output).toContain('build page: /only-amp')
expect(output).not.toContain('Could not find files for')
})
}) })
}) })
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