Unverified Commit 5d13e55a authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure modern AMP client page bundles are removed (#12138)

parent 0a3b64d2
...@@ -551,15 +551,18 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -551,15 +551,18 @@ export default async function build(dir: string, conf = null): Promise<void> {
if (result.isAmpOnly) { if (result.isAmpOnly) {
// ensure all AMP only bundles got removed // ensure all AMP only bundles got removed
try { try {
await fsUnlink( const clientBundle = path.join(
path.join(
distDir, distDir,
'static', 'static',
buildId, buildId,
'pages', 'pages',
actualPage + '.js' actualPage + '.js'
) )
) await fsUnlink(clientBundle)
if (config.experimental.modern) {
await fsUnlink(clientBundle.replace(/\.js$/, '.module.js'))
}
} catch (err) { } catch (err) {
if (err.code !== 'ENOENT') { if (err.code !== 'ENOENT') {
throw err throw err
......
...@@ -6,4 +6,5 @@ module.exports = { ...@@ -6,4 +6,5 @@ module.exports = {
amp: { amp: {
canonicalBase: 'http://localhost:1234', canonicalBase: 'http://localhost:1234',
}, },
// edit here
} }
...@@ -4,7 +4,13 @@ import { join } from 'path' ...@@ -4,7 +4,13 @@ import { join } from 'path'
import cheerio from 'cheerio' import cheerio from 'cheerio'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import { validateAMP } from 'amp-test-utils' import { validateAMP } from 'amp-test-utils'
import { accessSync, readFileSync, writeFileSync } from 'fs' import {
accessSync,
readFileSync,
writeFileSync,
writeFile,
readFile,
} from 'fs-extra'
import { import {
waitFor, waitFor,
nextServer, nextServer,
...@@ -28,6 +34,7 @@ jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5 ...@@ -28,6 +34,7 @@ jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5
const context = {} const context = {}
describe('AMP Usage', () => { describe('AMP Usage', () => {
describe('production mode', () => {
beforeAll(async () => { beforeAll(async () => {
await nextBuild(appDir) await nextBuild(appDir)
app = nextServer({ app = nextServer({
...@@ -69,11 +76,19 @@ describe('AMP Usage', () => { ...@@ -69,11 +76,19 @@ describe('AMP Usage', () => {
const ampOnly = ['only-amp', 'root-hmr', 'another-amp'] const ampOnly = ['only-amp', 'root-hmr', 'another-amp']
for (const pg of ampOnly) { for (const pg of ampOnly) {
expect(() => expect(() =>
accessSync(join(appDir, '.next/static', buildId, 'pages', pg + '.js')) accessSync(
join(appDir, '.next/static', buildId, 'pages', pg + '.js')
)
).toThrow() ).toThrow()
expect(() => expect(() =>
accessSync( accessSync(
join(appDir, '.next/server/static', buildId, 'pages', pg + '.html') join(
appDir,
'.next/server/static',
buildId,
'pages',
pg + '.html'
)
) )
).not.toThrow() ).not.toThrow()
expect(() => expect(() =>
...@@ -92,7 +107,9 @@ describe('AMP Usage', () => { ...@@ -92,7 +107,9 @@ describe('AMP Usage', () => {
$( $(
$('link[rel=preload]') $('link[rel=preload]')
.toArray() .toArray()
.find(i => $(i).attr('href') === 'https://cdn.ampproject.org/v0.js') .find(
i => $(i).attr('href') === 'https://cdn.ampproject.org/v0.js'
)
).attr('href') ).attr('href')
).toBe('https://cdn.ampproject.org/v0.js') ).toBe('https://cdn.ampproject.org/v0.js')
}) })
...@@ -202,14 +219,18 @@ describe('AMP Usage', () => { ...@@ -202,14 +219,18 @@ describe('AMP Usage', () => {
const html = await renderViaHTTP(appPort, '/conflicting-tag?amp=1') const html = await renderViaHTTP(appPort, '/conflicting-tag?amp=1')
const $ = cheerio.load(html) const $ = cheerio.load(html)
await validateAMP(html) await validateAMP(html)
expect($('meta[name=viewport]').attr('content')).not.toBe('something :p') expect($('meta[name=viewport]').attr('content')).not.toBe(
'something :p'
)
}) })
it('should allow manually setting canonical', async () => { it('should allow manually setting canonical', async () => {
const html = await renderViaHTTP(appPort, '/manual-rels?amp=1') const html = await renderViaHTTP(appPort, '/manual-rels?amp=1')
const $ = cheerio.load(html) const $ = cheerio.load(html)
await validateAMP(html) await validateAMP(html)
expect($('link[rel=canonical]').attr('href')).toBe('/my-custom-canonical') expect($('link[rel=canonical]').attr('href')).toBe(
'/my-custom-canonical'
)
}) })
it('should allow manually setting amphtml rel', async () => { it('should allow manually setting amphtml rel', async () => {
...@@ -244,6 +265,89 @@ describe('AMP Usage', () => { ...@@ -244,6 +265,89 @@ describe('AMP Usage', () => {
expect(styles).not.toMatch(/\/\*# sourceMappingURL=.*\*\//) expect(styles).not.toMatch(/\/\*# sourceMappingURL=.*\*\//)
}) })
}) })
})
describe('production mode modern', () => {
let origNextConfig = ''
const nextConfigPath = join(appDir, 'next.config.js')
beforeAll(async () => {
origNextConfig = await readFile(nextConfigPath, 'utf8')
await writeFile(
nextConfigPath,
origNextConfig.replace(
'// edit here',
`
experimental: {
modern: true
}
`
)
)
await nextBuild(appDir)
app = nextServer({
dir: join(__dirname, '../'),
dev: false,
quiet: true,
})
server = await startApp(app)
context.appPort = appPort = server.address().port
})
afterAll(async () => {
await stopApp(server)
await writeFile(nextConfigPath, origNextConfig)
})
it('should not output client pages for AMP only', async () => {
const buildId = readFileSync(join(appDir, '.next/BUILD_ID'), 'utf8')
const ampOnly = ['only-amp', 'root-hmr', 'another-amp']
for (const pg of ampOnly) {
expect(() =>
accessSync(join(appDir, '.next/static', buildId, 'pages', pg + '.js'))
).toThrow()
expect(() =>
accessSync(
join(appDir, '.next/server/static', buildId, 'pages', pg + '.html')
)
).not.toThrow()
expect(() =>
accessSync(
join(appDir, '.next/server/static', buildId, 'pages', pg + '.js')
)
).toThrow()
}
})
it('should not output modern client pages for AMP only', async () => {
const buildId = readFileSync(join(appDir, '.next/BUILD_ID'), 'utf8')
const ampOnly = ['only-amp', 'root-hmr', 'another-amp']
for (const pg of ampOnly) {
expect(() =>
accessSync(
join(appDir, '.next/static', buildId, 'pages', pg + '.module.js')
)
).toThrow()
expect(() =>
accessSync(
join(appDir, '.next/server/static', buildId, 'pages', pg + '.html')
)
).not.toThrow()
expect(() =>
accessSync(
join(
appDir,
'.next/server/static',
buildId,
'pages',
pg + '.module.js'
)
)
).toThrow()
}
})
})
describe('AMP dev no-warn', () => { describe('AMP dev no-warn', () => {
let dynamicAppPort let dynamicAppPort
......
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