From e351dbe90df3201bf05985907339545693149767 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Wed, 23 Oct 2019 16:56:17 -0600 Subject: [PATCH 1/5] Ensure directory before flushing cache iSSG file --- packages/next/next-server/server/spr-cache.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/next/next-server/server/spr-cache.ts b/packages/next/next-server/server/spr-cache.ts index 9d3d6eb8d30ae..c2e383e9edfd8 100644 --- a/packages/next/next-server/server/spr-cache.ts +++ b/packages/next/next-server/server/spr-cache.ts @@ -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) @@ -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( getSeedPath(pathname, 'json'), JSON.stringify(data.pageData), From 3967a4702da49e23a654f029c25c428a7a7dc753 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Fri, 25 Oct 2019 12:35:23 -0700 Subject: [PATCH 2/5] Add test for prerender cache flush --- .../prerender/pages/user/[user].js | 30 +++++++++++++++++++ test/integration/prerender/test/index.test.js | 26 ++++++++++++++-- 2 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 test/integration/prerender/pages/user/[user].js diff --git a/test/integration/prerender/pages/user/[user].js b/test/integration/prerender/pages/user/[user].js new file mode 100644 index 0000000000000..cd40d92f5f216 --- /dev/null +++ b/test/integration/prerender/pages/user/[user].js @@ -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 ( + <> +

User: {user}

+ time: {time} + + to home + + + ) +} diff --git a/test/integration/prerender/test/index.test.js b/test/integration/prerender/test/index.test.js index 4f2bd8fb1721f..0a28da17605c7 100644 --- a/test/integration/prerender/test/index.test.js +++ b/test/integration/prerender/test/index.test.js @@ -25,6 +25,7 @@ let appPort let buildId let distPagesDir let exportDir +let stderr const startServer = async (optEnv = {}) => { const scriptPath = join(appDir, 'server.js') @@ -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]': { + dataRoute: `/_next/data/${buildId}/user/[user].json`, + dataRouteRegex: `^\\/_next\\/data\\/${escapedBuildId}\\/user\\/([^\\/]+?)\\.json$`, + routeRegex: '^\\/user\\/([^\\/]+?)(?:\\/)?$' } }) }) @@ -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') + await waitFor(500) + expect(stderr).not.toMatch(/user\/user-1/) + }) } } @@ -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') }) @@ -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') }) From 192c8f3db3cfbfa65e4ba5b22e03ddec2a96c3de Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Fri, 25 Oct 2019 12:40:02 -0700 Subject: [PATCH 3/5] Nest the dynamic route test one more level --- .../prerender/pages/user/{[user].js => [user]/profile.js} | 0 test/integration/prerender/test/index.test.js | 8 ++++---- 2 files changed, 4 insertions(+), 4 deletions(-) rename test/integration/prerender/pages/user/{[user].js => [user]/profile.js} (100%) diff --git a/test/integration/prerender/pages/user/[user].js b/test/integration/prerender/pages/user/[user]/profile.js similarity index 100% rename from test/integration/prerender/pages/user/[user].js rename to test/integration/prerender/pages/user/[user]/profile.js diff --git a/test/integration/prerender/test/index.test.js b/test/integration/prerender/test/index.test.js index 0a28da17605c7..64c8ccab7971b 100644 --- a/test/integration/prerender/test/index.test.js +++ b/test/integration/prerender/test/index.test.js @@ -266,10 +266,10 @@ const runTests = (dev = false) => { dataRouteRegex: `^\\/_next\\/data\\/${escapedBuildId}\\/blog\\/([^\\/]+?)\\/([^\\/]+?)\\.json$`, routeRegex: '^\\/blog\\/([^\\/]+?)\\/([^\\/]+?)(?:\\/)?$' }, - '/user/[user]': { - dataRoute: `/_next/data/${buildId}/user/[user].json`, - dataRouteRegex: `^\\/_next\\/data\\/${escapedBuildId}\\/user\\/([^\\/]+?)\\.json$`, - routeRegex: '^\\/user\\/([^\\/]+?)(?:\\/)?$' + '/user/[user]/profile': { + dataRoute: `/_next/data/${buildId}/user/[user]/profile.json`, + dataRouteRegex: `^\\/_next\\/data\\/${escapedBuildId}\\/user\\/([^\\/]+?)\\/profile\\.json$`, + routeRegex: `^\\/user\\/([^\\/]+?)\\/profile(?:\\/)?$` } }) }) From 4cbfd85d710412f15a44fe7826d48cb519df9436 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Fri, 25 Oct 2019 13:43:37 -0700 Subject: [PATCH 4/5] update fetch for test --- test/integration/prerender/test/index.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/prerender/test/index.test.js b/test/integration/prerender/test/index.test.js index 64c8ccab7971b..5c484d302d05c 100644 --- a/test/integration/prerender/test/index.test.js +++ b/test/integration/prerender/test/index.test.js @@ -359,7 +359,7 @@ const runTests = (dev = false) => { }) it('should not error when flushing cache files', async () => { - await fetchViaHTTP(appPort, '/user/user-1') + await fetchViaHTTP(appPort, '/user/user-1/profile') await waitFor(500) expect(stderr).not.toMatch(/user\/user-1/) }) From abeef2e8fafad4a3091bf695d5cd6a6f25bcd2e2 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Fri, 25 Oct 2019 13:46:03 -0700 Subject: [PATCH 5/5] Update error check --- test/integration/prerender/test/index.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/prerender/test/index.test.js b/test/integration/prerender/test/index.test.js index 5c484d302d05c..824b93a272dfe 100644 --- a/test/integration/prerender/test/index.test.js +++ b/test/integration/prerender/test/index.test.js @@ -361,7 +361,7 @@ const runTests = (dev = false) => { it('should not error when flushing cache files', async () => { await fetchViaHTTP(appPort, '/user/user-1/profile') await waitFor(500) - expect(stderr).not.toMatch(/user\/user-1/) + expect(stderr).not.toMatch(/Failed to update prerender files for/) }) } }