diff --git a/packages/next/export/index.ts b/packages/next/export/index.ts index bd930d44fa28802..40c740a29449bc5 100644 --- a/packages/next/export/index.ts +++ b/packages/next/export/index.ts @@ -3,6 +3,7 @@ import findUp from 'find-up' import { copyFile as copyFileOrig, existsSync, + exists as existsOrig, mkdir as mkdirOrig, readFileSync, writeFileSync, @@ -37,6 +38,7 @@ import { normalizePagePath } from '../next-server/server/normalize-page-path' const copyFile = promisify(copyFileOrig) const mkdir = promisify(mkdirOrig) +const exists = promisify(existsOrig) const createProgress = (total: number, label = 'Exporting') => { let curProgress = 0 @@ -381,12 +383,21 @@ export default async function( subFolders && route !== '/index' ? `${sep}index` : '' }.html` ) + const ampHtmlDest = join( + outDir, + `${route}.amp${subFolders ? `${sep}index` : ''}.html` + ) const jsonDest = join(pagesDataDir, `${route}.json`) await mkdir(dirname(htmlDest), { recursive: true }) await mkdir(dirname(jsonDest), { recursive: true }) await copyFile(`${orig}.html`, htmlDest) await copyFile(`${orig}.json`, jsonDest) + + if (await exists(`${orig}.amp.html`)) { + await mkdir(dirname(ampHtmlDest), { recursive: true }) + await copyFile(`${orig}.amp.html`, ampHtmlDest) + } }) ) } diff --git a/test/integration/amphtml-ssg/test/index.test.js b/test/integration/amphtml-ssg/test/index.test.js index 65b96b6229534ad..e1d05d4b5ea8bbb 100644 --- a/test/integration/amphtml-ssg/test/index.test.js +++ b/test/integration/amphtml-ssg/test/index.test.js @@ -11,6 +11,7 @@ import { launchApp, killApp, nextStart, + nextExport, } from 'next-test-utils' const appDir = join(__dirname, '../') @@ -21,6 +22,12 @@ let app jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2 +const fsExists = file => + fs + .access(file) + .then(() => true) + .catch(() => false) + const runTests = (isDev = false) => { it('should load an amp first page correctly', async () => { const html = await renderViaHTTP(appPort, '/amp') @@ -51,12 +58,6 @@ const runTests = (isDev = false) => { }) if (!isDev) { - const fsExists = file => - fs - .access(file) - .then(() => true) - .catch(() => false) - const builtPage = file => join(builtServerPagesDir, file) it('should output prerendered files correctly during build', async () => { @@ -121,4 +122,31 @@ describe('AMP SSG Support', () => { afterAll(() => killApp(app)) runTests(true) }) + describe('export mode', () => { + let buildId + + beforeAll(async () => { + await nextBuild(appDir) + await nextExport(appDir, { outdir: join(appDir, 'out') }) + buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8') + }) + + it('should have copied SSG files correctly', async () => { + const outFile = file => join(appDir, 'out', file) + + expect(await fsExists(outFile('amp.html'))).toBe(true) + expect(await fsExists(outFile('index.html'))).toBe(true) + expect(await fsExists(outFile('hybrid.html'))).toBe(true) + expect(await fsExists(outFile('amp.amp.html'))).toBe(false) + expect(await fsExists(outFile('hybrid.amp.html'))).toBe(true) + + expect( + await fsExists(outFile(join('_next/data', buildId, 'amp.json'))) + ).toBe(true) + + expect( + await fsExists(outFile(join('_next/data', buildId, 'hybrid.json'))) + ).toBe(true) + }) + }) })