Unverified Commit 6dea45bd authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Add dataRoutes field to routes-manifest for SSG and serverProps routes (#10622)

* Add dataRoutes field to routes-manifest for SSG and serverProps routes

* Update routes-manifest test
parent 395714a4
...@@ -569,12 +569,13 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -569,12 +569,13 @@ export default async function build(dir: string, conf = null): Promise<void> {
) )
staticCheckWorkers.end() staticCheckWorkers.end()
if (serverPropsPages.size > 0) { if (serverPropsPages.size > 0 || ssgPages.size > 0) {
// We update the routes manifest after the build with the // We update the routes manifest after the build with the
// serverProps routes since we can't determine this until after build // data routes since we can't determine these until after build
routesManifest.serverPropsRoutes = {} routesManifest.dataRoutes = getSortedRoutes([
...serverPropsPages,
for (const page of serverPropsPages) { ...ssgPages,
]).map(page => {
const pagePath = normalizePagePath(page) const pagePath = normalizePagePath(page)
const dataRoute = path.posix.join( const dataRoute = path.posix.join(
'/_next/data', '/_next/data',
...@@ -582,7 +583,7 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -582,7 +583,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
`${pagePath}.json` `${pagePath}.json`
) )
routesManifest.serverPropsRoutes[page] = { return {
page, page,
dataRouteRegex: isDynamicRoute(page) dataRouteRegex: isDynamicRoute(page)
? getRouteRegex(dataRoute.replace(/\.json$/, '')).re.source.replace( ? getRouteRegex(dataRoute.replace(/\.json$/, '')).re.source.replace(
...@@ -597,7 +598,7 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -597,7 +598,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
)}$` )}$`
).source, ).source,
} }
} })
await fsWriteFile( await fsWriteFile(
routesManifestPath, routesManifestPath,
......
...@@ -15,6 +15,7 @@ import { ...@@ -15,6 +15,7 @@ import {
normalizeRegEx, normalizeRegEx,
} from 'next-test-utils' } from 'next-test-utils'
import cheerio from 'cheerio' import cheerio from 'cheerio'
import escapeRegex from 'escape-string-regexp'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2 jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
...@@ -485,6 +486,10 @@ function runTests(dev) { ...@@ -485,6 +486,10 @@ function runTests(dev) {
route.regex = normalizeRegEx(route.regex) route.regex = normalizeRegEx(route.regex)
} }
for (const route of manifest.dataRoutes) {
route.dataRouteRegex = normalizeRegEx(route.dataRouteRegex)
}
expect(manifest).toEqual({ expect(manifest).toEqual({
version: 1, version: 1,
pages404: true, pages404: true,
...@@ -492,6 +497,32 @@ function runTests(dev) { ...@@ -492,6 +497,32 @@ function runTests(dev) {
headers: [], headers: [],
rewrites: [], rewrites: [],
redirects: [], redirects: [],
dataRoutes: [
{
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(
buildId
)}\\/p1\\/p2\\/all\\-ssg\\/(.+?)\\.json$`
),
page: '/p1/p2/all-ssg/[...rest]',
},
{
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(
buildId
)}\\/p1\\/p2\\/nested\\-all\\-ssg\\/(.+?)\\.json$`
),
page: '/p1/p2/nested-all-ssg/[...rest]',
},
{
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(
buildId
)}\\/p1\\/p2\\/predefined\\-ssg\\/(.+?)\\.json$`
),
page: '/p1/p2/predefined-ssg/[...rest]',
},
],
dynamicRoutes: [ dynamicRoutes: [
{ {
page: '/blog/[name]/comment/[id]', page: '/blog/[name]/comment/[id]',
......
...@@ -25,72 +25,72 @@ let appPort ...@@ -25,72 +25,72 @@ let appPort
let buildId let buildId
let stderr let stderr
const expectedManifestRoutes = () => ({ const expectedManifestRoutes = () => [
'/something': { {
page: '/something',
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/something.json$` `^\\/_next\\/data\\/${escapeRegex(buildId)}\\/index.json$`
), ),
page: '/',
}, },
'/blog/[post]': { {
page: '/blog/[post]',
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/blog\\/([^/]+?)\\.json$` `^\\/_next\\/data\\/${escapeRegex(buildId)}\\/another.json$`
), ),
page: '/another',
}, },
'/': { {
page: '/',
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/index.json$` `^\\/_next\\/data\\/${escapeRegex(buildId)}\\/blog.json$`
), ),
page: '/blog',
}, },
'/default-revalidate': { {
page: '/default-revalidate',
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/default-revalidate.json$` `^\\/_next\\/data\\/${escapeRegex(buildId)}\\/blog\\/([^\\/]+?)\\.json$`
), ),
page: '/blog/[post]',
}, },
'/catchall/[...path]': { {
page: '/catchall/[...path]',
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/catchall\\/(.+?)\\.json$` `^\\/_next\\/data\\/${escapeRegex(
buildId
)}\\/blog\\/([^\\/]+?)\\/([^\\/]+?)\\.json$`
), ),
page: '/blog/[post]/[comment]',
}, },
'/blog': { {
page: '/blog',
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/blog.json$` `^\\/_next\\/data\\/${escapeRegex(buildId)}\\/catchall\\/(.+?)\\.json$`
), ),
page: '/catchall/[...path]',
}, },
'/blog/[post]/[comment]': { {
page: '/blog/[post]/[comment]',
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex( `^\\/_next\\/data\\/${escapeRegex(buildId)}\\/default-revalidate.json$`
buildId
)}\\/blog\\/([^/]+?)\\/([^/]+?)\\.json$`
), ),
page: '/default-revalidate',
}, },
'/user/[user]/profile': { {
page: '/user/[user]/profile',
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex( `^\\/_next\\/data\\/${escapeRegex(buildId)}\\/invalid-keys.json$`
buildId
)}\\/user\\/([^/]+?)\\/profile\\.json$`
), ),
page: '/invalid-keys',
}, },
'/another': { {
page: '/another',
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/another.json$` `^\\/_next\\/data\\/${escapeRegex(buildId)}\\/something.json$`
), ),
page: '/something',
}, },
'/invalid-keys': { {
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/invalid-keys.json$` `^\\/_next\\/data\\/${escapeRegex(
buildId
)}\\/user\\/([^\\/]+?)\\/profile\\.json$`
), ),
page: '/invalid-keys', page: '/user/[user]/profile',
}, },
}) ]
const navigateTest = (dev = false) => { const navigateTest = (dev = false) => {
it('should navigate between pages successfully', async () => { it('should navigate between pages successfully', async () => {
...@@ -209,7 +209,10 @@ const runTests = (dev = false) => { ...@@ -209,7 +209,10 @@ const runTests = (dev = false) => {
expect(JSON.parse(query)).toEqual({ path: ['first'] }) expect(JSON.parse(query)).toEqual({ path: ['first'] })
const data = JSON.parse( const data = JSON.parse(
await renderViaHTTP(appPort, `/_next/data/${buildId}/catchall/first.json`) await renderViaHTTP(
appPort,
`/_next/data/${escapeRegex(buildId)}/catchall/first.json`
)
) )
expect(data.pageProps.params).toEqual({ path: ['first'] }) expect(data.pageProps.params).toEqual({ path: ['first'] })
...@@ -217,7 +220,10 @@ const runTests = (dev = false) => { ...@@ -217,7 +220,10 @@ const runTests = (dev = false) => {
it('should return data correctly', async () => { it('should return data correctly', async () => {
const data = JSON.parse( 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') expect(data.pageProps.world).toBe('world')
}) })
...@@ -226,7 +232,7 @@ const runTests = (dev = false) => { ...@@ -226,7 +232,7 @@ const runTests = (dev = false) => {
const data = JSON.parse( const data = JSON.parse(
await renderViaHTTP( await renderViaHTTP(
appPort, appPort,
`/_next/data/${buildId}/something.json?another=thing` `/_next/data/${escapeRegex(buildId)}/something.json?another=thing`
) )
) )
expect(data.pageProps.query.another).toBe('thing') expect(data.pageProps.query.another).toBe('thing')
...@@ -234,7 +240,10 @@ const runTests = (dev = false) => { ...@@ -234,7 +240,10 @@ const runTests = (dev = false) => {
it('should return data correctly for dynamic page', async () => { it('should return data correctly for dynamic page', async () => {
const data = JSON.parse( 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') expect(data.pageProps.post).toBe('post-1')
}) })
...@@ -367,15 +376,14 @@ const runTests = (dev = false) => { ...@@ -367,15 +376,14 @@ const runTests = (dev = false) => {
}) })
it('should output routes-manifest correctly', async () => { it('should output routes-manifest correctly', async () => {
const { serverPropsRoutes } = await fs.readJSON( const { dataRoutes } = await fs.readJSON(
join(appDir, '.next/routes-manifest.json') join(appDir, '.next/routes-manifest.json')
) )
for (const key of Object.keys(serverPropsRoutes)) { for (const route of dataRoutes) {
const val = serverPropsRoutes[key].dataRouteRegex route.dataRouteRegex = normalizeRegEx(route.dataRouteRegex)
serverPropsRoutes[key].dataRouteRegex = normalizeRegEx(val)
} }
expect(serverPropsRoutes).toEqual(expectedManifestRoutes()) expect(dataRoutes).toEqual(expectedManifestRoutes())
}) })
it('should set no-cache, no-store, must-revalidate header', async () => { it('should set no-cache, no-store, must-revalidate header', async () => {
......
...@@ -589,6 +589,83 @@ const runTests = (dev = false, looseMode = false) => { ...@@ -589,6 +589,83 @@ const runTests = (dev = false, looseMode = false) => {
}) })
} }
it('outputs dataRoutes in routes-manifest correctly', async () => {
const { dataRoutes } = JSON.parse(
await fs.readFile(join(appDir, '.next/routes-manifest.json'), 'utf8')
)
for (const route of dataRoutes) {
route.dataRouteRegex = normalizeRegEx(route.dataRouteRegex)
}
expect(dataRoutes).toEqual([
{
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/index.json$`
),
page: '/',
},
{
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/another.json$`
),
page: '/another',
},
{
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/blog.json$`
),
page: '/blog',
},
{
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(
buildId
)}\\/blog\\/([^\\/]+?)\\.json$`
),
page: '/blog/[post]',
},
{
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(
buildId
)}\\/blog\\/([^\\/]+?)\\/([^\\/]+?)\\.json$`
),
page: '/blog/[post]/[comment]',
},
{
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(
buildId
)}\\/catchall\\/(.+?)\\.json$`
),
page: '/catchall/[...slug]',
},
{
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(
buildId
)}\\/default-revalidate.json$`
),
page: '/default-revalidate',
},
{
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/something.json$`
),
page: '/something',
},
{
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(
buildId
)}\\/user\\/([^\\/]+?)\\/profile\\.json$`
),
page: '/user/[user]/profile',
},
])
})
it('outputs a prerender-manifest correctly', async () => { it('outputs a prerender-manifest correctly', async () => {
const manifest = JSON.parse( const manifest = JSON.parse(
await fs.readFile(join(appDir, '.next/prerender-manifest.json'), 'utf8') await fs.readFile(join(appDir, '.next/prerender-manifest.json'), 'utf8')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment