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

Ensure directory before flushing cache file (#9187)

* Ensure directory before flushing cache iSSG file

* Add test for prerender cache flush

* Nest the dynamic route test one more level

* update fetch for test

* Update error check
parent a964dc82
...@@ -5,7 +5,9 @@ import { promisify } from 'util' ...@@ -5,7 +5,9 @@ import { promisify } from 'util'
import { PrerenderManifest } from '../../build' import { PrerenderManifest } from '../../build'
import { PRERENDER_MANIFEST } from '../lib/constants' import { PRERENDER_MANIFEST } from '../lib/constants'
import { normalizePagePath } from './normalize-page-path' import { normalizePagePath } from './normalize-page-path'
import mkdirpOrig from 'mkdirp'
const mkdirp = promisify(mkdirpOrig)
const readFile = promisify(fs.readFile) const readFile = promisify(fs.readFile)
const writeFile = promisify(fs.writeFile) const writeFile = promisify(fs.writeFile)
...@@ -166,7 +168,9 @@ export async function setSprCache( ...@@ -166,7 +168,9 @@ export async function setSprCache(
// `next build` output's manifest. // `next build` output's manifest.
if (sprOptions.flushToDisk) { if (sprOptions.flushToDisk) {
try { try {
await writeFile(getSeedPath(pathname, 'html'), data.html, 'utf8') const seedPath = getSeedPath(pathname, 'html')
await mkdirp(path.dirname(seedPath))
await writeFile(seedPath, data.html, 'utf8')
await writeFile( await writeFile(
getSeedPath(pathname, 'json'), getSeedPath(pathname, 'json'),
JSON.stringify(data.pageData), JSON.stringify(data.pageData),
......
import React from 'react'
import Link from 'next/link'
// eslint-disable-next-line camelcase
export async function unstable_getStaticParams () {
return []
}
// eslint-disable-next-line camelcase
export async function unstable_getStaticProps ({ params }) {
return {
props: {
user: params.user,
time: (await import('perf_hooks')).performance.now()
},
revalidate: 10
}
}
export default ({ user, time }) => {
return (
<>
<p>User: {user}</p>
<span>time: {time}</span>
<Link href='/'>
<a id='home'>to home</a>
</Link>
</>
)
}
...@@ -25,6 +25,7 @@ let appPort ...@@ -25,6 +25,7 @@ let appPort
let buildId let buildId
let distPagesDir let distPagesDir
let exportDir let exportDir
let stderr
const startServer = async (optEnv = {}) => { const startServer = async (optEnv = {}) => {
const scriptPath = join(appDir, 'server.js') const scriptPath = join(appDir, 'server.js')
...@@ -264,6 +265,11 @@ const runTests = (dev = false) => { ...@@ -264,6 +265,11 @@ const runTests = (dev = false) => {
dataRoute: `/_next/data/${buildId}/blog/[post]/[comment].json`, dataRoute: `/_next/data/${buildId}/blog/[post]/[comment].json`,
dataRouteRegex: `^\\/_next\\/data\\/${escapedBuildId}\\/blog\\/([^\\/]+?)\\/([^\\/]+?)\\.json$`, dataRouteRegex: `^\\/_next\\/data\\/${escapedBuildId}\\/blog\\/([^\\/]+?)\\/([^\\/]+?)\\.json$`,
routeRegex: '^\\/blog\\/([^\\/]+?)\\/([^\\/]+?)(?:\\/)?$' routeRegex: '^\\/blog\\/([^\\/]+?)\\/([^\\/]+?)(?:\\/)?$'
},
'/user/[user]/profile': {
dataRoute: `/_next/data/${buildId}/user/[user]/profile.json`,
dataRouteRegex: `^\\/_next\\/data\\/${escapedBuildId}\\/user\\/([^\\/]+?)\\/profile\\.json$`,
routeRegex: `^\\/user\\/([^\\/]+?)\\/profile(?:\\/)?$`
} }
}) })
}) })
...@@ -351,6 +357,12 @@ const runTests = (dev = false) => { ...@@ -351,6 +357,12 @@ const runTests = (dev = false) => {
const val = await browser.eval('window.thisShouldStay') const val = await browser.eval('window.thisShouldStay')
expect(val).toBe(true) expect(val).toBe(true)
}) })
it('should not error when flushing cache files', async () => {
await fetchViaHTTP(appPort, '/user/user-1/profile')
await waitFor(500)
expect(stderr).not.toMatch(/Failed to update prerender files for/)
})
} }
} }
...@@ -374,8 +386,13 @@ describe('SPR Prerender', () => { ...@@ -374,8 +386,13 @@ describe('SPR Prerender', () => {
'utf8' 'utf8'
) )
await nextBuild(appDir) await nextBuild(appDir)
stderr = ''
appPort = await findPort() appPort = await findPort()
app = nextStart(appDir, appPort) app = nextStart(appDir, appPort, {
onStderr: msg => {
stderr += msg
}
})
distPagesDir = join(appDir, '.next/serverless/pages') distPagesDir = join(appDir, '.next/serverless/pages')
buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8') buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8')
}) })
...@@ -404,8 +421,13 @@ describe('SPR Prerender', () => { ...@@ -404,8 +421,13 @@ describe('SPR Prerender', () => {
await fs.unlink(nextConfig) await fs.unlink(nextConfig)
} catch (_) {} } catch (_) {}
await nextBuild(appDir) await nextBuild(appDir)
stderr = ''
appPort = await findPort() appPort = await findPort()
app = await nextStart(appDir, appPort) app = await nextStart(appDir, appPort, {
onStderr: msg => {
stderr += msg
}
})
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')
}) })
......
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