From 261eb16308a0b89687626ba97bc26c8caaf93669 Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Thu, 6 Dec 2018 16:46:53 +0100 Subject: [PATCH] Only check if BUILD_ID exists when reading throws error (#5834) We don't have to check if the file already exists here, since it's always in production mode (dev overrides the readBuildId method to always be `development`) If the file is not found (error is thrown) we check if the file exists. If not we throw a helpful error. In other cases we throw the original error. --- packages/next-server/server/next-server.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/next-server/server/next-server.js b/packages/next-server/server/next-server.js index 10b62a04d2..bd86be3c7e 100644 --- a/packages/next-server/server/next-server.js +++ b/packages/next-server/server/next-server.js @@ -253,9 +253,15 @@ export default class Server { } readBuildId () { - if (!fs.existsSync(resolve(this.distDir, BUILD_ID_FILE))) { - throw new Error(`Could not find a valid build in the '${this.distDir}' directory! Try building your app with 'next build' before starting the server.`) + const buildIdFile = join(this.distDir, BUILD_ID_FILE) + try { + return fs.readFileSync(buildIdFile, 'utf8').trim() + } catch (err) { + if (!fs.existsSync(buildIdFile)) { + throw new Error(`Could not find a valid build in the '${this.distDir}' directory! Try building your app with 'next build' before starting the server.`) + } + + throw err } - return fs.readFileSync(join(this.distDir, BUILD_ID_FILE), 'utf8').trim() } } -- GitLab