-
Notifications
You must be signed in to change notification settings - Fork 27.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test case for persistent caching
- Loading branch information
Showing
6 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export default function Page() { | ||
return ( | ||
<> | ||
<p>hello world</p> | ||
<main>Timestamp</main> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()}`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export default function Page() { | ||
return ( | ||
<> | ||
<p>hello world</p> | ||
<main>Timestamp</main> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { nextTestSetup } from 'e2e-utils' | ||
import { waitFor } from 'next-test-utils' | ||
|
||
describe('persistent-caching', () => { | ||
const { skipped, next, isNextDev, isTurbopack } = 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() | ||
browser.close() | ||
} | ||
{ | ||
const browser = await next.browser('/pages') | ||
pagesTimestamp = await browser.elementByCss('main').text() | ||
browser.close() | ||
} | ||
await restartCycle() | ||
|
||
{ | ||
const browser = await next.browser('/') | ||
// TODO Persistent Caching for webpack dev server is broken | ||
if (!isNextDev || isTurbopack) { | ||
expect(await browser.elementByCss('main').text()).toBe(appTimestamp) | ||
} | ||
browser.close() | ||
} | ||
{ | ||
const browser = await next.browser('/pages') | ||
// TODO Persistent Caching for webpack dev server is broken | ||
if (!isNextDev || isTurbopack) { | ||
expect(await browser.elementByCss('main').text()).toBe(pagesTimestamp) | ||
} | ||
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') | ||
browser.close() | ||
} | ||
{ | ||
const browser = await next.browser('/pages') | ||
expect(await browser.elementByCss('p').text()).toBe('hello world') | ||
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' | ||
) | ||
browser.close() | ||
} | ||
{ | ||
const browser = await next.browser('/pages') | ||
expect(await browser.elementByCss('p').text()).toBe( | ||
'hello persistent caching' | ||
) | ||
browser.close() | ||
} | ||
await stop() | ||
} | ||
) | ||
} | ||
) | ||
await start() | ||
}) | ||
}) |