From f84ba20d4e1cecf533cb56a674c879d998c380ee Mon Sep 17 00:00:00 2001 From: Ross Zurowski Date: Mon, 9 Oct 2017 09:36:27 -0700 Subject: [PATCH] Use redirect page as error page A [recent change](https://github.com/zeit/next.js/pull/2973) in Next allowed for paths to be exported directly, meaning we can now generate 404.html straight from the config. This changes the build to use the redirect page to generate 404.html directly. --- next.config.js | 7 +++++-- package.json | 6 +++--- pages/_error.js | 17 ----------------- pages/_redirect.js | 16 ++++++++++------ 4 files changed, 18 insertions(+), 28 deletions(-) delete mode 100644 pages/_error.js diff --git a/next.config.js b/next.config.js index 43ab9fc..8838af0 100644 --- a/next.config.js +++ b/next.config.js @@ -10,12 +10,15 @@ const getLogPaths = async () => { module.exports = { async exportPathMap() { - const staticPaths = ['/', '/2017/japan', '/100', '/_error']; + const staticPaths = ['/', '/2017/japan', '/100']; + const customPaths = { + '/404.html': { page: '_redirect' }, + }; const logPaths = await getLogPaths(); const paths = staticPaths.concat(logPaths); - const pathMap = pathsToPages(paths); + const pathMap = { ...customPaths, ...pathsToPages(paths) }; return pathMap; }, diff --git a/package.json b/package.json index 8a2226b..85e3869 100644 --- a/package.json +++ b/package.json @@ -5,10 +5,10 @@ "scripts": { "prebuild": "yarn build:css", "build": "NODE_ENV=production next build && next export -o dist", - "postbuild": "yarn build:404 && yarn build:dat", - "build:404": "mv dist/_error/index.html dist/404.html && rm -r dist/_error", + "postbuild": "yarn build:dat", "build:css": "mkdir -p static/css/ && node css.js > static/css/index.css", - "build:dat": "cp dat.json dist/dat.json && mkdir -p dist/.well-known && touch dist/.well-known/dat && echo $(node -p 'require(\"./dat.json\").url') > dist/.well-known/dat && echo \"TTL=0\" >> dist/.well-known/dat", + "build:dat": + "cp dat.json dist/dat.json && mkdir -p dist/.well-known && touch dist/.well-known/dat && echo $(node -p 'require(\"./dat.json\").url') > dist/.well-known/dat && echo \"TTL=0\" >> dist/.well-known/dat", "start": "next" }, "dependencies": { diff --git a/pages/_error.js b/pages/_error.js deleted file mode 100644 index d878bc6..0000000 --- a/pages/_error.js +++ /dev/null @@ -1,17 +0,0 @@ -// @flow - -import React from 'react'; -import Head from 'next/head'; - -import Page from 'components/layouts/page'; - -const NOT_DEV = process.env.NODE_ENV !== 'development'; - -export default () => ( - - {NOT_DEV && } -
-

Just a sec, redirecting you...

-
-
-); diff --git a/pages/_redirect.js b/pages/_redirect.js index 2989ca9..06ccf1a 100644 --- a/pages/_redirect.js +++ b/pages/_redirect.js @@ -20,12 +20,16 @@ export default class Redirect extends Component { return ( {IS_PRODUCTION && } -
-

Just a sec, redirecting you...

- {!IS_PRODUCTION && ( -

- No redirects in dev. Go here -

+
+ {IS_PRODUCTION ? ( +

Just a sec, redirecting you...

+ ) : ( +
+

Redirect page disabled in dev.

+

+ Go here → +

+
)}