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

Ensure all entries are cleared from cache on runtime change #20652

Merged
merged 4 commits into from
Jan 1, 2021
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
2 changes: 1 addition & 1 deletion .github/workflows/build_test_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ jobs:
- run: cat package.json | jq '.resolutions."react-dom" = "^17.0.1"' > package.json.tmp && mv package.json.tmp package.json
- run: yarn install --check-files
- run: yarn list webpack react react-dom
- run: xvfb-run node run-tests.js test/integration/link-ref/test/index.test.js test/integration/production/test/index.test.js test/integration/basic/test/index.test.js test/integration/async-modules/test/index.test.js test/integration/font-optimization/test/index.test.js test/acceptance/*.test.js
- run: xvfb-run node run-tests.js test/integration/{link-ref,production,basic,async-modules,font-optimization,ssr-ctx}/test/index.test.js test/acceptance/*.test.js

testFirefox:
name: Test Firefox (production)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,28 @@ export class NextJsRequireCacheHotReloader implements Plugin {
compilation.outputOptions.path,
'webpack-runtime.js'
)

deleteCache(runtimeChunkPath)

for (const outputPath of this.previousOutputPathsWebpack5) {
if (!this.currentOutputPathsWebpack5.has(outputPath)) {
deleteCache(outputPath)
}
}

this.previousOutputPathsWebpack5 = new Set(
this.currentOutputPathsWebpack5
// we need to make sure to clear all server entries from cache
// since they can have a stale webpack-runtime cache
// which needs to always be in-sync
const entries = [...compilation.entries.keys()].filter((entry) =>
entry.toString().startsWith('pages/')
)
this.currentOutputPathsWebpack5.clear()

entries.forEach((page) => {
const outputPath = path.join(
compilation.outputOptions.path,
page + '.js'
)
deleteCache(outputPath)
})
})

this.previousOutputPathsWebpack5 = new Set(
this.currentOutputPathsWebpack5
)
this.currentOutputPathsWebpack5.clear()
return
}

Expand Down
5 changes: 5 additions & 0 deletions test/integration/ssr-ctx/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react'

export const Idk = React.createContext(null)

export const useIdk = () => React.useContext(Idk)
9 changes: 9 additions & 0 deletions test/integration/ssr-ctx/pages/_app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Idk } from '../context'

export default function MyApp({ Component, pageProps }) {
return (
<Idk.Provider value="hello world">
<Component {...pageProps} />
</Idk.Provider>
)
}
15 changes: 8 additions & 7 deletions test/integration/ssr-ctx/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import React from 'react'

const Idk = React.createContext(null)
import { useIdk } from '../context'

const Page = () => {
const idk = useIdk()

console.log(idk)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You left a console.log here 😉

Copy link
Member Author

@ijjk ijjk Jan 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was on purpose, it's to see when the test is failing while running since we can quickly see the null default value in the logs instead of waiting for the test to fail from checking the content


return (
<div>
<Idk.Provider value="hello world">
<Idk.Consumer>{(idk) => <p>Value: {idk}</p>}</Idk.Consumer>
</Idk.Provider>
</div>
<>
<p>Value: {idk}</p>
</>
)
}

Expand Down
53 changes: 45 additions & 8 deletions test/integration/ssr-ctx/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,66 @@

import { join } from 'path'
import {
File,
killApp,
findPort,
nextStart,
nextBuild,
renderViaHTTP,
check,
launchApp,
} from 'next-test-utils'

jest.setTimeout(1000 * 30)

const appDir = join(__dirname, '../')
const appPg = new File(join(appDir, 'pages/_app.js'))

let appPort
let app

describe('Production Usage', () => {
beforeAll(async () => {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))

const runTests = (isDev) => {
it('should render a page with context', async () => {
const html = await renderViaHTTP(appPort, '/')
expect(html).toMatch(/Value: .*?hello world/)
})

if (isDev) {
it('should render with context after change', async () => {
appPg.replace('hello world', 'new value')

try {
await check(() => renderViaHTTP(appPort, '/'), /Value: .*?new value/)
} finally {
appPg.restore()
}
await check(() => renderViaHTTP(appPort, '/'), /Value: .*?hello world/)
})
}
}

describe('React Context', () => {
describe('dev mode', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
appPg.restore()
})

runTests(true)
})

describe('production mode', () => {
beforeAll(async () => {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))

runTests()
})
})