Fixes for Turbopack HMR (#54790)

### What?

There were a few issues with the initial implementation of next-api HMR:
1. We incorrectly errored out when we received a Next.js WebSocket message
2. We didn't handle Next's `span-end` message, leading to another error
3. We listened to the `htmlEndpoint` change events instead of the `dataEndpoint`/`rscEndpoint`, leading to us detecting client-side changes and causing full page reloads

### Why?

HMR is the life-blood of development

### How?

Small fixes to our Turbopack reimplementation of the server-side HRM handlers

Closes WEB-1475
This commit is contained in:
Justin Ridgewell 2023-08-30 17:14:46 -04:00 committed by GitHub
parent 7744aa2640
commit 8b4bb031ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -754,6 +754,7 @@ async function startWatcher(opts: SetupOpts) {
case 'server-component-reload-page': // { clientId }
case 'client-reload-page': // { clientId }
case 'client-full-reload': // { stackTrace, hadRuntimeError }
case 'span-end': // { startTime, endTime, spanName, attributes }
// TODO
break
@ -775,7 +776,10 @@ async function startWatcher(opts: SetupOpts) {
break
default:
throw new Error(`unrecognized Turbopack HMR message "${data}"`)
// Might have been a Next.js message...
if (!parsedData.event) {
throw new Error(`unrecognized Turbopack message "${data}"`)
}
}
})
@ -886,7 +890,7 @@ async function startWatcher(opts: SetupOpts) {
page,
await route.htmlEndpoint.writeToDisk()
)
changeSubscription(page, route.htmlEndpoint, (pageName, change) => {
changeSubscription(page, route.dataEndpoint, (pageName, change) => {
switch (change) {
case ServerClientChangeType.Server:
case ServerClientChangeType.Both:
@ -939,7 +943,7 @@ async function startWatcher(opts: SetupOpts) {
}
case 'app-page': {
await processResult(page, await route.htmlEndpoint.writeToDisk())
changeSubscription(page, route.htmlEndpoint, (_page, change) => {
changeSubscription(page, route.rscEndpoint, (_page, change) => {
switch (change) {
case ServerClientChangeType.Server:
case ServerClientChangeType.Both: