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

Added a limit for loaded pages in dev #7282

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions packages/next-server/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const defaultConfig: {[key: string]: any} = {
onDemandEntries: {
maxInactiveAge: 60 * 1000,
pagesBufferLength: 2,
pagesBufferLimit: 8,
},
experimental: {
cpus: Math.max(
Expand Down
29 changes: 29 additions & 0 deletions packages/next/server/on-demand-entry-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export default function onDemandEntryHandler (devMiddleware, multiCompiler, {
pageExtensions,
maxInactiveAge,
pagesBufferLength,
pagesBufferLimit,
publicRuntimeConfig,
serverRuntimeConfig
}) {
Expand Down Expand Up @@ -249,6 +250,7 @@ export default function onDemandEntryHandler (devMiddleware, multiCompiler, {

entries[normalizedPage] = { name, absolutePagePath, status: ADDED }
doneCallbacks.once(normalizedPage, handleCallback)
disposeLimitedEntries(entries, lastAccessPages, pagesBufferLimit)

invalidator.invalidate()

Expand Down Expand Up @@ -332,6 +334,33 @@ function disposeInactiveEntries (devMiddleware, entries, lastAccessPages, maxIna
}
}

function disposeLimitedEntries (entries, lastAccessPages, pagesBufferLimit) {
const keys = Object.keys(entries)

if (keys.length <= pagesBufferLimit) return

const disposedPages = []
const lastActiveTime = page => entries[page].lastActiveTime || Number.MAX_SAFE_INTEGER
const sortedPages = keys.sort((a, b) => lastActiveTime(a) - lastActiveTime(b))
const removeNextEntry = () => {
const page = sortedPages.shift()

if (entries[page].status === BUILT) {
delete entries[page]
disposedPages.push(page)
}
if (sortedPages.length > pagesBufferLimit) {
removeNextEntry()
}
}

removeNextEntry()

if (disposedPages.length) {
Log.event(`disposing inactive page(s): ${disposedPages.join(', ')}`)
}
}

// /index and / is the same. So, we need to identify both pages as the same.
// This also applies to sub pages as well.
export function normalizePage (page) {
Expand Down
3 changes: 2 additions & 1 deletion test/integration/ondemand/next.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
onDemandEntries: {
maxInactiveAge: 1000 * 5
maxInactiveAge: 1000 * 5,
pagesBufferLimit: 5
}
}
9 changes: 9 additions & 0 deletions test/integration/ondemand/pages/multiple/about1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default () => {
return (
<div className='hmr-about-page'>
<p>
This is the about page.
</p>
</div>
)
}
9 changes: 9 additions & 0 deletions test/integration/ondemand/pages/multiple/about10.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default () => {
return (
<div className='hmr-about-page'>
<p>
This is the about page.
</p>
</div>
)
}
9 changes: 9 additions & 0 deletions test/integration/ondemand/pages/multiple/about2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default () => {
return (
<div className='hmr-about-page'>
<p>
This is the about page.
</p>
</div>
)
}
9 changes: 9 additions & 0 deletions test/integration/ondemand/pages/multiple/about3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default () => {
return (
<div className='hmr-about-page'>
<p>
This is the about page.
</p>
</div>
)
}
9 changes: 9 additions & 0 deletions test/integration/ondemand/pages/multiple/about4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default () => {
return (
<div className='hmr-about-page'>
<p>
This is the about page.
</p>
</div>
)
}
9 changes: 9 additions & 0 deletions test/integration/ondemand/pages/multiple/about5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default () => {
return (
<div className='hmr-about-page'>
<p>
This is the about page.
</p>
</div>
)
}
9 changes: 9 additions & 0 deletions test/integration/ondemand/pages/multiple/about6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default () => {
return (
<div className='hmr-about-page'>
<p>
This is the about page.
</p>
</div>
)
}
9 changes: 9 additions & 0 deletions test/integration/ondemand/pages/multiple/about7.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default () => {
return (
<div className='hmr-about-page'>
<p>
This is the about page.
</p>
</div>
)
}
9 changes: 9 additions & 0 deletions test/integration/ondemand/pages/multiple/about8.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default () => {
return (
<div className='hmr-about-page'>
<p>
This is the about page.
</p>
</div>
)
}
9 changes: 9 additions & 0 deletions test/integration/ondemand/pages/multiple/about9.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default () => {
return (
<div className='hmr-about-page'>
<p>
This is the about page.
</p>
</div>
)
}
28 changes: 28 additions & 0 deletions test/integration/ondemand/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,34 @@ describe('On Demand Entries', () => {
}
})

it('should be able to load many pages and dispose old ones', async () => {
const pagesPath = '../.next/static/development/pages'
const times = async (n, fn) => {
let i = 0
while (i < n) await fn(++i)
}

expect.assertions(15)

// Load many pages, this should be able to scale indefinitely to any number of pages
await times(10, async i => {
const page = `/multiple/about${i}`
await renderViaHTTP(context.appPort, page)
await doPing(page)
const aboutPagePath = resolve(__dirname, join(pagesPath, `${page}.js`))

expect(existsSync(aboutPagePath)).toBeTruthy()
})

// Old Pages should have been disposed
await times(5, async i => {
const page = `/multiple/about${i}`
const aboutPagePath = resolve(__dirname, join(pagesPath, `${page}.js`))

expect(existsSync(aboutPagePath)).toBeFalsy()
})
})

it('should navigate to pages with dynamic imports', async () => {
let browser
try {
Expand Down