Unverified Commit 611e13f5 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix disposing active entries in dev compilers (#39845)

As noticed in https://github.com/markdoc/markdoc/issues/131 it seems we are incorrectly disposing active entries causing HMR to fail after the configured `maxInactiveAge`. To fix this instead of only updating lastActiveTime for one compiler type's entry we update all active compiler types for the active entry. 

This also updates an existing test to catch this by waiting the `maxInactiveAge` before attempting a change that should trigger HMR. 

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [x] Errors have helpful link attached, see `contributing.md`

Fixes: https://github.com/markdoc/markdoc/issues/131
parent 31bcd044
...@@ -453,15 +453,21 @@ export function onDemandEntryHandler({ ...@@ -453,15 +453,21 @@ export function onDemandEntryHandler({
tree: FlightRouterState tree: FlightRouterState
): { success: true } | { invalid: true } { ): { success: true } | { invalid: true } {
const pages = getEntrypointsFromTree(tree, true) const pages = getEntrypointsFromTree(tree, true)
let toSend: { invalid: true } | { success: true } = { invalid: true }
for (const page of pages) { for (const page of pages) {
const pageKey = `server/${page}` for (const compilerType of [
COMPILER_NAMES.client,
COMPILER_NAMES.server,
COMPILER_NAMES.edgeServer,
]) {
const pageKey = `${compilerType}/${page}`
const entryInfo = entries[pageKey] const entryInfo = entries[pageKey]
// If there's no entry, it may have been invalidated and needs to be re-built. // If there's no entry, it may have been invalidated and needs to be re-built.
if (!entryInfo) { if (!entryInfo) {
// if (page !== lastEntry) client pings, but there's no entry for page // if (page !== lastEntry) client pings, but there's no entry for page
return { invalid: true } continue
} }
// We don't need to maintain active state of anything other than BUILT entries // We don't need to maintain active state of anything other than BUILT entries
...@@ -479,27 +485,38 @@ export function onDemandEntryHandler({ ...@@ -479,27 +485,38 @@ export function onDemandEntryHandler({
} }
entryInfo.lastActiveTime = Date.now() entryInfo.lastActiveTime = Date.now()
entryInfo.dispose = false entryInfo.dispose = false
toSend = { success: true }
} }
}
return { success: true } return toSend
} }
function handlePing(pg: string) { function handlePing(pg: string) {
const page = normalizePathSep(pg) const page = normalizePathSep(pg)
const pageKey = `client${page}` let toSend: { invalid: true } | { success: true } = { invalid: true }
for (const compilerType of [
COMPILER_NAMES.client,
COMPILER_NAMES.server,
COMPILER_NAMES.edgeServer,
]) {
const pageKey = `${compilerType}${page}`
const entryInfo = entries[pageKey] const entryInfo = entries[pageKey]
// If there's no entry, it may have been invalidated and needs to be re-built. // If there's no entry, it may have been invalidated and needs to be re-built.
if (!entryInfo) { if (!entryInfo) {
// if (page !== lastEntry) client pings, but there's no entry for page // if (page !== lastEntry) client pings, but there's no entry for page
if (compilerType === COMPILER_NAMES.client) {
return { invalid: true } return { invalid: true }
} }
continue
}
// 404 is an on demand entry but when a new page is added we have to refresh the page // 404 is an on demand entry but when a new page is added we have to refresh the page
const toSend = page === '/_error' ? { invalid: true } : { success: true } toSend = page === '/_error' ? { invalid: true } : { success: true }
// We don't need to maintain active state of anything other than BUILT entries // We don't need to maintain active state of anything other than BUILT entries
if (entryInfo.status !== BUILT) return if (entryInfo.status !== BUILT) continue
// If there's an entryInfo // If there's an entryInfo
if (!lastClientAccessPages.includes(pageKey)) { if (!lastClientAccessPages.includes(pageKey)) {
...@@ -512,6 +529,7 @@ export function onDemandEntryHandler({ ...@@ -512,6 +529,7 @@ export function onDemandEntryHandler({
} }
entryInfo.lastActiveTime = Date.now() entryInfo.lastActiveTime = Date.now()
entryInfo.dispose = false entryInfo.dispose = false
}
return toSend return toSend
} }
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
import { join } from 'path' import { join } from 'path'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import { createNext, FileRef } from 'e2e-utils' import { createNext, FileRef } from 'e2e-utils'
import { check, getRedboxHeader, hasRedbox } from 'next-test-utils' import { check, getRedboxHeader, hasRedbox, waitFor } from 'next-test-utils'
import { NextInstance } from 'test/lib/next-modes/base' import { NextInstance } from 'test/lib/next-modes/base'
const installCheckVisible = (browser) => { const installCheckVisible = (browser) => {
...@@ -319,6 +319,10 @@ describe('GS(S)P Server-Side Change Reloading', () => { ...@@ -319,6 +319,10 @@ describe('GS(S)P Server-Side Change Reloading', () => {
expect(props.count).toBe(1) expect(props.count).toBe(1)
expect(props.data).toEqual({ hello: 'world' }) expect(props.data).toEqual({ hello: 'world' })
// wait longer than the max inactive age for on-demand entries
// to ensure we aren't incorrectly disposing the active entry
await waitFor(20 * 1000)
const page = 'lib/data.json' const page = 'lib/data.json'
const originalContent = await next.readFile(page) const originalContent = await next.readFile(page)
......
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