Commit ac42beae authored by JJ Kasper's avatar JJ Kasper Committed by Joe Haddad

Add buildId to SPR data routes (#8929)

* Add buildId to SPR data routes

* Update buildId replace in serverless loader

* Use new RegExp and add comment

* Apply suggestions from code review
Co-Authored-By: 's avatarJoe Haddad <joe.haddad@zeit.co>

* Test to ensure client transition and handle / data route
parent 5e65161b
...@@ -535,6 +535,7 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -535,6 +535,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
srcRoute: null, srcRoute: null,
dataRoute: path.posix.join( dataRoute: path.posix.join(
'/_next/data', '/_next/data',
buildId,
`${page === '/' ? '/index' : page}.json` `${page === '/' ? '/index' : page}.json`
), ),
} }
...@@ -552,6 +553,7 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -552,6 +553,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
srcRoute: page, srcRoute: page,
dataRoute: path.posix.join( dataRoute: path.posix.join(
'/_next/data', '/_next/data',
buildId,
`${route === '/' ? '/index' : route}.json` `${route === '/' ? '/index' : route}.json`
), ),
} }
...@@ -583,6 +585,7 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -583,6 +585,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
tbdPrerenderRoutes.forEach(tbdRoute => { tbdPrerenderRoutes.forEach(tbdRoute => {
const dataRoute = path.posix.join( const dataRoute = path.posix.join(
'/_next/data', '/_next/data',
buildId,
`${tbdRoute === '/' ? '/index' : tbdRoute}.json` `${tbdRoute === '/' ? '/index' : tbdRoute}.json`
) )
......
...@@ -42,6 +42,7 @@ const nextServerlessLoader: loader.Loader = function() { ...@@ -42,6 +42,7 @@ const nextServerlessLoader: loader.Loader = function() {
/\\/g, /\\/g,
'/' '/'
) )
const escapedBuildId = buildId.replace(/[|\\{}()[\]^$+*?.-]/g, '\\$&')
if (page.match(API_ROUTE)) { if (page.match(API_ROUTE)) {
return ` return `
...@@ -110,7 +111,7 @@ const nextServerlessLoader: loader.Loader = function() { ...@@ -110,7 +111,7 @@ const nextServerlessLoader: loader.Loader = function() {
if (req.url.match(/_next\\/data/)) { if (req.url.match(/_next\\/data/)) {
sprData = true sprData = true
req.url = req.url req.url = req.url
.replace(/\\/_next\\/data\\//, '/') .replace(new RegExp('/_next/data/${escapedBuildId}/'), '/')
.replace(/\\.json$/, '') .replace(/\\.json$/, '')
} }
const parsedUrl = parse(req.url, true) const parsedUrl = parse(req.url, true)
......
...@@ -243,7 +243,9 @@ export default async function( ...@@ -243,7 +243,9 @@ export default async function(
} }
const progress = !options.silent && createProgress(filteredPaths.length) const progress = !options.silent && createProgress(filteredPaths.length)
const sprDataDir = options.buildExport ? outDir : join(outDir, '_next/data') const sprDataDir = options.buildExport
? outDir
: join(outDir, '_next/data', buildId)
const ampValidations: AmpPageStatus = {} const ampValidations: AmpPageStatus = {}
let hadValidationError = false let hadValidationError = false
......
...@@ -623,9 +623,14 @@ export default class Router implements BaseRouter { ...@@ -623,9 +623,14 @@ export default class Router implements BaseRouter {
(Component as any).__NEXT_SPR (Component as any).__NEXT_SPR
) { ) {
let status: any let status: any
const { pathname } = parse(ctx.asPath || ctx.pathname) // pathname should have leading slash
let { pathname } = parse(ctx.asPath || ctx.pathname)
pathname = !pathname || pathname === '/' ? '/index' : pathname
props = await fetch(`/_next/data${pathname}.json`) props = await fetch(
// @ts-ignore __NEXT_DATA__
`/_next/data/${__NEXT_DATA__.buildId}${pathname}.json`
)
.then(res => { .then(res => {
if (!res.ok) { if (!res.ok) {
status = res.status status = res.status
......
...@@ -252,10 +252,24 @@ export default class Server { ...@@ -252,10 +252,24 @@ export default class Server {
{ {
match: route('/_next/data/:path*'), match: route('/_next/data/:path*'),
fn: async (req, res, params, _parsedUrl) => { fn: async (req, res, params, _parsedUrl) => {
// Make sure to 404 for /_next/data/ itself // Make sure to 404 for /_next/data/ itself and
if (!params.path) return this.render404(req, res, _parsedUrl) // we also want to 404 if the buildId isn't correct
// TODO: force `.json` to be present if (!params.path || params.path[0] !== this.buildId) {
const pathname = `/${params.path.join('/')}`.replace(/\.json$/, '') return this.render404(req, res, _parsedUrl)
}
// remove buildId from URL
params.path.shift()
// show 404 if it doesn't end with .json
if (!params.path[params.path.length - 1].endsWith('.json')) {
return this.render404(req, res, _parsedUrl)
}
// re-create page's pathname
const pathname = `/${params.path.join('/')}`
.replace(/\.json$/, '')
.replace(/\/index$/, '/')
req.url = pathname req.url = pathname
const parsedUrl = parseUrl(pathname, true) const parsedUrl = parseUrl(pathname, true)
await this.render( await this.render(
...@@ -601,8 +615,9 @@ export default class Server { ...@@ -601,8 +615,9 @@ export default class Server {
// Serverless requests need its URL transformed back into the original // Serverless requests need its URL transformed back into the original
// request path (to emulate lambda behavior in production) // request path (to emulate lambda behavior in production)
if (isLikeServerless && isSprData) { if (isLikeServerless && isSprData) {
const curUrl = parseUrl(req.url || '', true) let { pathname } = parseUrl(req.url || '', true)
req.url = `/_next/data${curUrl.pathname}.json` pathname = !pathname || pathname === '/' ? '/index' : pathname
req.url = `/_next/data/${this.buildId}${pathname}.json`
} }
const doRender = withCoalescedInvoke(async function(): Promise<{ const doRender = withCoalescedInvoke(async function(): Promise<{
......
...@@ -20,9 +20,58 @@ const appDir = join(__dirname, '..') ...@@ -20,9 +20,58 @@ const appDir = join(__dirname, '..')
const nextConfig = join(appDir, 'next.config.js') const nextConfig = join(appDir, 'next.config.js')
let app let app
let appPort let appPort
let buildId
let distPagesDir let distPagesDir
let exportDir let exportDir
const expectedManifestRoutes = () => ({
'/': {
dataRoute: `/_next/data/${buildId}/index.json`,
initialRevalidateSeconds: 1,
srcRoute: null
},
'/blog/[post3]': {
dataRoute: `/_next/data/${buildId}/blog/[post3].json`,
initialRevalidateSeconds: 10,
srcRoute: '/blog/[post]'
},
'/blog/post-1': {
dataRoute: `/_next/data/${buildId}/blog/post-1.json`,
initialRevalidateSeconds: 10,
srcRoute: '/blog/[post]'
},
'/blog/post-2': {
dataRoute: `/_next/data/${buildId}/blog/post-2.json`,
initialRevalidateSeconds: 10,
srcRoute: '/blog/[post]'
},
'/blog/post-1/comment-1': {
dataRoute: `/_next/data/${buildId}/blog/post-1/comment-1.json`,
initialRevalidateSeconds: 2,
srcRoute: '/blog/[post]/[comment]'
},
'/blog/post-2/comment-2': {
dataRoute: `/_next/data/${buildId}/blog/post-2/comment-2.json`,
initialRevalidateSeconds: 2,
srcRoute: '/blog/[post]/[comment]'
},
'/another': {
dataRoute: `/_next/data/${buildId}/another.json`,
initialRevalidateSeconds: 0,
srcRoute: null
},
'/default-revalidate': {
dataRoute: `/_next/data/${buildId}/default-revalidate.json`,
initialRevalidateSeconds: 1,
srcRoute: null
},
'/something': {
dataRoute: `/_next/data/${buildId}/something.json`,
initialRevalidateSeconds: false,
srcRoute: null
}
})
const navigateTest = () => { const navigateTest = () => {
it('should navigate between pages successfully', async () => { it('should navigate between pages successfully', async () => {
const browser = await webdriver(appPort, '/') const browser = await webdriver(appPort, '/')
...@@ -36,16 +85,19 @@ const navigateTest = () => { ...@@ -36,16 +85,19 @@ const navigateTest = () => {
expect(text).toMatch(/hello.*?world/) expect(text).toMatch(/hello.*?world/)
// go to / // go to /
await browser.eval('window.didTransition = 1')
await browser.elementByCss('#home').click() await browser.elementByCss('#home').click()
await browser.waitForElementByCss('#another') await browser.waitForElementByCss('#another')
text = await browser.elementByCss('p').text() text = await browser.elementByCss('p').text()
expect(text).toMatch(/hello.*?world/) expect(text).toMatch(/hello.*?world/)
expect(await browser.eval('window.didTransition')).toBe(1)
// go to /something // go to /something
await browser.elementByCss('#something').click() await browser.elementByCss('#something').click()
await browser.waitForElementByCss('#home') await browser.waitForElementByCss('#home')
text = await browser.elementByCss('p').text() text = await browser.elementByCss('p').text()
expect(text).toMatch(/hello.*?world/) expect(text).toMatch(/hello.*?world/)
expect(await browser.eval('window.didTransition')).toBe(1)
// go to / // go to /
await browser.elementByCss('#home').click() await browser.elementByCss('#home').click()
...@@ -56,6 +108,7 @@ const navigateTest = () => { ...@@ -56,6 +108,7 @@ const navigateTest = () => {
await browser.waitForElementByCss('#home') await browser.waitForElementByCss('#home')
text = await browser.elementByCss('p').text() text = await browser.elementByCss('p').text()
expect(text).toMatch(/Post:.*?post-1/) expect(text).toMatch(/Post:.*?post-1/)
expect(await browser.eval('window.didTransition')).toBe(1)
// go to / // go to /
await browser.elementByCss('#home').click() await browser.elementByCss('#home').click()
...@@ -66,6 +119,7 @@ const navigateTest = () => { ...@@ -66,6 +119,7 @@ const navigateTest = () => {
await browser.waitForElementByCss('#home') await browser.waitForElementByCss('#home')
text = await browser.elementByCss('p:nth-child(2)').text() text = await browser.elementByCss('p:nth-child(2)').text()
expect(text).toMatch(/Comment:.*?comment-1/) expect(text).toMatch(/Comment:.*?comment-1/)
expect(await browser.eval('window.didTransition')).toBe(1)
await browser.close() await browser.close()
}) })
...@@ -86,21 +140,33 @@ const runTests = (dev = false) => { ...@@ -86,21 +140,33 @@ 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/something.json') await renderViaHTTP(
appPort,
expectedManifestRoutes()['/something'].dataRoute
)
) )
expect(data.pageProps.world).toBe('world') expect(data.pageProps.world).toBe('world')
}) })
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/blog/post-1.json') await renderViaHTTP(
appPort,
expectedManifestRoutes()['/blog/post-1'].dataRoute
)
) )
expect(data.pageProps.post).toBe('post-1') expect(data.pageProps.post).toBe('post-1')
}) })
it('should return data correctly for dynamic page (non-seeded)', async () => { it('should return data correctly for dynamic page (non-seeded)', async () => {
const data = JSON.parse( const data = JSON.parse(
await renderViaHTTP(appPort, '/_next/data/blog/post-3.json') await renderViaHTTP(
appPort,
expectedManifestRoutes()['/blog/post-1'].dataRoute.replace(
/post-1/,
'post-3'
)
)
) )
expect(data.pageProps.post).toBe('post-3') expect(data.pageProps.post).toBe('post-3')
}) })
...@@ -147,66 +213,22 @@ const runTests = (dev = false) => { ...@@ -147,66 +213,22 @@ const runTests = (dev = false) => {
}) })
} else { } else {
it('outputs a prerender-manifest correctly', async () => { it('outputs a prerender-manifest correctly', async () => {
const manifest = require(join(appDir, '.next', 'prerender-manifest.json')) const manifest = JSON.parse(
await fs.readFile(join(appDir, '.next/prerender-manifest.json'), 'utf8')
)
const escapedBuildId = buildId.replace(/[|\\{}()[\]^$+*?.-]/g, '\\$&')
expect(manifest.version).toBe(1) expect(manifest.version).toBe(1)
expect(manifest.routes).toEqual({ expect(manifest.routes).toEqual(expectedManifestRoutes())
'/': {
dataRoute: '/_next/data/index.json',
initialRevalidateSeconds: 1,
srcRoute: null
},
'/blog/[post3]': {
dataRoute: '/_next/data/blog/[post3].json',
initialRevalidateSeconds: 10,
srcRoute: '/blog/[post]'
},
'/blog/post-1': {
dataRoute: '/_next/data/blog/post-1.json',
initialRevalidateSeconds: 10,
srcRoute: '/blog/[post]'
},
'/blog/post-2': {
dataRoute: '/_next/data/blog/post-2.json',
initialRevalidateSeconds: 10,
srcRoute: '/blog/[post]'
},
'/blog/post-1/comment-1': {
dataRoute: '/_next/data/blog/post-1/comment-1.json',
initialRevalidateSeconds: 2,
srcRoute: '/blog/[post]/[comment]'
},
'/blog/post-2/comment-2': {
dataRoute: '/_next/data/blog/post-2/comment-2.json',
initialRevalidateSeconds: 2,
srcRoute: '/blog/[post]/[comment]'
},
'/another': {
dataRoute: '/_next/data/another.json',
initialRevalidateSeconds: 0,
srcRoute: null
},
'/default-revalidate': {
dataRoute: '/_next/data/default-revalidate.json',
initialRevalidateSeconds: 1,
srcRoute: null
},
'/something': {
dataRoute: '/_next/data/something.json',
initialRevalidateSeconds: false,
srcRoute: null
}
})
expect(manifest.dynamicRoutes).toEqual({ expect(manifest.dynamicRoutes).toEqual({
'/blog/[post]': { '/blog/[post]': {
dataRoute: '/_next/data/blog/[post].json', dataRoute: `/_next/data/${buildId}/blog/[post].json`,
dataRouteRegex: '^\\/_next\\/data\\/blog\\/([^\\/]+?)\\.json$', dataRouteRegex: `^\\/_next\\/data\\/${escapedBuildId}\\/blog\\/([^\\/]+?)\\.json$`,
routeRegex: '^\\/blog\\/([^\\/]+?)(?:\\/)?$' routeRegex: '^\\/blog\\/([^\\/]+?)(?:\\/)?$'
}, },
'/blog/[post]/[comment]': { '/blog/[post]/[comment]': {
dataRoute: '/_next/data/blog/[post]/[comment].json', dataRoute: `/_next/data/${buildId}/blog/[post]/[comment].json`,
dataRouteRegex: dataRouteRegex: `^\\/_next\\/data\\/${escapedBuildId}\\/blog\\/([^\\/]+?)\\/([^\\/]+?)\\.json$`,
'^\\/_next\\/data\\/blog\\/([^\\/]+?)\\/([^\\/]+?)\\.json$',
routeRegex: '^\\/blog\\/([^\\/]+?)\\/([^\\/]+?)(?:\\/)?$' routeRegex: '^\\/blog\\/([^\\/]+?)\\/([^\\/]+?)(?:\\/)?$'
} }
}) })
...@@ -254,7 +276,7 @@ const runTests = (dev = false) => { ...@@ -254,7 +276,7 @@ const runTests = (dev = false) => {
}) })
it('should handle revalidating JSON correctly', async () => { it('should handle revalidating JSON correctly', async () => {
const route = '/_next/data/blog/post-2/comment-3' const route = `/_next/data/${buildId}/blog/post-2/comment-3.json`
const initialJson = await renderViaHTTP(appPort, route) const initialJson = await renderViaHTTP(appPort, route)
expect(initialJson).toMatch(/post-2/) expect(initialJson).toMatch(/post-2/)
expect(initialJson).toMatch(/comment-3/) expect(initialJson).toMatch(/comment-3/)
...@@ -276,6 +298,7 @@ describe('SPR Prerender', () => { ...@@ -276,6 +298,7 @@ describe('SPR Prerender', () => {
beforeAll(async () => { beforeAll(async () => {
appPort = await findPort() appPort = await findPort()
app = await launchApp(appDir, appPort) app = await launchApp(appDir, appPort)
buildId = 'development'
}) })
afterAll(() => killApp(app)) afterAll(() => killApp(app))
...@@ -293,6 +316,7 @@ describe('SPR Prerender', () => { ...@@ -293,6 +316,7 @@ describe('SPR Prerender', () => {
appPort = await findPort() appPort = await findPort()
app = await nextStart(appDir, appPort) app = await nextStart(appDir, appPort)
distPagesDir = join(appDir, '.next/serverless/pages') distPagesDir = join(appDir, '.next/serverless/pages')
buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8')
}) })
afterAll(() => killApp(app)) afterAll(() => killApp(app))
...@@ -307,7 +331,7 @@ describe('SPR Prerender', () => { ...@@ -307,7 +331,7 @@ describe('SPR Prerender', () => {
await nextBuild(appDir) await nextBuild(appDir)
appPort = await findPort() appPort = await findPort()
app = await nextStart(appDir, appPort) app = await nextStart(appDir, appPort)
const buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8') buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8')
distPagesDir = join(appDir, '.next/server/static', buildId, 'pages') distPagesDir = join(appDir, '.next/server/static', buildId, 'pages')
}) })
afterAll(() => killApp(app)) afterAll(() => killApp(app))
...@@ -322,6 +346,7 @@ describe('SPR Prerender', () => { ...@@ -322,6 +346,7 @@ describe('SPR Prerender', () => {
await nextExport(appDir, { outdir: exportDir }) await nextExport(appDir, { outdir: exportDir })
app = await startStaticServer(exportDir) app = await startStaticServer(exportDir)
appPort = app.address().port appPort = app.address().port
buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8')
}) })
afterAll(() => killApp(app)) afterAll(() => killApp(app))
...@@ -335,7 +360,7 @@ describe('SPR Prerender', () => { ...@@ -335,7 +360,7 @@ describe('SPR Prerender', () => {
for (const route of routes) { for (const route of routes) {
await fs.access(join(exportDir, `${route}.html`)) await fs.access(join(exportDir, `${route}.html`))
await fs.access(join(exportDir, '_next/data', `${route}.json`)) await fs.access(join(exportDir, '_next/data', buildId, `${route}.json`))
} }
}) })
......
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