fix hard links for new client (#8986)

This commit is contained in:
Matteo Pagliazzi
2017-08-24 18:19:31 +02:00
committed by GitHub
parent 5c89451985
commit 82c912237b
4 changed files with 36 additions and 3 deletions

View File

@@ -1,4 +1,5 @@
import locals from '../../middlewares/locals'; import locals from '../../middlewares/locals';
import { serveClient } from '../../libs/client';
// import _ from 'lodash'; // import _ from 'lodash';
// import md from 'habitica-markdown'; // import md from 'habitica-markdown';
// import nconf from 'nconf'; // import nconf from 'nconf';
@@ -92,7 +93,7 @@ api.getNewClient = {
url: '/', url: '/',
noLanguage: true, noLanguage: true,
async handler (req, res) { async handler (req, res) {
return res.sendFile('./dist-client/index.html', {root: `${__dirname}/../../../../`}); return serveClient(res);
}, },
}; };
// } // }

View File

@@ -0,0 +1,5 @@
const ROOT = `${__dirname}/../../../`;
export function serveClient (expressRes) {
return expressRes.sendFile('./dist-client/index.html', {root: ROOT});
}

View File

@@ -1,7 +1,34 @@
import { import {
NotFound, NotFound,
} from '../libs/errors'; } 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) { 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);
}
}; };

View File

@@ -6,7 +6,7 @@ const IS_PROD = nconf.get('IS_PROD');
// const IS_NEW_CLIENT_ENABLED = nconf.get('NEW_CLIENT_ENABLED') === 'true'; // const IS_NEW_CLIENT_ENABLED = nconf.get('NEW_CLIENT_ENABLED') === 'true';
const MAX_AGE = IS_PROD ? 31536000000 : 0; const MAX_AGE = IS_PROD ? 31536000000 : 0;
const ASSETS_DIR = path.join(__dirname, '/../../assets'); 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'); const BUILD_DIR = path.join(__dirname, '/../../build');
module.exports = function staticMiddleware (expressApp) { module.exports = function staticMiddleware (expressApp) {