Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure to copy AMP SSG files during export #11331

Merged
merged 4 commits into from Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/next/export/index.ts
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
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)
})
})
})