From f44d0a3c44cea49e19ffbb68a430555c2ce28247 Mon Sep 17 00:00:00 2001 From: NGPixel Date: Sun, 23 Apr 2017 10:56:14 -0400 Subject: [PATCH] feat: URL trailing slashes remove + All pages basepath --- CHANGELOG.md | 4 +++- client/js/pages/all.js | 6 +++++- configure.js | 6 ++++++ controllers/pages.js | 8 ++++++-- middlewares/seo.js | 20 ++++++++++++++++++++ server.js | 6 ++++++ 6 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 middlewares/seo.js diff --git a/CHANGELOG.md b/CHANGELOG.md index c4e5f048..40ffd18d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,12 +8,14 @@ This project adheres to [Semantic Versioning](http://semver.org/). - **Auth**: Can now specify Read Access by default for all providers (except Local) - **View**: MathML and TeX math equations support - **Configuration Wizard**: Added Public Access option +- **Misc**: Heroku support - **Navigation**: All Pages section - **UI**: Beatiful new logo! ### Changed - **Auth**: Provider Strategies are now only loaded if enabled -- **System**: Updated dependencies +- **Misc**: Trailing slashes in URL are now removed +- **Misc**: Updated dependencies - **UI**: Footer is now always at the bottom of the page (but not fixed) ### Fixed diff --git a/client/js/pages/all.js b/client/js/pages/all.js index 2f0dd344..88e13997 100644 --- a/client/js/pages/all.js +++ b/client/js/pages/all.js @@ -32,7 +32,11 @@ module.exports = (alerts, socket) => { } }, mounted: function () { - this.fetch('') + let basePath = window.location.pathname.slice(0, -4) + if (basePath.length > 1) { + basePath = basePath.slice(1) + } + this.fetch(basePath) } }) } diff --git a/configure.js b/configure.js index d5b04bbd..358cc9d4 100644 --- a/configure.js +++ b/configure.js @@ -351,6 +351,12 @@ module.exports = (port, spinner) => { }) }) ).then(() => { + if (process.env.IS_HEROKU) { + return fs.outputJsonAsync('./app/heroku.json', { configured: true }) + } else { + return true + } + }).then(() => { res.json({ ok: true }) }).catch(err => { res.json({ ok: false, error: err.message }) diff --git a/controllers/pages.js b/controllers/pages.js index 986030a6..cb869d3c 100644 --- a/controllers/pages.js +++ b/controllers/pages.js @@ -137,8 +137,12 @@ router.put('/create/*', (req, res, next) => { /** * View tree view of all pages */ -router.get('/all', (req, res, next) => { - res.render('pages/all') +router.use((req, res, next) => { + if (_.endsWith(req.url, '/all')) { + res.render('pages/all') + } else { + next() + } }) // ========================================== diff --git a/middlewares/seo.js b/middlewares/seo.js new file mode 100644 index 00000000..a3788020 --- /dev/null +++ b/middlewares/seo.js @@ -0,0 +1,20 @@ +'use strict' + +const _ = require('lodash') + +/** + * SEO Middleware + * + * @param {Express Request} req Express request object + * @param {Express Response} res Express response object + * @param {Function} next next callback function + * @return {any} void + */ +module.exports = function (req, res, next) { + if (req.path.length > 1 && _.endsWith(req.path, '/')) { + let query = req.url.slice(req.path.length) || '' + res.redirect(301, req.path.slice(0, -1) + query) + } else { + return next() + } +} diff --git a/server.js b/server.js index 22c7f516..c6479585 100644 --- a/server.js +++ b/server.js @@ -110,6 +110,12 @@ app.use(flash()) app.use(passport.initialize()) app.use(passport.session()) +// ---------------------------------------- +// SEO +// ---------------------------------------- + +app.use(mw.seo) + // ---------------------------------------- // Localization Engine // ----------------------------------------