Unverified Commit 21eb54a8 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

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

### What?

Fixes persistent caching for webpack in development

Add a test case for persistent caching on Turbopack and webpack.
parent b097d105
......@@ -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
......
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return (
<>
<p>hello world</p>
<main suppressHydrationWarning>Timestamp</main>
</>
)
}
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()}`)
}
/**
* @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
export default function Page() {
return (
<>
<p>hello world</p>
<main suppressHydrationWarning>Timestamp</main>
</>
)
}
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()
})
})
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