2024-06-08 01:42:44 +02:00
|
|
|
import { waitFor } from 'next-test-utils'
|
2023-08-04 00:10:55 +02:00
|
|
|
import { outdent } from 'outdent'
|
2024-06-08 01:42:44 +02:00
|
|
|
import { isNextDev, nextTestSetup } from 'e2e-utils'
|
2023-08-04 00:10:55 +02:00
|
|
|
|
|
|
|
describe('app-fetch-deduping', () => {
|
2024-06-08 01:42:44 +02:00
|
|
|
if (isNextDev) {
|
2023-08-04 00:10:55 +02:00
|
|
|
describe('during next dev', () => {
|
2024-03-26 15:33:09 +01:00
|
|
|
const { next } = nextTestSetup({ files: __dirname })
|
|
|
|
function invocation(cliOutput: string): number {
|
|
|
|
return cliOutput.match(/Route Handler invoked/g).length
|
|
|
|
}
|
2023-08-04 00:10:55 +02:00
|
|
|
|
2024-03-26 15:33:09 +01:00
|
|
|
it('should dedupe requests called from the same component', async () => {
|
2023-08-04 00:10:55 +02:00
|
|
|
await next.patchFile(
|
|
|
|
'app/test/page.tsx',
|
|
|
|
outdent`
|
|
|
|
async function getTime() {
|
|
|
|
const res = await fetch("http://localhost:${next.appPort}/api/time")
|
|
|
|
return res.text()
|
|
|
|
}
|
|
|
|
|
|
|
|
export default async function Home() {
|
|
|
|
await getTime()
|
|
|
|
await getTime()
|
|
|
|
const time = await getTime()
|
|
|
|
|
|
|
|
return <h1>{time}</h1>
|
2024-03-26 15:33:09 +01:00
|
|
|
}`
|
2023-08-04 00:10:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
await next.render('/test')
|
|
|
|
|
2024-03-26 15:33:09 +01:00
|
|
|
expect(invocation(next.cliOutput)).toBe(1)
|
|
|
|
await next.stop()
|
2023-08-04 00:10:55 +02:00
|
|
|
})
|
2023-11-28 15:38:59 +01:00
|
|
|
|
|
|
|
it('should dedupe pending revalidation requests', async () => {
|
2024-03-26 15:33:09 +01:00
|
|
|
await next.start()
|
|
|
|
const revalidate = 5
|
2023-11-28 15:38:59 +01:00
|
|
|
await next.patchFile(
|
|
|
|
'app/test/page.tsx',
|
|
|
|
outdent`
|
|
|
|
async function getTime() {
|
2024-03-26 15:33:09 +01:00
|
|
|
const res = await fetch("http://localhost:${next.appPort}/api/time", { next: { revalidate: ${revalidate} } })
|
2023-11-28 15:38:59 +01:00
|
|
|
return res.text()
|
|
|
|
}
|
|
|
|
|
|
|
|
export default async function Home() {
|
|
|
|
await getTime()
|
|
|
|
await getTime()
|
|
|
|
const time = await getTime()
|
|
|
|
|
|
|
|
return <h1>{time}</h1>
|
2024-03-26 15:33:09 +01:00
|
|
|
}`
|
2023-11-28 15:38:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
await next.render('/test')
|
|
|
|
|
2024-03-26 15:33:09 +01:00
|
|
|
expect(invocation(next.cliOutput)).toBe(1)
|
2023-11-28 15:38:59 +01:00
|
|
|
|
|
|
|
// wait for the revalidation to finish
|
2024-03-26 15:33:09 +01:00
|
|
|
await waitFor(revalidate * 1000 + 1000)
|
2023-11-28 15:38:59 +01:00
|
|
|
|
|
|
|
await next.render('/test')
|
|
|
|
|
2024-03-26 15:33:09 +01:00
|
|
|
expect(invocation(next.cliOutput)).toBe(2)
|
2023-11-28 15:38:59 +01:00
|
|
|
})
|
2023-08-04 00:10:55 +02:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
it('should skip other scenarios', () => {})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|