未验证 提交 d68d21c3 编写于 作者: D Darsh Patel 提交者: GitHub

Test: serverless target should set correct revalidation (`cache-control`) header (#15512)

上级 450e7130
module.exports = {
target: 'experimental-serverless-trace',
// make sure error isn't thrown from empty publicRuntimeConfig
publicRuntimeConfig: {},
}
export const getStaticProps = () => {
return {
props: {
hello: 'hello world',
random: Math.random(),
},
revalidate: 10,
}
}
export default ({ hello, random }) => (
<>
<p id="hello">{hello}</p>
<p id="random">{random}</p>
</>
)
const path = require('path')
const http = require('http')
const server = http.createServer((req, res) => {
const pagePath = (page) => path.join('.next/serverless/pages/', page)
const render = (page) => {
require(`./${pagePath(page)}`).render(req, res)
}
switch (req.url) {
case '/revalidate': {
return render('/revalidate')
}
default: {
return res.end('404')
}
}
})
server.listen(process.env.PORT, () => {
console.log('ready on', process.env.PORT)
})
/* eslint-env jest */
import fs from 'fs-extra'
import { join } from 'path'
import {
nextBuild,
findPort,
killApp,
initNextServerScript,
} from 'next-test-utils'
const appDir = join(__dirname, '../')
jest.setTimeout(1000 * 60 * 2)
let appPort
let app
let buildId
const nextStart = async (appDir, appPort) => {
const scriptPath = join(appDir, 'server.js')
const env = Object.assign({ ...process.env }, { PORT: `${appPort}` })
return initNextServerScript(
scriptPath,
/ready on/i,
env,
/ReferenceError: options is not defined/
)
}
describe('Serverless Trace', () => {
beforeAll(async () => {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8')
})
afterAll(() => killApp(app))
it('should have revalidate page in prerender-manifest with correct interval', async () => {
const data = await fs.readJSON(
join(appDir, '.next/prerender-manifest.json')
)
expect(data.routes['/revalidate']).toEqual({
initialRevalidateSeconds: 10,
dataRoute: `/_next/data/${buildId}/revalidate.json`,
srcRoute: null,
})
})
it('should set correct Cache-Control header', async () => {
const url = `http://localhost:${appPort}/revalidate`
const res = await fetch(url)
expect(res.headers.get('Cache-Control')).toMatch(
's-maxage=10, stale-while-revalidate'
)
})
})
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册