-
Notifications
You must be signed in to change notification settings - Fork 27.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure hybrid AMP works correctly with SSG (#11205)
* Ensure hybrid AMP works correctly with SSG * Strip AMP from query when not needed
- Loading branch information
Showing
6 changed files
with
182 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { useAmp } from 'next/amp' | ||
|
||
export const config = { | ||
amp: true, | ||
} | ||
|
||
export const getStaticProps = () => { | ||
return { | ||
props: { | ||
hello: 'hello', | ||
random: Math.random(), | ||
}, | ||
} | ||
} | ||
|
||
export default ({ hello, random }) => ( | ||
<> | ||
<p id="use-amp">useAmp: {useAmp() ? 'yes' : 'no'}</p> | ||
<p id="hello">{hello}</p> | ||
<p id="random">{random}</p> | ||
</> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { useAmp } from 'next/amp' | ||
|
||
export const config = { | ||
amp: 'hybrid', | ||
} | ||
|
||
export const getStaticProps = () => { | ||
return { | ||
props: { | ||
hello: 'hello', | ||
random: Math.random(), | ||
}, | ||
} | ||
} | ||
|
||
export default ({ hello, random }) => ( | ||
<> | ||
<p id="use-amp">useAmp: {useAmp() ? 'yes' : 'no'}</p> | ||
<p id="hello">{hello}</p> | ||
<p id="random">{random}</p> | ||
</> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default () => <p>normal old page</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* eslint-env jest */ | ||
/* global jasmine */ | ||
import fs from 'fs-extra' | ||
import { join } from 'path' | ||
import cheerio from 'cheerio' | ||
import { validateAMP } from 'amp-test-utils' | ||
import { | ||
nextBuild, | ||
renderViaHTTP, | ||
findPort, | ||
launchApp, | ||
killApp, | ||
nextStart, | ||
} from 'next-test-utils' | ||
|
||
const appDir = join(__dirname, '../') | ||
const nextConfig = join(appDir, 'next.config.js') | ||
let builtServerPagesDir | ||
let appPort | ||
let app | ||
|
||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2 | ||
|
||
const runTests = (isDev = false) => { | ||
it('should load an amp first page correctly', async () => { | ||
const html = await renderViaHTTP(appPort, '/amp') | ||
|
||
if (!isDev) { | ||
await validateAMP(html) | ||
} | ||
const $ = cheerio.load(html) | ||
expect($('#use-amp').text()).toContain('yes') | ||
}) | ||
|
||
it('should load a hybrid amp page without query correctly', async () => { | ||
const html = await renderViaHTTP(appPort, '/hybrid') | ||
const $ = cheerio.load(html) | ||
expect($('#use-amp').text()).toContain('no') | ||
expect($('#hello').text()).toContain('hello') | ||
}) | ||
|
||
it('should load a hybrid amp page with query correctly', async () => { | ||
const html = await renderViaHTTP(appPort, '/hybrid?amp=1') | ||
|
||
if (!isDev) { | ||
await validateAMP(html) | ||
} | ||
const $ = cheerio.load(html) | ||
expect($('#use-amp').text()).toContain('yes') | ||
expect($('#hello').text()).toContain('hello') | ||
}) | ||
|
||
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 () => { | ||
expect(await fsExists(builtPage('amp.js'))).toBe(true) | ||
expect(await fsExists(builtPage('amp.html'))).toBe(true) | ||
expect(await fsExists(builtPage('amp.json'))).toBe(true) | ||
|
||
expect(await fsExists(builtPage('hybrid.js'))).toBe(true) | ||
expect(await fsExists(builtPage('hybrid.html'))).toBe(true) | ||
expect(await fsExists(builtPage('hybrid.json'))).toBe(true) | ||
|
||
expect(await fsExists(builtPage('hybrid.amp.js'))).toBe(false) | ||
expect(await fsExists(builtPage('hybrid.amp.html'))).toBe(true) | ||
expect(await fsExists(builtPage('hybrid.amp.json'))).toBe(true) | ||
}) | ||
} | ||
} | ||
|
||
describe('AMP SSG Support', () => { | ||
describe('serverless mode', () => { | ||
beforeAll(async () => { | ||
await fs.writeFile( | ||
nextConfig, | ||
` | ||
module.exports = { | ||
target: 'experimental-serverless-trace' | ||
} | ||
` | ||
) | ||
await nextBuild(appDir) | ||
appPort = await findPort() | ||
app = await nextStart(appDir, appPort) | ||
builtServerPagesDir = join(appDir, '.next/serverless/pages') | ||
}) | ||
afterAll(async () => { | ||
await fs.remove(nextConfig) | ||
await killApp(app) | ||
}) | ||
runTests() | ||
}) | ||
describe('server mode', () => { | ||
beforeAll(async () => { | ||
await nextBuild(appDir) | ||
appPort = await findPort() | ||
app = await nextStart(appDir, appPort) | ||
const buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8') | ||
builtServerPagesDir = join( | ||
appDir, | ||
'.next/server/static', | ||
buildId, | ||
'pages' | ||
) | ||
}) | ||
afterAll(() => killApp(app)) | ||
runTests() | ||
}) | ||
describe('dev mode', () => { | ||
beforeAll(async () => { | ||
appPort = await findPort() | ||
app = await launchApp(appDir, appPort) | ||
}) | ||
afterAll(() => killApp(app)) | ||
runTests(true) | ||
}) | ||
}) |