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

Fix AMP HMR for child components (#9736)

parent 87d9b2e0
...@@ -81,7 +81,17 @@ export default function nextPageConfig({ ...@@ -81,7 +81,17 @@ export default function nextPageConfig({
} }
if (config.amp === true) { if (config.amp === true) {
if (
!(
state.file &&
state.file.opts &&
state.file.opts.caller.isDev
)
) {
// don't replace bundle in development so HMR can track
// dependencies and trigger reload when they are changed
replaceBundle(path, t) replaceBundle(path, t)
}
state.bundleDropped = true state.bundleDropped = true
return return
} }
......
...@@ -130,6 +130,7 @@ module.exports = babelLoader.custom(babel => { ...@@ -130,6 +130,7 @@ module.exports = babelLoader.custom(babel => {
options.caller.isServer = isServer options.caller.isServer = isServer
options.caller.isModern = isModern options.caller.isModern = isModern
options.caller.isDev = development
options.plugins = options.plugins || [] options.plugins = options.plugins || []
......
import fs from 'fs'
import { IncomingMessage, ServerResponse } from 'http' import { IncomingMessage, ServerResponse } from 'http'
import { join, normalize, relative as relativePath, sep } from 'path' import { join, normalize, relative as relativePath, sep } from 'path'
import { promisify } from 'util'
import webpack from 'webpack' import webpack from 'webpack'
import WebpackDevMiddleware from 'webpack-dev-middleware' import WebpackDevMiddleware from 'webpack-dev-middleware'
import WebpackHotMiddleware from 'webpack-hot-middleware' import WebpackHotMiddleware from 'webpack-hot-middleware'
...@@ -25,9 +23,6 @@ import onDemandEntryHandler, { normalizePage } from './on-demand-entry-handler' ...@@ -25,9 +23,6 @@ import onDemandEntryHandler, { normalizePage } from './on-demand-entry-handler'
import { NextHandleFunction } from 'connect' import { NextHandleFunction } from 'connect'
import { UrlObject } from 'url' import { UrlObject } from 'url'
const access = promisify(fs.access)
const readFile = promisify(fs.readFile)
export async function renderScriptError(res: ServerResponse, error: Error) { export async function renderScriptError(res: ServerResponse, error: Error) {
// Asks CDNs and others to not to cache the errored page // Asks CDNs and others to not to cache the errored page
res.setHeader( res.setHeader(
...@@ -196,15 +191,14 @@ export default class HotReloader { ...@@ -196,15 +191,14 @@ export default class HotReloader {
const bundlePath = join( const bundlePath = join(
this.dir, this.dir,
this.config.distDir, this.config.distDir,
'static/development/pages', 'server/static/development/pages',
page + '.js' page + '.js'
) )
// make sure to 404 for AMP bundles in case they weren't removed // Make sure to 404 for AMP first pages
try { try {
await access(bundlePath) const mod = require(bundlePath)
const data = await readFile(bundlePath, 'utf8') if (mod && mod.config && mod.config.amp === true) {
if (data.includes('__NEXT_DROP_CLIENT_FILE__')) {
res.statusCode = 404 res.statusCode = 404
res.end() res.end()
return { finished: true } return { finished: true }
......
export default () => <p id="hello-comp">hello</p>
import Hello from '../../components/hello'
export const config = { amp: true }
export default () => (
<>
<Hello />
<span>{new Date().getTime()}</span>
</>
)
...@@ -356,6 +356,23 @@ describe('AMP Usage', () => { ...@@ -356,6 +356,23 @@ describe('AMP Usage', () => {
} }
}) })
it('should detect changes to component and refresh an AMP page', async () => {
const browser = await webdriver(dynamicAppPort, '/hmr/comp')
const text = await browser.elementByCss('#hello-comp').text()
expect(text).toBe('hello')
const testComp = join(__dirname, '../components/hello.js')
const origContent = readFileSync(testComp, 'utf8')
const newContent = origContent.replace('>hello<', '>hi<')
writeFileSync(testComp, newContent, 'utf8')
await check(() => browser.elementByCss('#hello-comp').text(), /hi/)
writeFileSync(testComp, origContent, 'utf8')
await check(() => browser.elementByCss('#hello-comp').text(), /hello/)
})
it('should not reload unless the page is edited for an AMP page', async () => { it('should not reload unless the page is edited for an AMP page', async () => {
let browser let browser
try { 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