2021-08-18 17:41:40 +02:00
|
|
|
import glob from 'glob'
|
2021-11-09 18:03:20 +01:00
|
|
|
import fs from 'fs-extra'
|
2020-12-16 21:46:55 +01:00
|
|
|
import cheerio from 'cheerio'
|
2021-11-09 18:03:20 +01:00
|
|
|
import { join } from 'path'
|
2021-10-05 20:52:19 +02:00
|
|
|
import { createNext, FileRef } from 'e2e-utils'
|
|
|
|
import { NextInstance } from 'test/lib/next-modes/base'
|
2020-12-16 21:46:55 +01:00
|
|
|
import {
|
2021-10-23 01:49:38 +02:00
|
|
|
check,
|
2020-12-16 21:46:55 +01:00
|
|
|
fetchViaHTTP,
|
|
|
|
findPort,
|
2021-08-18 17:41:40 +02:00
|
|
|
initNextServerScript,
|
|
|
|
killApp,
|
2020-12-16 21:46:55 +01:00
|
|
|
renderViaHTTP,
|
2022-03-03 00:06:54 +01:00
|
|
|
waitFor,
|
2020-12-16 21:46:55 +01:00
|
|
|
} from 'next-test-utils'
|
|
|
|
|
2021-10-05 20:52:19 +02:00
|
|
|
describe('should set-up next', () => {
|
|
|
|
let next: NextInstance
|
|
|
|
let server
|
|
|
|
let appPort
|
|
|
|
let errors = []
|
2022-04-05 17:57:45 +02:00
|
|
|
let stderr = ''
|
2021-10-05 20:52:19 +02:00
|
|
|
let requiredFilesManifest
|
2020-12-16 21:46:55 +01:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
2022-02-09 19:37:55 +01:00
|
|
|
// test build against environment with next support
|
|
|
|
process.env.NOW_BUILDER = '1'
|
|
|
|
|
2021-10-05 20:52:19 +02:00
|
|
|
next = await createNext({
|
|
|
|
files: {
|
|
|
|
pages: new FileRef(join(__dirname, 'required-server-files/pages')),
|
|
|
|
lib: new FileRef(join(__dirname, 'required-server-files/lib')),
|
2022-05-19 17:46:21 +02:00
|
|
|
'middleware.js': new FileRef(
|
|
|
|
join(__dirname, 'required-server-files/middleware.js')
|
|
|
|
),
|
2021-10-05 20:52:19 +02:00
|
|
|
'data.txt': new FileRef(
|
|
|
|
join(__dirname, 'required-server-files/data.txt')
|
|
|
|
),
|
2022-02-10 18:48:14 +01:00
|
|
|
'.env': new FileRef(join(__dirname, 'required-server-files/.env')),
|
|
|
|
'.env.local': new FileRef(
|
|
|
|
join(__dirname, 'required-server-files/.env.local')
|
|
|
|
),
|
|
|
|
'.env.production': new FileRef(
|
|
|
|
join(__dirname, 'required-server-files/.env.production')
|
|
|
|
),
|
2021-08-18 17:41:40 +02:00
|
|
|
},
|
2021-10-05 20:52:19 +02:00
|
|
|
nextConfig: {
|
|
|
|
eslint: {
|
|
|
|
ignoreDuringBuilds: true,
|
|
|
|
},
|
2022-06-24 21:58:35 +02:00
|
|
|
output: 'standalone',
|
2021-10-05 20:52:19 +02:00
|
|
|
async rewrites() {
|
2022-04-26 21:12:29 +02:00
|
|
|
return {
|
|
|
|
beforeFiles: [],
|
|
|
|
fallback: [
|
|
|
|
{
|
|
|
|
source: '/an-ssg-path',
|
|
|
|
destination: '/hello.txt',
|
|
|
|
},
|
2022-05-03 22:40:36 +02:00
|
|
|
{
|
|
|
|
source: '/fallback-false/:path',
|
|
|
|
destination: '/hello.txt',
|
|
|
|
},
|
2022-04-26 21:12:29 +02:00
|
|
|
],
|
|
|
|
afterFiles: [
|
|
|
|
{
|
|
|
|
source: '/some-catch-all/:path*',
|
|
|
|
destination: '/',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
source: '/to-dynamic/post-2',
|
|
|
|
destination: '/dynamic/post-2?hello=world',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
source: '/to-dynamic/:path',
|
|
|
|
destination: '/dynamic/:path',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
2021-10-05 20:52:19 +02:00
|
|
|
},
|
2021-03-03 20:20:48 +01:00
|
|
|
},
|
|
|
|
})
|
2021-10-05 20:52:19 +02:00
|
|
|
await next.stop()
|
2020-12-16 21:46:55 +01:00
|
|
|
|
2021-10-05 20:52:19 +02:00
|
|
|
requiredFilesManifest = JSON.parse(
|
|
|
|
await next.readFile('.next/required-server-files.json')
|
2021-08-18 17:41:40 +02:00
|
|
|
)
|
2021-11-09 18:03:20 +01:00
|
|
|
await fs.move(
|
|
|
|
join(next.testDir, '.next/standalone'),
|
|
|
|
join(next.testDir, 'standalone')
|
|
|
|
)
|
|
|
|
for (const file of await fs.readdir(next.testDir)) {
|
|
|
|
if (file !== 'standalone') {
|
|
|
|
await fs.remove(join(next.testDir, file))
|
|
|
|
console.log('removed', file)
|
2021-10-05 20:52:19 +02:00
|
|
|
}
|
|
|
|
}
|
2021-11-09 18:03:20 +01:00
|
|
|
const files = glob.sync('**/*', {
|
|
|
|
cwd: join(next.testDir, 'standalone/.next/server/pages'),
|
2021-10-05 20:52:19 +02:00
|
|
|
dot: true,
|
2021-08-18 17:41:40 +02:00
|
|
|
})
|
|
|
|
|
2021-11-09 18:03:20 +01:00
|
|
|
for (const file of files) {
|
|
|
|
if (file.endsWith('.json') || file.endsWith('.html')) {
|
|
|
|
await fs.remove(join(next.testDir, '.next/server', file))
|
2020-12-16 21:46:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-09 18:03:20 +01:00
|
|
|
const testServer = join(next.testDir, 'standalone/server.js')
|
|
|
|
await fs.writeFile(
|
2021-10-05 20:52:19 +02:00
|
|
|
testServer,
|
2021-11-09 18:03:20 +01:00
|
|
|
(await fs.readFile(testServer, 'utf8'))
|
|
|
|
.replace('console.error(err)', `console.error('top-level', err)`)
|
|
|
|
.replace('conf:', 'minimalMode: true,conf:')
|
2021-08-18 17:41:40 +02:00
|
|
|
)
|
2021-11-09 18:03:20 +01:00
|
|
|
appPort = await findPort()
|
2021-08-18 17:41:40 +02:00
|
|
|
server = await initNextServerScript(
|
2021-10-05 20:52:19 +02:00
|
|
|
testServer,
|
2021-11-09 18:03:20 +01:00
|
|
|
/Listening on/,
|
2021-08-18 17:41:40 +02:00
|
|
|
{
|
|
|
|
...process.env,
|
2021-11-09 18:03:20 +01:00
|
|
|
PORT: appPort,
|
2021-08-18 17:41:40 +02:00
|
|
|
},
|
|
|
|
undefined,
|
|
|
|
{
|
2021-10-05 20:52:19 +02:00
|
|
|
cwd: next.testDir,
|
2021-08-18 17:41:40 +02:00
|
|
|
onStderr(msg) {
|
|
|
|
if (msg.includes('top-level')) {
|
|
|
|
errors.push(msg)
|
|
|
|
}
|
2022-04-05 17:57:45 +02:00
|
|
|
stderr += msg
|
2021-08-18 17:41:40 +02:00
|
|
|
},
|
2020-12-16 21:46:55 +01:00
|
|
|
}
|
2021-08-18 17:41:40 +02:00
|
|
|
)
|
2020-12-16 21:46:55 +01:00
|
|
|
})
|
|
|
|
afterAll(async () => {
|
2021-10-05 20:52:19 +02:00
|
|
|
await next.destroy()
|
|
|
|
if (server) await killApp(server)
|
2020-12-16 21:46:55 +01:00
|
|
|
})
|
|
|
|
|
2022-06-28 01:47:32 +02:00
|
|
|
it('should resolve correctly when a redirect is returned', async () => {
|
|
|
|
const toRename = `standalone/.next/server/pages/route-resolving/[slug]/[project].html`
|
|
|
|
await next.renameFile(toRename, `${toRename}.bak`)
|
|
|
|
try {
|
|
|
|
const res = await fetchViaHTTP(
|
|
|
|
appPort,
|
|
|
|
'/route-resolving/import/first',
|
|
|
|
undefined,
|
|
|
|
{
|
|
|
|
redirect: 'manual',
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': '/route-resolving/import/[slug]',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
expect(res.status).toBe(307)
|
|
|
|
expect(new URL(res.headers.get('location'), 'http://n').pathname).toBe(
|
|
|
|
'/somewhere'
|
|
|
|
)
|
|
|
|
|
|
|
|
await waitFor(3000)
|
|
|
|
expect(stderr).not.toContain('ENOENT')
|
|
|
|
} finally {
|
|
|
|
await next.renameFile(`${toRename}.bak`, toRename)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-06-27 21:15:09 +02:00
|
|
|
it('should show invariant when an automatic static page is requested', async () => {
|
|
|
|
const toRename = `standalone/.next/server/pages/auto-static.html`
|
|
|
|
await next.renameFile(toRename, `${toRename}.bak`)
|
|
|
|
|
|
|
|
try {
|
|
|
|
const res = await fetchViaHTTP(appPort, '/auto-static', undefined, {
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': '/auto-static',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(res.status).toBe(500)
|
|
|
|
await check(() => stderr, /Invariant: failed to load static page/)
|
|
|
|
} finally {
|
|
|
|
await next.renameFile(`${toRename}.bak`, toRename)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-06-23 20:23:30 +02:00
|
|
|
it.each([
|
|
|
|
{
|
|
|
|
case: 'redirect no revalidate',
|
|
|
|
path: '/optional-ssg/redirect-1',
|
|
|
|
dest: '/somewhere',
|
|
|
|
cacheControl: 's-maxage=31536000, stale-while-revalidate',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
case: 'redirect with revalidate',
|
|
|
|
path: '/optional-ssg/redirect-2',
|
|
|
|
dest: '/somewhere-else',
|
|
|
|
cacheControl: 's-maxage=5, stale-while-revalidate',
|
|
|
|
},
|
|
|
|
])(
|
|
|
|
`should have correct cache-control for $case`,
|
|
|
|
async ({ path, dest, cacheControl }) => {
|
|
|
|
const res = await fetchViaHTTP(appPort, path, undefined, {
|
|
|
|
redirect: 'manual',
|
|
|
|
})
|
|
|
|
expect(res.status).toBe(307)
|
|
|
|
expect(new URL(res.headers.get('location'), 'http://n').pathname).toBe(
|
|
|
|
dest
|
|
|
|
)
|
|
|
|
expect(res.headers.get('cache-control')).toBe(cacheControl)
|
|
|
|
|
|
|
|
const dataRes = await fetchViaHTTP(
|
|
|
|
appPort,
|
|
|
|
`/_next/data/${next.buildId}${path}.json`,
|
|
|
|
undefined,
|
|
|
|
{
|
|
|
|
redirect: 'manual',
|
|
|
|
}
|
|
|
|
)
|
|
|
|
expect(dataRes.headers.get('cache-control')).toBe(cacheControl)
|
|
|
|
expect((await dataRes.json()).pageProps).toEqual({
|
|
|
|
__N_REDIRECT: dest,
|
|
|
|
__N_REDIRECT_STATUS: 307,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
it.each([
|
|
|
|
{
|
|
|
|
case: 'notFound no revalidate',
|
|
|
|
path: '/optional-ssg/not-found-1',
|
|
|
|
dest: '/somewhere',
|
|
|
|
cacheControl: 's-maxage=31536000, stale-while-revalidate',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
case: 'notFound with revalidate',
|
|
|
|
path: '/optional-ssg/not-found-2',
|
|
|
|
dest: '/somewhere-else',
|
|
|
|
cacheControl: 's-maxage=5, stale-while-revalidate',
|
|
|
|
},
|
|
|
|
])(
|
|
|
|
`should have correct cache-control for $case`,
|
|
|
|
async ({ path, dest, cacheControl }) => {
|
|
|
|
const res = await fetchViaHTTP(appPort, path, undefined, {
|
|
|
|
redirect: 'manual',
|
|
|
|
})
|
|
|
|
expect(res.status).toBe(404)
|
|
|
|
expect(res.headers.get('cache-control')).toBe(cacheControl)
|
|
|
|
|
|
|
|
const dataRes = await fetchViaHTTP(
|
|
|
|
appPort,
|
|
|
|
`/_next/data/${next.buildId}${path}.json`,
|
|
|
|
undefined,
|
|
|
|
{
|
|
|
|
redirect: 'manual',
|
|
|
|
}
|
|
|
|
)
|
|
|
|
expect(dataRes.headers.get('cache-control')).toBe(cacheControl)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
it('should have the correct cache-control for props with no revalidate', async () => {
|
|
|
|
const res = await fetchViaHTTP(appPort, '/optional-ssg/props-no-revalidate')
|
|
|
|
expect(res.status).toBe(200)
|
|
|
|
expect(res.headers.get('cache-control')).toBe(
|
|
|
|
's-maxage=31536000, stale-while-revalidate'
|
|
|
|
)
|
|
|
|
const $ = cheerio.load(await res.text())
|
|
|
|
expect(JSON.parse($('#props').text()).params).toEqual({
|
|
|
|
rest: ['props-no-revalidate'],
|
|
|
|
})
|
|
|
|
|
|
|
|
const dataRes = await fetchViaHTTP(
|
|
|
|
appPort,
|
|
|
|
`/_next/data/${next.buildId}/optional-ssg/props-no-revalidate.json`,
|
|
|
|
undefined
|
|
|
|
)
|
|
|
|
expect(dataRes.status).toBe(200)
|
|
|
|
expect(res.headers.get('cache-control')).toBe(
|
|
|
|
's-maxage=31536000, stale-while-revalidate'
|
|
|
|
)
|
|
|
|
expect((await dataRes.json()).pageProps.params).toEqual({
|
|
|
|
rest: ['props-no-revalidate'],
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-04-05 17:57:45 +02:00
|
|
|
it('should warn when "next" is imported directly', async () => {
|
|
|
|
await renderViaHTTP(appPort, '/gssp')
|
|
|
|
await check(
|
|
|
|
() => stderr,
|
|
|
|
/"next" should not be imported directly, imported in/
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-01-27 03:13:10 +01:00
|
|
|
it('`compress` should be `true` by default', async () => {
|
|
|
|
expect(
|
|
|
|
await fs.readFileSync(join(next.testDir, 'standalone/server.js'), 'utf8')
|
|
|
|
).toContain('"compress":true')
|
|
|
|
})
|
|
|
|
|
2022-01-04 01:47:18 +01:00
|
|
|
it('should output middleware correctly', async () => {
|
|
|
|
expect(
|
2022-01-10 18:41:53 +01:00
|
|
|
await fs.pathExists(
|
2022-04-27 11:50:29 +02:00
|
|
|
join(next.testDir, 'standalone/.next/server/edge-runtime-webpack.js')
|
2022-01-10 18:41:53 +01:00
|
|
|
)
|
2022-01-04 01:47:18 +01:00
|
|
|
).toBe(true)
|
|
|
|
expect(
|
|
|
|
await fs.pathExists(
|
2022-05-19 17:46:21 +02:00
|
|
|
join(next.testDir, 'standalone/.next/server/middleware.js')
|
2022-01-06 17:20:43 +01:00
|
|
|
)
|
|
|
|
).toBe(true)
|
2022-01-04 01:47:18 +01:00
|
|
|
})
|
|
|
|
|
2020-12-16 21:46:55 +01:00
|
|
|
it('should output required-server-files manifest correctly', async () => {
|
|
|
|
expect(requiredFilesManifest.version).toBe(1)
|
|
|
|
expect(Array.isArray(requiredFilesManifest.files)).toBe(true)
|
|
|
|
expect(Array.isArray(requiredFilesManifest.ignore)).toBe(true)
|
|
|
|
expect(requiredFilesManifest.files.length).toBeGreaterThan(0)
|
|
|
|
expect(requiredFilesManifest.ignore.length).toBeGreaterThan(0)
|
|
|
|
expect(typeof requiredFilesManifest.config.configFile).toBe('undefined')
|
|
|
|
expect(typeof requiredFilesManifest.config.trailingSlash).toBe('boolean')
|
2021-03-09 19:41:57 +01:00
|
|
|
expect(typeof requiredFilesManifest.appDir).toBe('string')
|
2020-12-16 21:46:55 +01:00
|
|
|
})
|
|
|
|
|
2022-03-03 00:06:54 +01:00
|
|
|
it('should de-dupe HTML/data requests', async () => {
|
|
|
|
const res = await fetchViaHTTP(appPort, '/gsp', undefined, {
|
|
|
|
redirect: 'manual',
|
|
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
2022-05-10 00:08:29 +02:00
|
|
|
expect(res.headers.get('x-nextjs-cache')).toBeFalsy()
|
2022-03-03 00:06:54 +01:00
|
|
|
const $ = cheerio.load(await res.text())
|
|
|
|
const props = JSON.parse($('#props').text())
|
|
|
|
expect(props.gspCalls).toBeDefined()
|
|
|
|
|
|
|
|
const res2 = await fetchViaHTTP(
|
|
|
|
appPort,
|
|
|
|
`/_next/data/${next.buildId}/gsp.json`,
|
|
|
|
undefined,
|
|
|
|
{
|
|
|
|
redirect: 'manual',
|
|
|
|
}
|
|
|
|
)
|
|
|
|
expect(res2.status).toBe(200)
|
2022-05-10 00:08:29 +02:00
|
|
|
expect(res2.headers.get('x-nextjs-cache')).toBeFalsy()
|
2022-03-03 00:06:54 +01:00
|
|
|
const { pageProps: props2 } = await res2.json()
|
|
|
|
expect(props2.gspCalls).toBe(props.gspCalls)
|
2022-03-23 15:28:04 +01:00
|
|
|
|
|
|
|
const res3 = await fetchViaHTTP(appPort, '/index', undefined, {
|
|
|
|
redirect: 'manual',
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': '/index',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
expect(res3.status).toBe(200)
|
|
|
|
const $2 = cheerio.load(await res3.text())
|
|
|
|
const props3 = JSON.parse($2('#props').text())
|
|
|
|
expect(props3.gspCalls).toBeDefined()
|
|
|
|
|
|
|
|
const res4 = await fetchViaHTTP(
|
|
|
|
appPort,
|
|
|
|
`/_next/data/${next.buildId}/index.json`,
|
|
|
|
undefined,
|
|
|
|
{
|
|
|
|
redirect: 'manual',
|
|
|
|
}
|
|
|
|
)
|
|
|
|
expect(res4.status).toBe(200)
|
|
|
|
const { pageProps: props4 } = await res4.json()
|
|
|
|
expect(props4.gspCalls).toBe(props3.gspCalls)
|
2022-03-03 00:06:54 +01:00
|
|
|
})
|
|
|
|
|
2022-04-07 18:32:45 +02:00
|
|
|
it('should cap de-dupe previousCacheItem expires time', async () => {
|
|
|
|
const res = await fetchViaHTTP(appPort, '/gsp-long-revalidate', undefined, {
|
|
|
|
redirect: 'manual',
|
|
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
|
|
const $ = cheerio.load(await res.text())
|
|
|
|
const props = JSON.parse($('#props').text())
|
|
|
|
expect(props.gspCalls).toBeDefined()
|
|
|
|
|
|
|
|
await waitFor(1000)
|
|
|
|
|
|
|
|
const res2 = await fetchViaHTTP(
|
|
|
|
appPort,
|
|
|
|
`/_next/data/${next.buildId}/gsp-long-revalidate.json`,
|
|
|
|
undefined,
|
|
|
|
{
|
|
|
|
redirect: 'manual',
|
|
|
|
}
|
|
|
|
)
|
|
|
|
expect(res2.status).toBe(200)
|
|
|
|
const { pageProps: props2 } = await res2.json()
|
|
|
|
expect(props2.gspCalls).not.toBe(props.gspCalls)
|
|
|
|
})
|
|
|
|
|
2022-04-13 22:04:00 +02:00
|
|
|
it('should not 404 for onlyGenerated manual revalidate in minimal mode', async () => {
|
|
|
|
const previewProps = JSON.parse(
|
|
|
|
await next.readFile('standalone/.next/prerender-manifest.json')
|
|
|
|
).preview
|
|
|
|
|
|
|
|
const res = await fetchViaHTTP(
|
|
|
|
appPort,
|
|
|
|
'/optional-ssg/only-generated-1',
|
|
|
|
undefined,
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
'x-prerender-revalidate': previewProps.previewModeId,
|
|
|
|
'x-prerender-revalidate-if-generated': '1',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
expect(res.status).toBe(200)
|
|
|
|
})
|
|
|
|
|
2021-09-10 10:15:13 +02:00
|
|
|
it('should set correct SWR headers with notFound gsp', async () => {
|
2022-03-03 00:06:54 +01:00
|
|
|
await waitFor(2000)
|
2021-11-09 18:03:20 +01:00
|
|
|
await next.patchFile('standalone/data.txt', 'show')
|
2021-09-10 10:15:13 +02:00
|
|
|
|
|
|
|
const res = await fetchViaHTTP(appPort, '/gsp', undefined, {
|
|
|
|
redirect: 'manual ',
|
|
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
|
|
expect(res.headers.get('cache-control')).toBe(
|
|
|
|
's-maxage=1, stale-while-revalidate'
|
|
|
|
)
|
|
|
|
|
2022-03-03 00:06:54 +01:00
|
|
|
await waitFor(2000)
|
2021-11-09 18:03:20 +01:00
|
|
|
await next.patchFile('standalone/data.txt', 'hide')
|
2021-09-10 10:15:13 +02:00
|
|
|
|
|
|
|
const res2 = await fetchViaHTTP(appPort, '/gsp', undefined, {
|
|
|
|
redirect: 'manual ',
|
|
|
|
})
|
|
|
|
expect(res2.status).toBe(404)
|
|
|
|
expect(res2.headers.get('cache-control')).toBe(
|
|
|
|
's-maxage=1, stale-while-revalidate'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should set correct SWR headers with notFound gssp', async () => {
|
2021-11-09 18:03:20 +01:00
|
|
|
await next.patchFile('standalone/data.txt', 'show')
|
2021-09-10 10:15:13 +02:00
|
|
|
|
|
|
|
const res = await fetchViaHTTP(appPort, '/gssp', undefined, {
|
|
|
|
redirect: 'manual ',
|
|
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
|
|
expect(res.headers.get('cache-control')).toBe(
|
|
|
|
's-maxage=1, stale-while-revalidate'
|
|
|
|
)
|
|
|
|
|
2021-11-09 18:03:20 +01:00
|
|
|
await next.patchFile('standalone/data.txt', 'hide')
|
2021-09-10 10:15:13 +02:00
|
|
|
|
|
|
|
const res2 = await fetchViaHTTP(appPort, '/gssp', undefined, {
|
|
|
|
redirect: 'manual ',
|
|
|
|
})
|
2022-03-23 15:28:04 +01:00
|
|
|
await next.patchFile('standalone/data.txt', 'show')
|
|
|
|
|
2021-09-10 10:15:13 +02:00
|
|
|
expect(res2.status).toBe(404)
|
|
|
|
expect(res2.headers.get('cache-control')).toBe(
|
|
|
|
's-maxage=1, stale-while-revalidate'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-12-16 21:46:55 +01:00
|
|
|
it('should render SSR page correctly', async () => {
|
2022-03-23 15:28:04 +01:00
|
|
|
const html = await renderViaHTTP(appPort, '/gssp')
|
2020-12-16 21:46:55 +01:00
|
|
|
const $ = cheerio.load(html)
|
|
|
|
const data = JSON.parse($('#props').text())
|
|
|
|
|
2022-03-23 15:28:04 +01:00
|
|
|
expect($('#gssp').text()).toBe('getServerSideProps page')
|
2020-12-16 21:46:55 +01:00
|
|
|
expect(data.hello).toBe('world')
|
|
|
|
|
2022-03-23 15:28:04 +01:00
|
|
|
const html2 = await renderViaHTTP(appPort, '/gssp')
|
2020-12-16 21:46:55 +01:00
|
|
|
const $2 = cheerio.load(html2)
|
|
|
|
const data2 = JSON.parse($2('#props').text())
|
|
|
|
|
2022-03-23 15:28:04 +01:00
|
|
|
expect($2('#gssp').text()).toBe('getServerSideProps page')
|
2020-12-16 21:46:55 +01:00
|
|
|
expect(isNaN(data2.random)).toBe(false)
|
|
|
|
expect(data2.random).not.toBe(data.random)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should render dynamic SSR page correctly', async () => {
|
|
|
|
const html = await renderViaHTTP(appPort, '/dynamic/first')
|
|
|
|
const $ = cheerio.load(html)
|
|
|
|
const data = JSON.parse($('#props').text())
|
|
|
|
|
|
|
|
expect($('#dynamic').text()).toBe('dynamic page')
|
|
|
|
expect($('#slug').text()).toBe('first')
|
|
|
|
expect(data.hello).toBe('world')
|
|
|
|
|
|
|
|
const html2 = await renderViaHTTP(appPort, '/dynamic/second')
|
|
|
|
const $2 = cheerio.load(html2)
|
|
|
|
const data2 = JSON.parse($2('#props').text())
|
|
|
|
|
|
|
|
expect($2('#dynamic').text()).toBe('dynamic page')
|
|
|
|
expect($2('#slug').text()).toBe('second')
|
|
|
|
expect(isNaN(data2.random)).toBe(false)
|
|
|
|
expect(data2.random).not.toBe(data.random)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should render fallback page correctly', async () => {
|
|
|
|
const html = await renderViaHTTP(appPort, '/fallback/first')
|
|
|
|
const $ = cheerio.load(html)
|
|
|
|
const data = JSON.parse($('#props').text())
|
|
|
|
|
|
|
|
expect($('#fallback').text()).toBe('fallback page')
|
|
|
|
expect($('#slug').text()).toBe('first')
|
|
|
|
expect(data.hello).toBe('world')
|
|
|
|
|
2022-03-03 00:06:54 +01:00
|
|
|
await waitFor(2000)
|
2020-12-16 21:46:55 +01:00
|
|
|
const html2 = await renderViaHTTP(appPort, '/fallback/first')
|
|
|
|
const $2 = cheerio.load(html2)
|
|
|
|
const data2 = JSON.parse($2('#props').text())
|
|
|
|
|
|
|
|
expect($2('#fallback').text()).toBe('fallback page')
|
|
|
|
expect($2('#slug').text()).toBe('first')
|
|
|
|
expect(isNaN(data2.random)).toBe(false)
|
|
|
|
expect(data2.random).not.toBe(data.random)
|
|
|
|
|
|
|
|
const html3 = await renderViaHTTP(appPort, '/fallback/second')
|
|
|
|
const $3 = cheerio.load(html3)
|
|
|
|
const data3 = JSON.parse($3('#props').text())
|
|
|
|
|
|
|
|
expect($3('#fallback').text()).toBe('fallback page')
|
|
|
|
expect($3('#slug').text()).toBe('second')
|
|
|
|
expect(isNaN(data3.random)).toBe(false)
|
|
|
|
|
|
|
|
const { pageProps: data4 } = JSON.parse(
|
2021-10-05 20:52:19 +02:00
|
|
|
await renderViaHTTP(
|
|
|
|
appPort,
|
|
|
|
`/_next/data/${next.buildId}/fallback/third.json`
|
|
|
|
)
|
2020-12-16 21:46:55 +01:00
|
|
|
)
|
|
|
|
expect(data4.hello).toBe('world')
|
|
|
|
expect(data4.slug).toBe('third')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should render SSR page correctly with x-matched-path', async () => {
|
|
|
|
const html = await renderViaHTTP(appPort, '/some-other-path', undefined, {
|
|
|
|
headers: {
|
2022-03-23 15:28:04 +01:00
|
|
|
'x-matched-path': '/gssp',
|
2020-12-16 21:46:55 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
const $ = cheerio.load(html)
|
|
|
|
const data = JSON.parse($('#props').text())
|
|
|
|
|
2022-03-23 15:28:04 +01:00
|
|
|
expect($('#gssp').text()).toBe('getServerSideProps page')
|
2020-12-16 21:46:55 +01:00
|
|
|
expect(data.hello).toBe('world')
|
|
|
|
|
|
|
|
const html2 = await renderViaHTTP(appPort, '/some-other-path', undefined, {
|
|
|
|
headers: {
|
2022-03-23 15:28:04 +01:00
|
|
|
'x-matched-path': '/gssp',
|
2020-12-16 21:46:55 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
const $2 = cheerio.load(html2)
|
|
|
|
const data2 = JSON.parse($2('#props').text())
|
|
|
|
|
2022-03-23 15:28:04 +01:00
|
|
|
expect($2('#gssp').text()).toBe('getServerSideProps page')
|
2020-12-16 21:46:55 +01:00
|
|
|
expect(isNaN(data2.random)).toBe(false)
|
|
|
|
expect(data2.random).not.toBe(data.random)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should render dynamic SSR page correctly with x-matched-path', async () => {
|
2022-06-10 19:35:12 +02:00
|
|
|
const html = await renderViaHTTP(
|
|
|
|
appPort,
|
|
|
|
'/some-other-path?slug=first',
|
|
|
|
undefined,
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': '/dynamic/[slug]',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
2020-12-16 21:46:55 +01:00
|
|
|
const $ = cheerio.load(html)
|
|
|
|
const data = JSON.parse($('#props').text())
|
|
|
|
|
|
|
|
expect($('#dynamic').text()).toBe('dynamic page')
|
|
|
|
expect($('#slug').text()).toBe('first')
|
|
|
|
expect(data.hello).toBe('world')
|
|
|
|
|
2022-06-10 19:35:12 +02:00
|
|
|
const html2 = await renderViaHTTP(
|
|
|
|
appPort,
|
|
|
|
'/some-other-path?slug=second',
|
|
|
|
undefined,
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': '/dynamic/[slug]',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
2020-12-16 21:46:55 +01:00
|
|
|
const $2 = cheerio.load(html2)
|
|
|
|
const data2 = JSON.parse($2('#props').text())
|
|
|
|
|
|
|
|
expect($2('#dynamic').text()).toBe('dynamic page')
|
|
|
|
expect($2('#slug').text()).toBe('second')
|
|
|
|
expect(isNaN(data2.random)).toBe(false)
|
|
|
|
expect(data2.random).not.toBe(data.random)
|
2021-05-18 21:24:22 +02:00
|
|
|
|
|
|
|
const html3 = await renderViaHTTP(appPort, '/some-other-path', undefined, {
|
|
|
|
headers: {
|
2022-06-10 19:35:12 +02:00
|
|
|
'x-matched-path': '/dynamic/[slug]',
|
2021-05-18 21:24:22 +02:00
|
|
|
'x-now-route-matches': '1=second&slug=second',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
const $3 = cheerio.load(html3)
|
|
|
|
const data3 = JSON.parse($3('#props').text())
|
|
|
|
|
|
|
|
expect($3('#dynamic').text()).toBe('dynamic page')
|
|
|
|
expect($3('#slug').text()).toBe('second')
|
|
|
|
expect(isNaN(data3.random)).toBe(false)
|
|
|
|
expect(data3.random).not.toBe(data.random)
|
2020-12-16 21:46:55 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should render fallback page correctly with x-matched-path and routes-matches', async () => {
|
|
|
|
const html = await renderViaHTTP(appPort, '/fallback/first', undefined, {
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': '/fallback/first',
|
|
|
|
'x-now-route-matches': '1=first',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
const $ = cheerio.load(html)
|
|
|
|
const data = JSON.parse($('#props').text())
|
|
|
|
|
|
|
|
expect($('#fallback').text()).toBe('fallback page')
|
|
|
|
expect($('#slug').text()).toBe('first')
|
|
|
|
expect(data.hello).toBe('world')
|
|
|
|
|
|
|
|
const html2 = await renderViaHTTP(appPort, `/fallback/[slug]`, undefined, {
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': '/fallback/[slug]',
|
|
|
|
'x-now-route-matches': '1=second',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
const $2 = cheerio.load(html2)
|
|
|
|
const data2 = JSON.parse($2('#props').text())
|
|
|
|
|
|
|
|
expect($2('#fallback').text()).toBe('fallback page')
|
|
|
|
expect($2('#slug').text()).toBe('second')
|
|
|
|
expect(isNaN(data2.random)).toBe(false)
|
|
|
|
expect(data2.random).not.toBe(data.random)
|
|
|
|
})
|
|
|
|
|
2022-06-13 15:34:08 +02:00
|
|
|
it('should favor valid route params over routes-matches', async () => {
|
|
|
|
const html = await renderViaHTTP(appPort, '/fallback/first', undefined, {
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': '/fallback/first',
|
|
|
|
'x-now-route-matches': '1=fallback%2ffirst',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
const $ = cheerio.load(html)
|
|
|
|
const data = JSON.parse($('#props').text())
|
|
|
|
|
|
|
|
expect($('#fallback').text()).toBe('fallback page')
|
|
|
|
expect($('#slug').text()).toBe('first')
|
|
|
|
expect(data.hello).toBe('world')
|
|
|
|
|
|
|
|
const html2 = await renderViaHTTP(appPort, `/fallback/second`, undefined, {
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': '/fallback/[slug]',
|
|
|
|
'x-now-route-matches': '1=fallback%2fsecond',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
const $2 = cheerio.load(html2)
|
|
|
|
const data2 = JSON.parse($2('#props').text())
|
|
|
|
|
|
|
|
expect($2('#fallback').text()).toBe('fallback page')
|
|
|
|
expect($2('#slug').text()).toBe('second')
|
|
|
|
expect(isNaN(data2.random)).toBe(false)
|
|
|
|
expect(data2.random).not.toBe(data.random)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should favor valid route params over routes-matches optional', async () => {
|
|
|
|
const html = await renderViaHTTP(appPort, '/optional-ssg', undefined, {
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': '/optional-ssg',
|
|
|
|
'x-now-route-matches': '1=optional-ssg',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
const $ = cheerio.load(html)
|
|
|
|
const data = JSON.parse($('#props').text())
|
|
|
|
expect(data.params).toEqual({})
|
|
|
|
|
|
|
|
const html2 = await renderViaHTTP(appPort, `/optional-ssg`, undefined, {
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': '/optional-ssg',
|
|
|
|
'x-now-route-matches': '1=optional-ssg%2fanother',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
const $2 = cheerio.load(html2)
|
|
|
|
const data2 = JSON.parse($2('#props').text())
|
|
|
|
|
|
|
|
expect(isNaN(data2.random)).toBe(false)
|
|
|
|
expect(data2.params).toEqual({})
|
|
|
|
})
|
|
|
|
|
2020-12-16 21:46:55 +01:00
|
|
|
it('should return data correctly with x-matched-path', async () => {
|
|
|
|
const res = await fetchViaHTTP(
|
|
|
|
appPort,
|
2022-06-10 19:35:12 +02:00
|
|
|
`/_next/data/${next.buildId}/dynamic/first.json?slug=first`,
|
2020-12-16 21:46:55 +01:00
|
|
|
undefined,
|
|
|
|
{
|
|
|
|
headers: {
|
2022-06-10 19:35:12 +02:00
|
|
|
'x-matched-path': `/dynamic/[slug]`,
|
2020-12-16 21:46:55 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const { pageProps: data } = await res.json()
|
|
|
|
|
|
|
|
expect(data.slug).toBe('first')
|
|
|
|
expect(data.hello).toBe('world')
|
|
|
|
|
|
|
|
const res2 = await fetchViaHTTP(
|
|
|
|
appPort,
|
2021-10-05 20:52:19 +02:00
|
|
|
`/_next/data/${next.buildId}/fallback/[slug].json`,
|
2020-12-16 21:46:55 +01:00
|
|
|
undefined,
|
|
|
|
{
|
|
|
|
headers: {
|
2021-10-05 20:52:19 +02:00
|
|
|
'x-matched-path': `/_next/data/${next.buildId}/fallback/[slug].json`,
|
2020-12-16 21:46:55 +01:00
|
|
|
'x-now-route-matches': '1=second',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const { pageProps: data2 } = await res2.json()
|
|
|
|
|
|
|
|
expect(data2.slug).toBe('second')
|
|
|
|
expect(data2.hello).toBe('world')
|
|
|
|
})
|
|
|
|
|
2021-01-16 16:46:21 +01:00
|
|
|
it('should render fallback optional catch-all route correctly with x-matched-path and routes-matches', async () => {
|
|
|
|
const html = await renderViaHTTP(
|
|
|
|
appPort,
|
|
|
|
'/catch-all/[[...rest]]',
|
|
|
|
undefined,
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': '/catch-all/[[...rest]]',
|
|
|
|
'x-now-route-matches': '',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
const $ = cheerio.load(html)
|
|
|
|
const data = JSON.parse($('#props').text())
|
|
|
|
|
|
|
|
expect($('#catch-all').text()).toBe('optional catch-all page')
|
|
|
|
expect(data.params).toEqual({})
|
|
|
|
expect(data.hello).toBe('world')
|
|
|
|
|
|
|
|
const html2 = await renderViaHTTP(
|
|
|
|
appPort,
|
|
|
|
'/catch-all/[[...rest]]',
|
|
|
|
undefined,
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': '/catch-all/[[...rest]]',
|
|
|
|
'x-now-route-matches': '1=hello&catchAll=hello',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
const $2 = cheerio.load(html2)
|
|
|
|
const data2 = JSON.parse($2('#props').text())
|
|
|
|
|
|
|
|
expect($2('#catch-all').text()).toBe('optional catch-all page')
|
|
|
|
expect(data2.params).toEqual({ rest: ['hello'] })
|
|
|
|
expect(isNaN(data2.random)).toBe(false)
|
|
|
|
expect(data2.random).not.toBe(data.random)
|
|
|
|
|
|
|
|
const html3 = await renderViaHTTP(
|
|
|
|
appPort,
|
2022-06-13 15:34:08 +02:00
|
|
|
'/catch-all/[[...rest]]',
|
2021-01-16 16:46:21 +01:00
|
|
|
undefined,
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': '/catch-all/[[...rest]]',
|
|
|
|
'x-now-route-matches': '1=hello/world&catchAll=hello/world',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
const $3 = cheerio.load(html3)
|
|
|
|
const data3 = JSON.parse($3('#props').text())
|
|
|
|
|
|
|
|
expect($3('#catch-all').text()).toBe('optional catch-all page')
|
|
|
|
expect(data3.params).toEqual({ rest: ['hello', 'world'] })
|
|
|
|
expect(isNaN(data3.random)).toBe(false)
|
|
|
|
expect(data3.random).not.toBe(data.random)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return data correctly with x-matched-path for optional catch-all route', async () => {
|
|
|
|
const res = await fetchViaHTTP(
|
|
|
|
appPort,
|
2021-10-05 20:52:19 +02:00
|
|
|
`/_next/data/${next.buildId}/catch-all.json`,
|
2022-06-10 19:35:12 +02:00
|
|
|
|
2021-01-16 16:46:21 +01:00
|
|
|
undefined,
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': '/catch-all/[[...rest]]',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const { pageProps: data } = await res.json()
|
|
|
|
|
|
|
|
expect(data.params).toEqual({})
|
|
|
|
expect(data.hello).toBe('world')
|
|
|
|
|
|
|
|
const res2 = await fetchViaHTTP(
|
|
|
|
appPort,
|
2021-10-05 20:52:19 +02:00
|
|
|
`/_next/data/${next.buildId}/catch-all/[[...rest]].json`,
|
2021-01-16 16:46:21 +01:00
|
|
|
undefined,
|
|
|
|
{
|
|
|
|
headers: {
|
2021-10-05 20:52:19 +02:00
|
|
|
'x-matched-path': `/_next/data/${next.buildId}/catch-all/[[...rest]].json`,
|
2021-01-16 16:46:21 +01:00
|
|
|
'x-now-route-matches': '1=hello&rest=hello',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const { pageProps: data2 } = await res2.json()
|
|
|
|
|
|
|
|
expect(data2.params).toEqual({ rest: ['hello'] })
|
|
|
|
expect(data2.hello).toBe('world')
|
|
|
|
|
|
|
|
const res3 = await fetchViaHTTP(
|
|
|
|
appPort,
|
2021-10-05 20:52:19 +02:00
|
|
|
`/_next/data/${next.buildId}/catch-all/[[...rest]].json`,
|
2021-01-16 16:46:21 +01:00
|
|
|
undefined,
|
|
|
|
{
|
|
|
|
headers: {
|
2021-10-05 20:52:19 +02:00
|
|
|
'x-matched-path': `/_next/data/${next.buildId}/catch-all/[[...rest]].json`,
|
2021-01-16 16:46:21 +01:00
|
|
|
'x-now-route-matches': '1=hello/world&rest=hello/world',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const { pageProps: data3 } = await res3.json()
|
|
|
|
|
|
|
|
expect(data3.params).toEqual({ rest: ['hello', 'world'] })
|
|
|
|
expect(data3.hello).toBe('world')
|
|
|
|
})
|
|
|
|
|
2020-12-16 21:46:55 +01:00
|
|
|
it('should not apply trailingSlash redirect', async () => {
|
|
|
|
for (const path of [
|
|
|
|
'/',
|
|
|
|
'/dynamic/another/',
|
|
|
|
'/dynamic/another',
|
|
|
|
'/fallback/first/',
|
|
|
|
'/fallback/first',
|
|
|
|
'/fallback/another/',
|
|
|
|
'/fallback/another',
|
|
|
|
]) {
|
|
|
|
const res = await fetchViaHTTP(appPort, path, undefined, {
|
|
|
|
redirect: 'manual',
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(res.status).toBe(200)
|
|
|
|
}
|
|
|
|
})
|
2021-01-16 16:46:21 +01:00
|
|
|
|
|
|
|
it('should normalize catch-all rewrite query values correctly', async () => {
|
|
|
|
const html = await renderViaHTTP(
|
|
|
|
appPort,
|
|
|
|
'/some-catch-all/hello/world',
|
|
|
|
{
|
|
|
|
path: 'hello/world',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
headers: {
|
2022-03-23 15:28:04 +01:00
|
|
|
'x-matched-path': '/gssp',
|
2021-01-16 16:46:21 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
const $ = cheerio.load(html)
|
|
|
|
expect(JSON.parse($('#router').text()).query).toEqual({
|
|
|
|
path: ['hello', 'world'],
|
|
|
|
})
|
|
|
|
})
|
2021-02-11 10:31:49 +01:00
|
|
|
|
2021-11-06 00:39:52 +01:00
|
|
|
it('should handle bad request correctly with rewrite', async () => {
|
|
|
|
const res = await fetchViaHTTP(
|
|
|
|
appPort,
|
|
|
|
'/to-dynamic/%c0.%c0.',
|
|
|
|
'?path=%c0.%c0.',
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': '/dynamic/[slug]',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
expect(res.status).toBe(400)
|
|
|
|
expect(await res.text()).toContain('Bad Request')
|
|
|
|
})
|
|
|
|
|
2022-04-22 10:00:33 +02:00
|
|
|
it('should have correct resolvedUrl from rewrite', async () => {
|
|
|
|
const res = await fetchViaHTTP(appPort, '/to-dynamic/post-1', undefined, {
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': '/dynamic/[slug]',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
|
|
const $ = cheerio.load(await res.text())
|
|
|
|
expect($('#resolved-url').text()).toBe('/dynamic/post-1')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should have correct resolvedUrl from rewrite with added query', async () => {
|
|
|
|
const res = await fetchViaHTTP(appPort, '/to-dynamic/post-2', undefined, {
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': '/dynamic/[slug]',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
|
|
const $ = cheerio.load(await res.text())
|
|
|
|
expect($('#resolved-url').text()).toBe('/dynamic/post-2')
|
2022-04-26 21:12:29 +02:00
|
|
|
expect(JSON.parse($('#router').text()).asPath).toBe('/to-dynamic/post-2')
|
2022-04-22 10:00:33 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should have correct resolvedUrl from dynamic route', async () => {
|
|
|
|
const res = await fetchViaHTTP(
|
|
|
|
appPort,
|
|
|
|
`/_next/data/${next.buildId}/dynamic/post-2.json`,
|
|
|
|
{ slug: 'post-2' },
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': '/dynamic/[slug]',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
expect(res.status).toBe(200)
|
|
|
|
const json = await res.json()
|
|
|
|
expect(json.pageProps.resolvedUrl).toBe('/dynamic/post-2')
|
|
|
|
})
|
|
|
|
|
2021-02-11 10:31:49 +01:00
|
|
|
it('should bubble error correctly for gip page', async () => {
|
|
|
|
errors = []
|
|
|
|
const res = await fetchViaHTTP(appPort, '/errors/gip', { crash: '1' })
|
|
|
|
expect(res.status).toBe(500)
|
2021-11-09 18:03:20 +01:00
|
|
|
expect(await res.text()).toBe('internal server error')
|
2021-10-23 01:49:38 +02:00
|
|
|
|
|
|
|
await check(
|
|
|
|
() => (errors[0].includes('gip hit an oops') ? 'success' : errors[0]),
|
|
|
|
'success'
|
|
|
|
)
|
2021-02-11 10:31:49 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should bubble error correctly for gssp page', async () => {
|
|
|
|
errors = []
|
|
|
|
const res = await fetchViaHTTP(appPort, '/errors/gssp', { crash: '1' })
|
|
|
|
expect(res.status).toBe(500)
|
2021-11-09 18:03:20 +01:00
|
|
|
expect(await res.text()).toBe('internal server error')
|
2021-10-23 01:49:38 +02:00
|
|
|
await check(
|
|
|
|
() => (errors[0].includes('gssp hit an oops') ? 'success' : errors[0]),
|
|
|
|
'success'
|
|
|
|
)
|
2021-02-11 10:31:49 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should bubble error correctly for gsp page', async () => {
|
|
|
|
errors = []
|
|
|
|
const res = await fetchViaHTTP(appPort, '/errors/gsp/crash')
|
|
|
|
expect(res.status).toBe(500)
|
2021-11-09 18:03:20 +01:00
|
|
|
expect(await res.text()).toBe('internal server error')
|
2021-10-23 01:49:38 +02:00
|
|
|
await check(
|
|
|
|
() => (errors[0].includes('gsp hit an oops') ? 'success' : errors[0]),
|
|
|
|
'success'
|
|
|
|
)
|
2021-02-11 10:31:49 +01:00
|
|
|
})
|
2021-03-02 20:51:53 +01:00
|
|
|
|
2021-07-02 22:54:53 +02:00
|
|
|
it('should bubble error correctly for API page', async () => {
|
|
|
|
errors = []
|
|
|
|
const res = await fetchViaHTTP(appPort, '/api/error')
|
|
|
|
expect(res.status).toBe(500)
|
2021-11-09 18:03:20 +01:00
|
|
|
expect(await res.text()).toBe('internal server error')
|
2021-10-23 01:49:38 +02:00
|
|
|
await check(
|
|
|
|
() =>
|
|
|
|
errors[0].includes('some error from /api/error')
|
|
|
|
? 'success'
|
|
|
|
: errors[0],
|
|
|
|
'success'
|
|
|
|
)
|
2021-07-02 22:54:53 +02:00
|
|
|
})
|
|
|
|
|
2021-03-02 20:51:53 +01:00
|
|
|
it('should normalize optional values correctly for SSP page', async () => {
|
|
|
|
const res = await fetchViaHTTP(
|
|
|
|
appPort,
|
|
|
|
'/optional-ssp',
|
|
|
|
{ rest: '', another: 'value' },
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': '/optional-ssp/[[...rest]]',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const html = await res.text()
|
|
|
|
const $ = cheerio.load(html)
|
|
|
|
const props = JSON.parse($('#props').text())
|
|
|
|
expect(props.params).toEqual({})
|
|
|
|
expect(props.query).toEqual({ another: 'value' })
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should normalize optional values correctly for SSG page', async () => {
|
|
|
|
const res = await fetchViaHTTP(
|
|
|
|
appPort,
|
|
|
|
'/optional-ssg',
|
|
|
|
{ rest: '', another: 'value' },
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': '/optional-ssg/[[...rest]]',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const html = await res.text()
|
|
|
|
const $ = cheerio.load(html)
|
|
|
|
const props = JSON.parse($('#props').text())
|
|
|
|
expect(props.params).toEqual({})
|
|
|
|
})
|
|
|
|
|
2022-04-27 20:46:33 +02:00
|
|
|
it('should normalize optional revalidations correctly for SSG page', async () => {
|
|
|
|
const reqs = [
|
|
|
|
{
|
|
|
|
path: `/_next/data/${next.buildId}/optional-ssg/[[...rest]].json`,
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': `/_next/data/${next.buildId}/optional-ssg/[[...rest]].json`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: `/_next/data/${next.buildId}/optional-ssg.json`,
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': `/_next/data/${next.buildId}/optional-ssg/[[...rest]].json`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: `/_next/data/${next.buildId}/optional-ssg.json`,
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': `/_next/data/${next.buildId}/optional-ssg.json`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: `/_next/data/${next.buildId}/optional-ssg/[[...rest]].json`,
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': `/_next/data/${next.buildId}/optional-ssg/[[...rest]].json`,
|
|
|
|
},
|
|
|
|
query: { rest: '' },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: `/_next/data/${next.buildId}/optional-ssg/[[...rest]].json`,
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': `/_next/data/${next.buildId}/optional-ssg/[[...rest]].json`,
|
|
|
|
'x-now-route-matches': '1=',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: `/_next/data/${next.buildId}/optional-ssg/.json`,
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': `/_next/data/${next.buildId}/optional-ssg/[[...rest]].json`,
|
|
|
|
'x-now-route-matches': '',
|
|
|
|
'x-vercel-id': 'cle1::',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: `/optional-ssg/[[...rest]]`,
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': `/_next/data/${next.buildId}/optional-ssg/[[...rest]].json`,
|
|
|
|
'x-now-route-matches': '',
|
|
|
|
'x-vercel-id': 'cle1::',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: `/_next/data/${next.buildId}/optional-ssg/[[...rest]].json`,
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': `/optional-ssg/[[...rest]]`,
|
|
|
|
'x-now-route-matches': '',
|
|
|
|
'x-vercel-id': 'cle1::',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
for (const req of reqs) {
|
|
|
|
console.error('checking', req)
|
|
|
|
const res = await fetchViaHTTP(appPort, req.path, req.query, {
|
|
|
|
headers: req.headers,
|
|
|
|
})
|
|
|
|
|
|
|
|
const content = await res.text()
|
|
|
|
let props
|
|
|
|
|
|
|
|
try {
|
|
|
|
const data = JSON.parse(content)
|
|
|
|
props = data.pageProps
|
|
|
|
} catch (_) {
|
|
|
|
props = JSON.parse(cheerio.load(content)('#__NEXT_DATA__').text()).props
|
|
|
|
.pageProps
|
|
|
|
}
|
|
|
|
expect(props.params).toEqual({})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-07-06 23:28:43 +02:00
|
|
|
it('should normalize optional values correctly for SSG page with encoded slash', async () => {
|
|
|
|
const res = await fetchViaHTTP(
|
|
|
|
appPort,
|
|
|
|
'/optional-ssg/[[...rest]]',
|
|
|
|
undefined,
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': '/optional-ssg/[[...rest]]',
|
|
|
|
'x-now-route-matches':
|
|
|
|
'1=en%2Fes%2Fhello%252Fworld&rest=en%2Fes%2Fhello%252Fworld',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const html = await res.text()
|
|
|
|
const $ = cheerio.load(html)
|
|
|
|
const props = JSON.parse($('#props').text())
|
|
|
|
expect(props.params).toEqual({
|
|
|
|
rest: ['en', 'es', 'hello/world'],
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-03-02 20:51:53 +01:00
|
|
|
it('should normalize optional values correctly for API page', async () => {
|
|
|
|
const res = await fetchViaHTTP(
|
|
|
|
appPort,
|
|
|
|
'/api/optional',
|
|
|
|
{ rest: '', another: 'value' },
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': '/api/optional/[[...rest]]',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const json = await res.json()
|
|
|
|
expect(json.query).toEqual({ another: 'value' })
|
|
|
|
expect(json.url).toBe('/api/optional?another=value')
|
|
|
|
})
|
2021-03-04 23:09:45 +01:00
|
|
|
|
2022-01-22 00:16:24 +01:00
|
|
|
it('should normalize index optional values correctly for API page', async () => {
|
|
|
|
const res = await fetchViaHTTP(
|
|
|
|
appPort,
|
|
|
|
'/api/optional/index',
|
|
|
|
{ rest: 'index', another: 'value' },
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': '/api/optional/[[...rest]]',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const json = await res.json()
|
|
|
|
expect(json.query).toEqual({ another: 'value', rest: ['index'] })
|
|
|
|
expect(json.url).toBe('/api/optional/index?another=value')
|
|
|
|
})
|
|
|
|
|
2021-03-04 23:09:45 +01:00
|
|
|
it('should match the index page correctly', async () => {
|
|
|
|
const res = await fetchViaHTTP(appPort, '/', undefined, {
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': '/index',
|
|
|
|
},
|
|
|
|
redirect: 'manual',
|
|
|
|
})
|
|
|
|
|
|
|
|
const html = await res.text()
|
|
|
|
const $ = cheerio.load(html)
|
|
|
|
expect($('#index').text()).toBe('index page')
|
|
|
|
})
|
|
|
|
|
2022-03-29 05:53:51 +02:00
|
|
|
it('should match the root dynamic page correctly', async () => {
|
2022-06-13 15:34:08 +02:00
|
|
|
const res = await fetchViaHTTP(appPort, '/slug-1', undefined, {
|
2021-03-04 23:09:45 +01:00
|
|
|
headers: {
|
|
|
|
'x-matched-path': '/[slug]',
|
|
|
|
},
|
|
|
|
redirect: 'manual',
|
|
|
|
})
|
|
|
|
|
|
|
|
const html = await res.text()
|
|
|
|
const $ = cheerio.load(html)
|
|
|
|
expect($('#slug-page').text()).toBe('[slug] page')
|
2022-06-13 22:46:19 +02:00
|
|
|
expect(JSON.parse($('#router').text()).query).toEqual({
|
|
|
|
slug: 'slug-1',
|
|
|
|
})
|
|
|
|
|
|
|
|
const res2 = await fetchViaHTTP(appPort, '/[slug]', undefined, {
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': '/[slug]',
|
|
|
|
},
|
|
|
|
redirect: 'manual',
|
|
|
|
})
|
|
|
|
|
|
|
|
const html2 = await res2.text()
|
|
|
|
const $2 = cheerio.load(html2)
|
|
|
|
expect($2('#slug-page').text()).toBe('[slug] page')
|
|
|
|
expect(JSON.parse($2('#router').text()).query).toEqual({
|
|
|
|
slug: '[slug]',
|
|
|
|
})
|
2021-03-04 23:09:45 +01:00
|
|
|
})
|
2022-02-10 18:48:14 +01:00
|
|
|
|
2022-04-26 21:12:29 +02:00
|
|
|
it('should have correct asPath on dynamic SSG page correctly', async () => {
|
|
|
|
const res = await fetchViaHTTP(appPort, '/an-ssg-path', undefined, {
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': '/[slug]',
|
|
|
|
},
|
|
|
|
redirect: 'manual',
|
|
|
|
})
|
|
|
|
|
|
|
|
const html = await res.text()
|
|
|
|
const $ = cheerio.load(html)
|
|
|
|
expect($('#slug-page').text()).toBe('[slug] page')
|
|
|
|
expect(JSON.parse($('#router').text()).asPath).toBe('/an-ssg-path')
|
|
|
|
})
|
|
|
|
|
2022-05-03 22:40:36 +02:00
|
|
|
it('should have correct asPath on dynamic SSG page fallback correctly', async () => {
|
|
|
|
const toCheck = [
|
|
|
|
{
|
|
|
|
pathname: '/fallback-false/first',
|
|
|
|
matchedPath: '/fallback-false/first',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pathname: '/fallback-false/first',
|
|
|
|
matchedPath: `/_next/data/${next.buildId}/fallback-false/first.json`,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
for (const check of toCheck) {
|
|
|
|
console.warn('checking', check)
|
|
|
|
const res = await fetchViaHTTP(appPort, check.pathname, undefined, {
|
|
|
|
headers: {
|
|
|
|
'x-matched-path': check.matchedPath,
|
|
|
|
},
|
|
|
|
redirect: 'manual',
|
|
|
|
})
|
|
|
|
|
|
|
|
const html = await res.text()
|
|
|
|
const $ = cheerio.load(html)
|
|
|
|
expect($('#page').text()).toBe('blog slug')
|
|
|
|
expect($('#asPath').text()).toBe('/fallback-false/first')
|
|
|
|
expect($('#pathname').text()).toBe('/fallback-false/[slug]')
|
|
|
|
expect(JSON.parse($('#query').text())).toEqual({ slug: 'first' })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-02-10 18:48:14 +01:00
|
|
|
it('should copy and read .env file', async () => {
|
|
|
|
const res = await fetchViaHTTP(appPort, '/api/env')
|
|
|
|
|
|
|
|
const envVariables = await res.json()
|
|
|
|
|
|
|
|
expect(envVariables.env).not.toBeUndefined()
|
|
|
|
expect(envVariables.envProd).not.toBeUndefined()
|
|
|
|
expect(envVariables.envLocal).toBeUndefined()
|
|
|
|
})
|
2020-12-16 21:46:55 +01:00
|
|
|
})
|