Commit 36436122 authored by AugustinLF's avatar AugustinLF Committed by Tim Neutkens

Keep some buffered pages, that won't be disposed. Fix #1939 (#2592)

* Keep some buffered pages, that won't be disposed. Fix #1939

* With reworked buffer
parent 418cc210
...@@ -14,7 +14,8 @@ export default function onDemandEntryHandler (devMiddleware, compiler, { ...@@ -14,7 +14,8 @@ export default function onDemandEntryHandler (devMiddleware, compiler, {
dir, dir,
dev, dev,
reload, reload,
maxInactiveAge = 1000 * 25 maxInactiveAge = 1000 * 25,
pagesBufferLength = 2
}) { }) {
let entries = {} let entries = {}
let lastAccessPages = [''] let lastAccessPages = ['']
...@@ -202,8 +203,12 @@ export default function onDemandEntryHandler (devMiddleware, compiler, { ...@@ -202,8 +203,12 @@ export default function onDemandEntryHandler (devMiddleware, compiler, {
if (entryInfo.status !== BUILT) return if (entryInfo.status !== BUILT) return
// If there's an entryInfo // If there's an entryInfo
lastAccessPages.pop() if (!lastAccessPages.includes(page)) {
lastAccessPages.unshift(page) lastAccessPages.unshift(page)
// Maintain the buffer max length
if (lastAccessPages.length > pagesBufferLength) lastAccessPages.pop()
}
entryInfo.lastActiveTime = Date.now() entryInfo.lastActiveTime = Date.now()
} }
} }
...@@ -234,7 +239,7 @@ function disposeInactiveEntries (devMiddleware, entries, lastAccessPages, maxIna ...@@ -234,7 +239,7 @@ function disposeInactiveEntries (devMiddleware, entries, lastAccessPages, maxIna
// We should not build the last accessed page even we didn't get any pings // We should not build the last accessed page even we didn't get any pings
// Sometimes, it's possible our XHR ping to wait before completing other requests. // Sometimes, it's possible our XHR ping to wait before completing other requests.
// In that case, we should not dispose the current viewing page // In that case, we should not dispose the current viewing page
if (lastAccessPages[0] === page) return if (lastAccessPages.includes(page)) return
if (Date.now() - lastActiveTime > maxInactiveAge) { if (Date.now() - lastActiveTime > maxInactiveAge) {
disposingPages.push(page) disposingPages.push(page)
......
export default () => (
<div>
<p>Third Page</p>
</div>
)
...@@ -22,7 +22,10 @@ describe('On Demand Entries', () => { ...@@ -22,7 +22,10 @@ describe('On Demand Entries', () => {
afterAll(() => killApp(context.server)) afterAll(() => killApp(context.server))
it('should compile pages for SSR', async () => { it('should compile pages for SSR', async () => {
// The buffer of built page uses the on-demand-entries-ping to know which pages should be
// buffered. Therefore, we need to double each render call with a ping.
const pageContent = await renderViaHTTP(context.appPort, '/') const pageContent = await renderViaHTTP(context.appPort, '/')
await renderViaHTTP(context.appPort, '/_next/on-demand-entries-ping', {page: '/'})
expect(pageContent.includes('Index Page')).toBeTruthy() expect(pageContent.includes('Index Page')).toBeTruthy()
}) })
...@@ -32,15 +35,26 @@ describe('On Demand Entries', () => { ...@@ -32,15 +35,26 @@ describe('On Demand Entries', () => {
}) })
it('should dispose inactive pages', async () => { it('should dispose inactive pages', async () => {
const indexPagePath = resolve(__dirname, '../.next/bundles/pages/index.js')
expect(existsSync(indexPagePath)).toBeTruthy()
// Render two pages after the index, since the server keeps at least two pages
await renderViaHTTP(context.appPort, '/_next/-/page/about') await renderViaHTTP(context.appPort, '/_next/-/page/about')
await renderViaHTTP(context.appPort, '/_next/on-demand-entries-ping', {page: '/about'})
const aboutPagePath = resolve(__dirname, '../.next/bundles/pages/about.js') const aboutPagePath = resolve(__dirname, '../.next/bundles/pages/about.js')
expect(existsSync(aboutPagePath)).toBeTruthy()
await renderViaHTTP(context.appPort, '/_next/-/page/third')
await renderViaHTTP(context.appPort, '/_next/on-demand-entries-ping', {page: '/third'})
const thirdPagePath = resolve(__dirname, '../.next/bundles/pages/third.js')
// Wait maximum of jasmine.DEFAULT_TIMEOUT_INTERVAL checking // Wait maximum of jasmine.DEFAULT_TIMEOUT_INTERVAL checking
// for disposing /about // for disposing /about
while (true) { while (true) {
await waitFor(1000 * 1) await waitFor(1000 * 1)
if (!existsSync(aboutPagePath)) return // Assert that the two lastly demanded page are not disposed
expect(existsSync(aboutPagePath)).toBeTruthy()
expect(existsSync(thirdPagePath)).toBeTruthy()
if (!existsSync(indexPagePath)) return
} }
}) })
}) })
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