Skip to content

Commit

Permalink
rsc: clean client buffer cache after flushed (#34475)
Browse files Browse the repository at this point in the history
## Bug

Fixes #34464

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [x] Errors have helpful link attached, see `contributing.md`
  • Loading branch information
huozhi committed Feb 17, 2022
1 parent eddabd9 commit 1c167af
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/next/client/index.tsx
Expand Up @@ -702,6 +702,7 @@ if (process.env.__NEXT_RSC) {
writer.write(encoder.encode(val))
})
buffer.length = 0
serverDataBuffer.delete(key)
}
serverDataWriter.set(key, writer)
}
Expand Down
@@ -1,4 +1,5 @@
import Foo from '../components/foo.client'
import Link from 'next/link'

const envVar = process.env.ENV_VAR_TEST
const headerKey = 'x-next-test-client'
Expand All @@ -13,6 +14,9 @@ export default function Index({ header, router }) {
<div>
<Foo />
</div>
<Link href={'/'}>
<a id="refresh">refresh</a>
</Link>
</div>
)
}
Expand Down
36 changes: 33 additions & 3 deletions test/integration/react-streaming-and-server-components/test/rsc.js
Expand Up @@ -38,7 +38,7 @@ export default function (context, { runtime, env }) {
page.on('request', (request) => {
requestsCount++
const url = request.url()
if (/__flight__=1/.test(url)) {
if (/\?__flight__=1/.test(url)) {
hasFlightRequest = true
}
})
Expand Down Expand Up @@ -95,9 +95,39 @@ export default function (context, { runtime, env }) {
})
}

// For prod build, the directory contains the build ID so it's not deterministic.
// Only enable it for dev for now.
it('should refresh correctly with next/link', async () => {
// Select the button which is not hidden but rendered
const selector = '#__next #refresh'
let hasFlightRequest = false
const browser = await webdriver(context.appPort, '/', {
beforePageLoad(page) {
page.on('request', (request) => {
const url = request.url()
if (/\?__flight__=1/.test(url)) {
hasFlightRequest = true
}
})
},
})

// wait for hydration
await new Promise((res) => setTimeout(res, 1000))
if (env === 'dev') {
expect(hasFlightRequest).toBe(false)
}
await browser.elementByCss(selector).click()
// wait for re-hydration
await new Promise((res) => setTimeout(res, 1000))
if (env === 'dev') {
expect(hasFlightRequest).toBe(true)
}
const refreshText = await browser.elementByCss(selector).text()
expect(refreshText).toBe('refresh')
})

if (env === 'dev') {
// For prod build, the directory contains the build ID so it's not deterministic.
// Only enable it for dev for now.
it('should not bundle external imports into client builds for RSC', async () => {
const html = await renderViaHTTP(context.appPort, '/external-imports')
expect(html).toContain('date:')
Expand Down

0 comments on commit 1c167af

Please sign in to comment.