File size: 3,939 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
import { findPort, retry } from 'next-test-utils'
import http from 'http'
import url from 'url'
import { outdent } from 'outdent'
import { isNextDev, isNextStart, nextTestSetup } from 'e2e-utils'
describe('app-fetch-deduping', () => {
if (isNextStart) {
describe('during static generation', () => {
const { next } = nextTestSetup({ files: __dirname, skipStart: true })
let externalServerPort: number
let externalServer: http.Server
let successfulRequests = []
beforeAll(async () => {
externalServerPort = await findPort()
externalServer = http.createServer((req, res) => {
const parsedUrl = url.parse(req.url, true)
const overrideStatus = parsedUrl.query.status
// if the requested url has a "status" search param, override the response status
if (overrideStatus) {
res.statusCode = Number(overrideStatus)
} else {
successfulRequests.push(req.url)
}
// Generate a response with more than two MB of data.
res.end(
`Request ${req.url} received at ${Date.now()}\n\n${'a'.repeat(2_000_000)}`
)
})
await new Promise<void>((resolve, reject) => {
externalServer.listen(externalServerPort, () => {
resolve()
})
externalServer.once('error', (err) => {
reject(err)
})
})
await next.patchFile(
'next.config.js',
`module.exports = {
env: { TEST_SERVER_PORT: "${externalServerPort}" },
}`
)
await next.build()
})
afterAll(() => externalServer.close())
it('dedupes requests amongst static workers', async () => {
expect(successfulRequests.length).toBe(1)
})
it('does not print a fetch cache size limit warning', async () => {
expect(next.cliOutput).not.toInclude('Failed to set Next.js data cache')
})
})
} else if (isNextDev) {
describe('during next dev', () => {
const { next } = nextTestSetup({ files: __dirname, patchFileDelay: 500 })
function invocation(cliOutput: string): number {
return cliOutput.match(/Route Handler invoked/g).length
}
it('should dedupe requests called from the same component', async () => {
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>
}`
)
await next.render('/test')
expect(invocation(next.cliOutput)).toBe(1)
await next.stop()
})
it('should dedupe pending revalidation requests', async () => {
await next.start()
const revalidate = 5
await next.patchFile(
'app/test/page.tsx',
outdent`
async function getTime() {
const res = await fetch("http://localhost:${next.appPort}/api/time", { next: { revalidate: ${revalidate} } })
return res.text()
}
export default async function Home() {
await getTime()
await getTime()
const time = await getTime()
return <h1>{time}</h1>
}`
)
await next.render('/test')
expect(invocation(next.cliOutput)).toBe(1)
// wait for the revalidation to finish
await retry(async () => {
await next.render('/test')
expect(invocation(next.cliOutput)).toBe(2)
}, 10_000)
})
})
} else {
it('should skip other scenarios', () => {})
return
}
})
|