Skip to content

Commit

Permalink
Make sure to copy AMP SSG files during export (#11331)
Browse files Browse the repository at this point in the history
* Make sure to copy AMP SSG files during export

* bump

* bump
  • Loading branch information
ijjk authored Mar 25, 2020
1 parent e7842af commit 7738050
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
11 changes: 11 additions & 0 deletions packages/next/export/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import findUp from 'find-up'
import {
copyFile as copyFileOrig,
existsSync,
exists as existsOrig,
mkdir as mkdirOrig,
readFileSync,
writeFileSync,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
})
)
}
Expand Down
40 changes: 34 additions & 6 deletions test/integration/amphtml-ssg/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
launchApp,
killApp,
nextStart,
nextExport,
} from 'next-test-utils'

const appDir = join(__dirname, '../')
Expand All @@ -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')
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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)
})
})
})

0 comments on commit 7738050

Please sign in to comment.