mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
* Keep some buffered pages, that won't be disposed. Fix #1939 * With reworked buffer
This commit is contained in:
parent
418cc210fa
commit
36436122f2
|
@ -14,7 +14,8 @@ export default function onDemandEntryHandler (devMiddleware, compiler, {
|
|||
dir,
|
||||
dev,
|
||||
reload,
|
||||
maxInactiveAge = 1000 * 25
|
||||
maxInactiveAge = 1000 * 25,
|
||||
pagesBufferLength = 2
|
||||
}) {
|
||||
let entries = {}
|
||||
let lastAccessPages = ['']
|
||||
|
@ -202,8 +203,12 @@ export default function onDemandEntryHandler (devMiddleware, compiler, {
|
|||
if (entryInfo.status !== BUILT) return
|
||||
|
||||
// If there's an entryInfo
|
||||
lastAccessPages.pop()
|
||||
lastAccessPages.unshift(page)
|
||||
if (!lastAccessPages.includes(page)) {
|
||||
lastAccessPages.unshift(page)
|
||||
|
||||
// Maintain the buffer max length
|
||||
if (lastAccessPages.length > pagesBufferLength) lastAccessPages.pop()
|
||||
}
|
||||
entryInfo.lastActiveTime = Date.now()
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
// Sometimes, it's possible our XHR ping to wait before completing other requests.
|
||||
// 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) {
|
||||
disposingPages.push(page)
|
||||
|
|
5
test/integration/ondemand/pages/third.js
Normal file
5
test/integration/ondemand/pages/third.js
Normal file
|
@ -0,0 +1,5 @@
|
|||
export default () => (
|
||||
<div>
|
||||
<p>Third Page</p>
|
||||
</div>
|
||||
)
|
|
@ -22,7 +22,10 @@ describe('On Demand Entries', () => {
|
|||
afterAll(() => killApp(context.server))
|
||||
|
||||
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, '/')
|
||||
await renderViaHTTP(context.appPort, '/_next/on-demand-entries-ping', {page: '/'})
|
||||
expect(pageContent.includes('Index Page')).toBeTruthy()
|
||||
})
|
||||
|
||||
|
@ -32,15 +35,26 @@ describe('On Demand Entries', () => {
|
|||
})
|
||||
|
||||
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/on-demand-entries-ping', {page: '/about'})
|
||||
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
|
||||
// for disposing /about
|
||||
while (true) {
|
||||
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
|
||||
}
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue