Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure directory before flushing cache file #9187

Merged
merged 6 commits into from
Oct 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/next/next-server/server/spr-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { promisify } from 'util'
import { PrerenderManifest } from '../../build'
import { PRERENDER_MANIFEST } from '../lib/constants'
import { normalizePagePath } from './normalize-page-path'
import mkdirpOrig from 'mkdirp'

const mkdirp = promisify(mkdirpOrig)
const readFile = promisify(fs.readFile)
const writeFile = promisify(fs.writeFile)

Expand Down Expand Up @@ -166,7 +168,9 @@ export async function setSprCache(
// `next build` output's manifest.
if (sprOptions.flushToDisk) {
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(
Timer marked this conversation as resolved.
Show resolved Hide resolved
getSeedPath(pathname, 'json'),
JSON.stringify(data.pageData),
Expand Down
30 changes: 30 additions & 0 deletions test/integration/prerender/pages/user/[user]/profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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>
</>
)
}
26 changes: 24 additions & 2 deletions test/integration/prerender/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ let appPort
let buildId
let distPagesDir
let exportDir
let stderr

const startServer = async (optEnv = {}) => {
const scriptPath = join(appDir, 'server.js')
Expand Down Expand Up @@ -264,6 +265,11 @@ const runTests = (dev = false) => {
dataRoute: `/_next/data/${buildId}/blog/[post]/[comment].json`,
dataRouteRegex: `^\\/_next\\/data\\/${escapedBuildId}\\/blog\\/([^\\/]+?)\\/([^\\/]+?)\\.json$`,
routeRegex: '^\\/blog\\/([^\\/]+?)\\/([^\\/]+?)(?:\\/)?$'
},
'/user/[user]/profile': {
dataRoute: `/_next/data/${buildId}/user/[user]/profile.json`,
dataRouteRegex: `^\\/_next\\/data\\/${escapedBuildId}\\/user\\/([^\\/]+?)\\/profile\\.json$`,
routeRegex: `^\\/user\\/([^\\/]+?)\\/profile(?:\\/)?$`
}
})
})
Expand Down Expand Up @@ -351,6 +357,12 @@ const runTests = (dev = false) => {
const val = await browser.eval('window.thisShouldStay')
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/)
})
}
}

Expand All @@ -374,8 +386,13 @@ describe('SPR Prerender', () => {
'utf8'
)
await nextBuild(appDir)
stderr = ''
appPort = await findPort()
app = nextStart(appDir, appPort)
app = nextStart(appDir, appPort, {
onStderr: msg => {
stderr += msg
}
})
distPagesDir = join(appDir, '.next/serverless/pages')
buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8')
})
Expand Down Expand Up @@ -404,8 +421,13 @@ describe('SPR Prerender', () => {
await fs.unlink(nextConfig)
} catch (_) {}
await nextBuild(appDir)
stderr = ''
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')
distPagesDir = join(appDir, '.next/server/static', buildId, 'pages')
})
Expand Down