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

fixes webpack persistent caching in development and add test case for persistent caching #73697

Merged
merged 3 commits into from
Dec 10, 2024
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
20 changes: 20 additions & 0 deletions packages/next/src/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2091,8 +2091,28 @@ export default async function getBaseWebpackConfig(
if (config.webpack && config.configFile) {
cache.buildDependencies = {
config: [config.configFile],
// We don't want to use the webpack default buildDependencies as we already include the next.js version
defaultWebpack: [],
}
} else {
cache.buildDependencies = {
// We don't want to use the webpack default buildDependencies as we already include the next.js version
defaultWebpack: [],
}
}
webpack5Config.plugins?.push((compiler) => {
compiler.hooks.done.tap('next-build-dependencies', (stats) => {
const buildDependencies = stats.compilation.buildDependencies
const nextPackage = path.dirname(require.resolve('next/package.json'))
// Remove all next.js build dependencies, they are already covered by the cacheVersion
// and next.js also imports the output files which leads to broken caching.
for (const dep of buildDependencies) {
if (dep.startsWith(nextPackage)) {
buildDependencies.delete(dep)
}
}
})
})

webpack5Config.cache = cache

Expand Down
8 changes: 8 additions & 0 deletions test/e2e/persistent-caching/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
8 changes: 8 additions & 0 deletions test/e2e/persistent-caching/app/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default function Page() {
return (
<>
<p>hello world</p>
<main suppressHydrationWarning>Timestamp</main>
</>
)
}
5 changes: 5 additions & 0 deletions test/e2e/persistent-caching/my-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = async function myLoader(source) {
// Make webpack consider the build as large change which makes it persistent cache it sooner
await new Promise((resolve) => setTimeout(resolve, 2000))
return source.replace(/Timestamp/g, `Timestamp = ${Date.now()}`)
}
28 changes: 28 additions & 0 deletions test/e2e/persistent-caching/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
turbo: {
unstablePersistentCaching: true,
rules: {
'app/page.tsx': {
loaders: ['./my-loader.js'],
},
'pages/pages.tsx': {
loaders: ['./my-loader.js'],
},
},
},
},
webpack(config) {
config.module.rules.push({
test: /app\/page\.tsx|pages\/pages.tsx/,
use: ['./my-loader.js'],
})

return config
},
}

module.exports = nextConfig
8 changes: 8 additions & 0 deletions test/e2e/persistent-caching/pages/pages.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default function Page() {
return (
<>
<p>hello world</p>
<main suppressHydrationWarning>Timestamp</main>
</>
)
}
110 changes: 110 additions & 0 deletions test/e2e/persistent-caching/persistent-caching.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { nextTestSetup } from 'e2e-utils'
import { waitFor } from 'next-test-utils'

describe('persistent-caching', () => {
const { skipped, next, isNextDev } = nextTestSetup({
files: __dirname,
skipDeployment: true,
})

if (skipped) {
return
}

async function restartCycle() {
await stop()
await start()
}

async function stop() {
if (isNextDev) {
// Give Persistent Cache time to write to disk
await waitFor(10000)
}
await next.stop()
}

async function start() {
if (!isNextDev) {
await next.build()
}
await next.start()
}

it('should persistent cache loaders', async () => {
let appTimestamp, pagesTimestamp
{
const browser = await next.browser('/')
appTimestamp = await browser.elementByCss('main').text()
await browser.close()
}
{
const browser = await next.browser('/pages')
pagesTimestamp = await browser.elementByCss('main').text()
await browser.close()
}
await restartCycle()

{
const browser = await next.browser('/')
// TODO Persistent Caching for webpack dev server is broken
expect(await browser.elementByCss('main').text()).toBe(appTimestamp)
await browser.close()
}
{
const browser = await next.browser('/pages')
// TODO Persistent Caching for webpack dev server is broken
expect(await browser.elementByCss('main').text()).toBe(pagesTimestamp)
await browser.close()
}
})

it('should allow to change files while stopped', async () => {
{
const browser = await next.browser('/')
expect(await browser.elementByCss('p').text()).toBe('hello world')
await browser.close()
}
{
const browser = await next.browser('/pages')
expect(await browser.elementByCss('p').text()).toBe('hello world')
await browser.close()
}

await stop()

await next.patchFile(
'pages/pages.tsx',
(content) => {
return content.replace('hello world', 'hello persistent caching')
},
async () => {
await next.patchFile(
'app/page.tsx',
(content) => {
return content.replace('hello world', 'hello persistent caching')
},
async () => {
await start()
{
const browser = await next.browser('/')
expect(await browser.elementByCss('p').text()).toBe(
'hello persistent caching'
)
await browser.close()
}
{
const browser = await next.browser('/pages')
expect(await browser.elementByCss('p').text()).toBe(
'hello persistent caching'
)
await browser.close()
}
await stop()
}
)
}
)
await start()
})
})
Loading