From 82c912237b629fa590fdcc078f516ee207c1b938 Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Thu, 24 Aug 2017 18:19:31 +0200 Subject: [PATCH] fix hard links for new client (#8986) --- website/server/controllers/top-level/pages.js | 3 +- website/server/libs/client.js | 5 ++++ website/server/middlewares/notFound.js | 29 ++++++++++++++++++- website/server/middlewares/static.js | 2 +- 4 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 website/server/libs/client.js diff --git a/website/server/controllers/top-level/pages.js b/website/server/controllers/top-level/pages.js index b8e770d3d6..afdb9ebf0f 100644 --- a/website/server/controllers/top-level/pages.js +++ b/website/server/controllers/top-level/pages.js @@ -1,4 +1,5 @@ import locals from '../../middlewares/locals'; +import { serveClient } from '../../libs/client'; // import _ from 'lodash'; // import md from 'habitica-markdown'; // import nconf from 'nconf'; @@ -92,7 +93,7 @@ api.getNewClient = { url: '/', noLanguage: true, async handler (req, res) { - return res.sendFile('./dist-client/index.html', {root: `${__dirname}/../../../../`}); + return serveClient(res); }, }; // } diff --git a/website/server/libs/client.js b/website/server/libs/client.js new file mode 100644 index 0000000000..12aa476c95 --- /dev/null +++ b/website/server/libs/client.js @@ -0,0 +1,5 @@ +const ROOT = `${__dirname}/../../../`; + +export function serveClient (expressRes) { + return expressRes.sendFile('./dist-client/index.html', {root: ROOT}); +} \ No newline at end of file diff --git a/website/server/middlewares/notFound.js b/website/server/middlewares/notFound.js index 8e2814148b..adf74bcbd1 100644 --- a/website/server/middlewares/notFound.js +++ b/website/server/middlewares/notFound.js @@ -1,7 +1,34 @@ import { NotFound, } from '../libs/errors'; +import { serveClient } from '../libs/client'; + +// Serve the client side unless the route starts with one of these strings +// in which case, respond with a 404 error. +const TOP_LEVEL_ROUTES = [ + '/api', + '/amazon', + '/iap', + '/paypal', + '/stripe', + '/export', + '/email', + '/qr-code', + // logout, old-client and /static/user/auth/local/reset-password-set-new-one don't need the not found + // handler because they don't have any child route +]; module.exports = function NotFoundMiddleware (req, res, next) { - next(new NotFound()); + const reqUrl = req.originalUrl; + + const isExistingRoute = TOP_LEVEL_ROUTES.find(routeRoot => { + if (reqUrl.lastIndexOf(routeRoot, 0) === 0) return true; // starts with + return false; + }); + + if (isExistingRoute || req.method !== 'GET') { + return next(new NotFound()); + } else { + serveClient(res); + } }; diff --git a/website/server/middlewares/static.js b/website/server/middlewares/static.js index eb78d7eb43..505428a875 100644 --- a/website/server/middlewares/static.js +++ b/website/server/middlewares/static.js @@ -6,7 +6,7 @@ const IS_PROD = nconf.get('IS_PROD'); // const IS_NEW_CLIENT_ENABLED = nconf.get('NEW_CLIENT_ENABLED') === 'true'; const MAX_AGE = IS_PROD ? 31536000000 : 0; const ASSETS_DIR = path.join(__dirname, '/../../assets'); -const PUBLIC_DIR = path.join(__dirname, '/../../client'); +const PUBLIC_DIR = path.join(__dirname, '/../../client-old'); // TODO static files are still there const BUILD_DIR = path.join(__dirname, '/../../build'); module.exports = function staticMiddleware (expressApp) {