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

Fix AMP HMR for child components #9736

Merged
merged 2 commits into from
Dec 16, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/next/build/babel/plugins/next-page-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,17 @@ export default function nextPageConfig({
}

if (config.amp === true) {
replaceBundle(path, t)
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)
}
state.bundleDropped = true
return
}
Expand Down
1 change: 1 addition & 0 deletions packages/next/build/webpack/loaders/next-babel-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ module.exports = babelLoader.custom(babel => {

options.caller.isServer = isServer
options.caller.isModern = isModern
options.caller.isDev = development

options.plugins = options.plugins || []

Expand Down
14 changes: 4 additions & 10 deletions packages/next/server/hot-reloader.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import fs from 'fs'
import { IncomingMessage, ServerResponse } from 'http'
import { join, normalize, relative as relativePath, sep } from 'path'
import { promisify } from 'util'
import webpack from 'webpack'
import WebpackDevMiddleware from 'webpack-dev-middleware'
import WebpackHotMiddleware from 'webpack-hot-middleware'
Expand All @@ -25,9 +23,6 @@ import onDemandEntryHandler, { normalizePage } from './on-demand-entry-handler'
import { NextHandleFunction } from 'connect'
import { UrlObject } from 'url'

const access = promisify(fs.access)
const readFile = promisify(fs.readFile)

export async function renderScriptError(res: ServerResponse, error: Error) {
// Asks CDNs and others to not to cache the errored page
res.setHeader(
Expand Down Expand Up @@ -196,15 +191,14 @@ export default class HotReloader {
const bundlePath = join(
this.dir,
this.config.distDir,
'static/development/pages',
'server/static/development/pages',
page + '.js'
)

// make sure to 404 for AMP bundles in case they weren't removed
// Make sure to 404 for AMP first pages
try {
await access(bundlePath)
const data = await readFile(bundlePath, 'utf8')
if (data.includes('__NEXT_DROP_CLIENT_FILE__')) {
const mod = require(bundlePath)
if (mod && mod.config && mod.config.amp === true) {
res.statusCode = 404
res.end()
return { finished: true }
Expand Down
1 change: 1 addition & 0 deletions test/integration/amphtml/components/hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => <p id="hello-comp">hello</p>
10 changes: 10 additions & 0 deletions test/integration/amphtml/pages/hmr/comp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Hello from '../../components/hello'

export const config = { amp: true }

export default () => (
<>
<Hello />
<span>{new Date().getTime()}</span>
</>
)
17 changes: 17 additions & 0 deletions test/integration/amphtml/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
let browser
try {
Expand Down