Unverified Commit 77380507 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Make sure to copy AMP SSG files during export (#11331)

* Make sure to copy AMP SSG files during export

* bump

* bump
parent e7842aff
...@@ -3,6 +3,7 @@ import findUp from 'find-up' ...@@ -3,6 +3,7 @@ import findUp from 'find-up'
import { import {
copyFile as copyFileOrig, copyFile as copyFileOrig,
existsSync, existsSync,
exists as existsOrig,
mkdir as mkdirOrig, mkdir as mkdirOrig,
readFileSync, readFileSync,
writeFileSync, writeFileSync,
...@@ -37,6 +38,7 @@ import { normalizePagePath } from '../next-server/server/normalize-page-path' ...@@ -37,6 +38,7 @@ import { normalizePagePath } from '../next-server/server/normalize-page-path'
const copyFile = promisify(copyFileOrig) const copyFile = promisify(copyFileOrig)
const mkdir = promisify(mkdirOrig) const mkdir = promisify(mkdirOrig)
const exists = promisify(existsOrig)
const createProgress = (total: number, label = 'Exporting') => { const createProgress = (total: number, label = 'Exporting') => {
let curProgress = 0 let curProgress = 0
...@@ -381,12 +383,21 @@ export default async function( ...@@ -381,12 +383,21 @@ export default async function(
subFolders && route !== '/index' ? `${sep}index` : '' subFolders && route !== '/index' ? `${sep}index` : ''
}.html` }.html`
) )
const ampHtmlDest = join(
outDir,
`${route}.amp${subFolders ? `${sep}index` : ''}.html`
)
const jsonDest = join(pagesDataDir, `${route}.json`) const jsonDest = join(pagesDataDir, `${route}.json`)
await mkdir(dirname(htmlDest), { recursive: true }) await mkdir(dirname(htmlDest), { recursive: true })
await mkdir(dirname(jsonDest), { recursive: true }) await mkdir(dirname(jsonDest), { recursive: true })
await copyFile(`${orig}.html`, htmlDest) await copyFile(`${orig}.html`, htmlDest)
await copyFile(`${orig}.json`, jsonDest) await copyFile(`${orig}.json`, jsonDest)
if (await exists(`${orig}.amp.html`)) {
await mkdir(dirname(ampHtmlDest), { recursive: true })
await copyFile(`${orig}.amp.html`, ampHtmlDest)
}
}) })
) )
} }
......
...@@ -11,6 +11,7 @@ import { ...@@ -11,6 +11,7 @@ import {
launchApp, launchApp,
killApp, killApp,
nextStart, nextStart,
nextExport,
} from 'next-test-utils' } from 'next-test-utils'
const appDir = join(__dirname, '../') const appDir = join(__dirname, '../')
...@@ -21,6 +22,12 @@ let app ...@@ -21,6 +22,12 @@ let app
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2 jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
const fsExists = file =>
fs
.access(file)
.then(() => true)
.catch(() => false)
const runTests = (isDev = false) => { const runTests = (isDev = false) => {
it('should load an amp first page correctly', async () => { it('should load an amp first page correctly', async () => {
const html = await renderViaHTTP(appPort, '/amp') const html = await renderViaHTTP(appPort, '/amp')
...@@ -51,12 +58,6 @@ const runTests = (isDev = false) => { ...@@ -51,12 +58,6 @@ const runTests = (isDev = false) => {
}) })
if (!isDev) { if (!isDev) {
const fsExists = file =>
fs
.access(file)
.then(() => true)
.catch(() => false)
const builtPage = file => join(builtServerPagesDir, file) const builtPage = file => join(builtServerPagesDir, file)
it('should output prerendered files correctly during build', async () => { it('should output prerendered files correctly during build', async () => {
...@@ -121,4 +122,31 @@ describe('AMP SSG Support', () => { ...@@ -121,4 +122,31 @@ describe('AMP SSG Support', () => {
afterAll(() => killApp(app)) afterAll(() => killApp(app))
runTests(true) 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)
})
})
}) })
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