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

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

Merged
merged 2 commits into from
Sep 28, 2017
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
13 changes: 9 additions & 4 deletions server/on-demand-entry-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ['']
Expand Down Expand Up @@ -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()
}
}
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions test/integration/ondemand/pages/third.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default () => (
<div>
<p>Third Page</p>
</div>
)
18 changes: 16 additions & 2 deletions test/integration/ondemand/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

Expand All @@ -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
}
})
})