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

Make sure to 404 for AMP client bundles in dev mode (#7183)

* Make sure to 404 for AMP bundle in dev mode

* Add test for non-AMP to AMP navigating in dev mode
parent 2eee8766
......@@ -12,6 +12,11 @@ import { createPagesMapping, createEntrypoints } from '../build/entries'
import { watchCompiler } from '../build/output'
import { findPageFile } from './lib/find-page-file'
import { recursiveDelete } from '../lib/recursive-delete'
import { promisify } from 'util'
import fs from 'fs'
const access = promisify(fs.access)
const readFile = promisify(fs.readFile)
export async function renderScriptError (res, error) {
// Asks CDNs and others to not to cache the errored page
......@@ -134,6 +139,23 @@ export default class HotReloader {
return { finished: true }
}
const bundlePath = join(
this.dir,
this.config.distDir,
'static/development/pages', page + '.js'
)
// make sure to 404 for AMP bundles in case they weren't removed
try {
await access(bundlePath)
const data = await readFile(bundlePath, 'utf8')
if (data.includes('__NEXT_DROP_CLIENT_FILE__')) {
res.statusCode = 404
res.end()
return { finished: true }
}
} catch (_) {}
const errors = await this.getCompilationErrors(page)
if (errors.length > 0) {
await renderScriptError(res, errors[0])
......
export default () => <div>No AMP for me...</div>
import Link from 'next/link'
export default () => (
<div>
<p>No AMP for me...</p>
<Link href='/only-amp'>
<a id='to-amp'>To AMP page!</a>
</Link>
</div>
)
import { withAmp } from 'next/amp'
export default withAmp(() => <div>Only AMP for me...</div>)
export default withAmp(() => (
<div>
<p id='only-amp'>Only AMP for me...</p>
</div>
))
......@@ -256,7 +256,7 @@ describe('AMP Usage', () => {
})
})
describe('editing a page', () => {
describe('AMP dev mode', () => {
let dynamicAppPort
let ampDynamic
......@@ -267,6 +267,13 @@ describe('AMP Usage', () => {
afterAll(() => killApp(ampDynamic))
it('should navigate from non-AMP to AMP without error', async () => {
const browser = await webdriver(dynamicAppPort, '/normal')
await browser.elementByCss('#to-amp').click()
await browser.waitForElementByCss('#only-amp')
expect(await browser.elementByCss('#only-amp').text()).toMatch(/Only AMP/)
})
it('should detect the changes and display it', async () => {
let browser
try {
......
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