Skip to content

Commit

Permalink
Add catchall test and update routes-manifest field
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Jan 17, 2020
1 parent 40b3e68 commit 7397571
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 30 deletions.
17 changes: 12 additions & 5 deletions packages/next/build/index.ts
Expand Up @@ -530,13 +530,20 @@ export default async function build(dir: string, conf = null): Promise<void> {
routesManifest.serverPropsRoutes = {}

for (const page of serverPropsPages) {
const dataRoute = path.posix.join(
'/_next/data',
buildId,
`${page === '/' ? '/index' : page}.json`
)

routesManifest.serverPropsRoutes[page] = {
page,
dataRoute: path.posix.join(
'/_next/data',
buildId,
`${page === '/' ? '/index' : page}.json`
),
dataRouteRegex: isDynamicRoute(page)
? getRouteRegex(dataRoute.replace(/\.json$/, '')).re.source.replace(
/\(\?:\\\/\)\?\$$/,
'\\.json$'
)
: new RegExp(`^${dataRoute}$`).source,
}
}

Expand Down
32 changes: 32 additions & 0 deletions test/integration/getserverprops/pages/catchall/[...path].js
@@ -0,0 +1,32 @@
import React from 'react'
import Link from 'next/link'
import { useRouter } from 'next/router'

// eslint-disable-next-line camelcase
export async function unstable_getServerProps({ params }) {
return {
world: 'world',
params: params || {},
time: new Date().getTime(),
random: Math.random(),
}
}

export default ({ world, time, params, random }) => {
return (
<>
<p>hello: {world}</p>
<span>time: {time}</span>
<div id="random">{random}</div>
<div id="params">{JSON.stringify(params)}</div>
<div id="query">{JSON.stringify(useRouter().query)}</div>
<Link href="/">
<a id="home">to home</a>
</Link>
<br />
<Link href="/another">
<a id="another">to another</a>
</Link>
</>
)
}
107 changes: 82 additions & 25 deletions test/integration/getserverprops/test/index.test.js
Expand Up @@ -13,6 +13,7 @@ import {
waitFor,
nextBuild,
nextStart,
normalizeRegEx,
} from 'next-test-utils'

jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
Expand All @@ -22,38 +23,66 @@ let app
let appPort
let buildId

const escapeRegex = str => str.replace(/[|\\{}()[\]^$+*?.-]/g, '\\$&')

const expectedManifestRoutes = () => ({
'/user/[user]/profile': {
dataRoute: `/_next/data/${buildId}/user/[user]/profile.json`,
page: '/user/[user]/profile',
'/something': {
page: '/something',
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/something.json$`
),
},
'/blog/[post]': {
page: '/blog/[post]',
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/blog\\/([^/]+?)\\.json$`
),
},
'/': {
dataRoute: `/_next/data/${buildId}/index.json`,
page: '/',
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/index.json$`
),
},
'/blog/[post]/[comment]': {
dataRoute: `/_next/data/${buildId}/blog/[post]/[comment].json`,
page: '/blog/[post]/[comment]',
'/default-revalidate': {
page: '/default-revalidate',
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/default-revalidate.json$`
),
},
'/catchall/[...path]': {
page: '/catchall/[...path]',
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/catchall\\/(.+?)\\.json$`
),
},
'/blog': {
dataRoute: `/_next/data/${buildId}/blog.json`,
page: '/blog',
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/blog.json$`
),
},
'/default-revalidate': {
dataRoute: `/_next/data/${buildId}/default-revalidate.json`,
page: '/default-revalidate',
'/blog/[post]/[comment]': {
page: '/blog/[post]/[comment]',
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(
buildId
)}\\/blog\\/([^/]+?)\\/([^/]+?)\\.json$`
),
},
'/user/[user]/profile': {
page: '/user/[user]/profile',
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(
buildId
)}\\/user\\/([^/]+?)\\/profile\\.json$`
),
},
'/another': {
dataRoute: `/_next/data/${buildId}/another.json`,
page: '/another',
},
'/blog/[post]': {
dataRoute: `/_next/data/${buildId}/blog/[post].json`,
page: '/blog/[post]',
},
'/something': {
dataRoute: `/_next/data/${buildId}/something.json`,
page: '/something',
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/another.json$`
),
},
})

Expand Down Expand Up @@ -169,16 +198,40 @@ const runTests = (dev = false) => {
expect(JSON.parse(query)).toEqual({ hello: 'world', post: 'post-1' })
})

it('should supply params values for catchall correctly', async () => {
const html = await renderViaHTTP(appPort, '/catchall/first')
const $ = cheerio.load(html)
const params = $('#params').text()
expect(JSON.parse(params)).toEqual({ path: ['first'] })
const query = $('#query').text()
expect(JSON.parse(query)).toEqual({ path: ['first'] })

const data = JSON.parse(
await renderViaHTTP(
appPort,
`/_next/data/${escapeRegex(buildId)}/catchall/first.json`
)
)

expect(data.pageProps.params).toEqual({ path: ['first'] })
})

it('should return data correctly', async () => {
const data = JSON.parse(
await renderViaHTTP(appPort, `/_next/data/${buildId}/something.json`)
await renderViaHTTP(
appPort,
`/_next/data/${escapeRegex(buildId)}/something.json`
)
)
expect(data.pageProps.world).toBe('world')
})

it('should return data correctly for dynamic page', async () => {
const data = JSON.parse(
await renderViaHTTP(appPort, `/_next/data/${buildId}/blog/post-1.json`)
await renderViaHTTP(
appPort,
`/_next/data/${escapeRegex(buildId)}/blog/post-1.json`
)
)
expect(data.pageProps.post).toBe('post-1')
})
Expand Down Expand Up @@ -254,17 +307,21 @@ const runTests = (dev = false) => {
})

it('should output routes-manifest correctly', async () => {
const routesManifest = await fs.readJSON(
const { serverPropsRoutes } = await fs.readJSON(
join(appDir, '.next/routes-manifest.json')
)
for (const key of Object.keys(serverPropsRoutes)) {
const val = serverPropsRoutes[key].dataRouteRegex
serverPropsRoutes[key].dataRouteRegex = normalizeRegEx(val)
}

expect(routesManifest.serverPropsRoutes).toEqual(expectedManifestRoutes())
expect(serverPropsRoutes).toEqual(expectedManifestRoutes())
})

it('should set no-cache, no-store, must-revalidate header', async () => {
const res = await fetchViaHTTP(
appPort,
`/_next/data/${buildId}/something.json`
`/_next/data/${escapeRegex(buildId)}/something.json`
)
expect(res.headers.get('cache-control')).toBe(
'no-cache, no-store, must-revalidate'
Expand Down

0 comments on commit 7397571

Please sign in to comment.