Skip to content

Commit

Permalink
✨ feature: define redirects in a custom file (#7719) (#7945)
Browse files Browse the repository at this point in the history
refs #7707

- be able to add a custom redirect file into the content folder
- define redirects as JSON format

The redirects feature is already present in the LTS branch.
I was not able to cherry-pick over, too many changes or conflicts.
Creating a PR to ensure 1. tests pass and 2. overview of code changes.
I had to add an example active theme to our test fixture utils, because otherwise Ghost will complain when forking Ghost.
  • Loading branch information
kirrg001 committed Feb 6, 2017
1 parent f9986c4 commit 3ed009a
Show file tree
Hide file tree
Showing 18 changed files with 696 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core/server/blog/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var debug = require('debug')('ghost:blog'),
serveSharedFile = require('../middleware/serve-shared-file'),
staticTheme = require('../middleware/static-theme'),
themeHandler = require('../middleware/theme-handler'),
customRedirects = require('../middleware/redirects'),
serveFavicon = require('../middleware/serve-favicon');

module.exports = function setupBlogApp() {
Expand All @@ -39,6 +40,10 @@ module.exports = function setupBlogApp() {
blogApp.use(themeHandler.configHbsForContext);
debug('Themes done');

// you can extend Ghost with a custom redirects file
// see https://github.com/TryGhost/Ghost/issues/7707
customRedirects(blogApp);

// Static content/assets
// @TODO make sure all of these have a local 404 error handler
// Favicon
Expand Down
2 changes: 2 additions & 0 deletions core/server/config/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ exports.getContentPath = function getContentPath(type) {
return path.join(this.get('paths:contentPath'), 'scheduling/');
case 'logs':
return path.join(this.get('paths:contentPath'), 'logs/');
case 'data':
return path.join(this.get('paths:contentPath'), 'data/');
default:
throw new Error('getContentPath was called with: ' + type);
}
Expand Down
52 changes: 52 additions & 0 deletions core/server/middleware/redirects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
var fs = require('fs-extra'),
_ = require('lodash'),
config = require('../config'),
errors = require('../errors'),
utils = require('../utils');

/**
* you can extend Ghost with a custom redirects file
* see https://github.com/TryGhost/Ghost/issues/7707
* file loads synchronously, because we need to register the routes before anything else
*/
module.exports = function redirects(blogApp) {
try {
var redirects = fs.readFileSync(config.getContentPath('data') + '/redirects.json', 'utf-8');
redirects = JSON.parse(redirects);

_.each(redirects, function (redirect) {
if (!redirect.from || !redirect.to) {
errors.logError(null, 'Your redirects.json file is in a wrong format');
return;
}

/**
* always delete trailing slashes, doesn't matter if regex or not
* Example:
* - you define /my-blog-post-1/ as from property
* - /my-blog-post-1 or /my-blog-post-1/ should work
*/
if (redirect.from.match(/\/$/)) {
redirect.from = redirect.from.slice(0, -1);
}

if (redirect.from[redirect.from.length - 1] !== '$') {
redirect.from += '\/?$';
}

blogApp.get(new RegExp(redirect.from), function (req, res) {
var maxAge = redirect.permanent ? utils.ONE_YEAR_S : 0;

res.set({
'Cache-Control': 'public, max-age=' + maxAge
});

res.redirect(redirect.permanent ? 301 : 302, req.originalUrl.replace(new RegExp(redirect.from), redirect.to));
});
});
} catch (err) {
if (err.code !== 'ENOENT') {
errors.logAndThrowError(err, 'Your redirects.json is broken.', 'Check if your JSON is valid.');
}
}
};
1 change: 1 addition & 0 deletions core/server/middleware/url-redirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ urlRedirects = function urlRedirects(req, res, next) {
}));
}

debug('no url redirect');
next();
};

Expand Down
7 changes: 7 additions & 0 deletions core/server/utils/read-directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ function readDirectory(dir, options) {
});

return tree;
})
.catch(function (err) {
if (err.code === 'ENOENT') {
return;
}

return Promise.reject(err);
});
}

Expand Down
213 changes: 213 additions & 0 deletions core/test/functional/routes/frontend_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -776,5 +776,218 @@ describe('Frontend Routing', function () {
.end(doEnd(done));
});
});

describe('Redirects (use redirects.json from test/utils/fixtures/data)', function () {
var forkedGhost, request;

before(function (done) {
testUtils.fork.ghost({
url: 'http://localhost:2370/',
server: {
port: 2370
},
paths: {
contentPath: 'core/test/utils/fixtures'
}
}, 'testredirects')
.then(function (child) {
forkedGhost = child;
request = require('supertest');
request = request('http://localhost:2370');
}).then(done).catch(done);
});

after(function (done) {
if (forkedGhost) {
forkedGhost.kill(done);
} else {
done(new Error('No forked ghost process exists, test setup must have failed.'));
}
});

describe('1 case', function () {
it('with trailing slash', function (done) {
request.get('/post/10/a-nice-blog-post')
.expect(302)
.expect('Cache-Control', testUtils.cacheRules.public)
.end(function (err, res) {
res.headers.location.should.eql('/a-nice-blog-post');
doEnd(done)(err, res);
});
});

it('without trailing slash', function (done) {
request.get('/post/10/a-nice-blog-post/')
.expect(302)
.expect('Cache-Control', testUtils.cacheRules.public)
.end(function (err, res) {
res.headers.location.should.eql('/a-nice-blog-post');
doEnd(done)(err, res);
});
});

it('should not redirect', function (done) {
request.get('/post/a-nice-blog-post/')
.end(function (err, res) {
res.statusCode.should.not.eql(302);
res.statusCode.should.not.eql(301);
doEnd(done)(err, res);
});
});
});

describe('2 case', function () {
it('with trailing slash', function (done) {
request.get('/my-old-blog-post/')
.expect(301)
.expect('Cache-Control', testUtils.cacheRules.year)
.end(function (err, res) {
res.headers.location.should.eql('/revamped-url/');
doEnd(done)(err, res);
});
});

it('without trailing slash', function (done) {
request.get('/my-old-blog-post')
.expect(301)
.expect('Cache-Control', testUtils.cacheRules.year)
.end(function (err, res) {
res.headers.location.should.eql('/revamped-url/');
doEnd(done)(err, res);
});
});

it('should not redirect', function (done) {
request.get('/my-old-blog-post-1/')
.end(function (err, res) {
res.statusCode.should.not.eql(302);
res.statusCode.should.not.eql(301);
doEnd(done)(err, res);
});
});
});

describe('3 case', function () {
it('with trailing slash', function (done) {
request.get('/what/')
.expect(302)
.expect('Cache-Control', testUtils.cacheRules.public)
.end(function (err, res) {
res.headers.location.should.eql('/what-does-god-say');
doEnd(done)(err, res);
});
});

it('without trailing slash', function (done) {
request.get('/what')
.expect(302)
.expect('Cache-Control', testUtils.cacheRules.public)
.end(function (err, res) {
res.headers.location.should.eql('/what-does-god-say');
doEnd(done)(err, res);
});
});
});

describe('4 case', function () {
it('with trailing slash', function (done) {
request.get('/search/label/&&&/')
.expect(302)
.expect('Cache-Control', testUtils.cacheRules.public)
.end(function (err, res) {
res.headers.location.should.eql('/tag/&&&/');
doEnd(done)(err, res);
});
});

it('without trailing slash', function (done) {
request.get('/search/label/&&&/')
.expect(302)
.expect('Cache-Control', testUtils.cacheRules.public)
.end(function (err, res) {
res.headers.location.should.eql('/tag/&&&/');
doEnd(done)(err, res);
});
});
});

describe('5 case', function () {
it('with trailing slash', function (done) {
request.get('/topic/')
.expect(302)
.expect('Cache-Control', testUtils.cacheRules.public)
.end(function (err, res) {
res.headers.location.should.eql('/');
doEnd(done)(err, res);
});
});

it('without trailing slash', function (done) {
request.get('/topic')
.expect(302)
.expect('Cache-Control', testUtils.cacheRules.public)
.end(function (err, res) {
res.headers.location.should.eql('/');
doEnd(done)(err, res);
});
});
});

describe('6 case', function () {
it('with trailing slash', function (done) {
request.get('/resources/download/')
.expect(302)
.expect('Cache-Control', testUtils.cacheRules.public)
.end(function (err, res) {
res.headers.location.should.eql('/shubal-stearns');
doEnd(done)(err, res);
});
});

it('without trailing slash', function (done) {
request.get('/resources/download')
.expect(302)
.expect('Cache-Control', testUtils.cacheRules.public)
.end(function (err, res) {
res.headers.location.should.eql('/shubal-stearns');
doEnd(done)(err, res);
});
});
});

describe('7 case', function () {
it('with trailing slash', function (done) {
request.get('/2016/11/welcome.html')
.expect(302)
.expect('Cache-Control', testUtils.cacheRules.public)
.end(function (err, res) {
res.headers.location.should.eql('/welcome');
doEnd(done)(err, res);
});
});
});

describe('last case', function () {
it('default', function (done) {
request.get('/prefix/')
.expect(302)
.expect('Cache-Control', testUtils.cacheRules.public)
.end(function (err, res) {
res.headers.location.should.eql('/blog/');
doEnd(done)(err, res);
});
});

it('with a custom path', function (done) {
request.get('/prefix/expect-redirect')
.expect(302)
.expect('Cache-Control', testUtils.cacheRules.public)
.end(function (err, res) {
res.headers.location.should.eql('/blog/expect-redirect');
doEnd(done)(err, res);
});
});
});
});
});
});
35 changes: 35 additions & 0 deletions core/test/utils/fixtures/data/redirects.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[
{
"from": "^/post/[0-9]+/([a-z0-9\\-]+)",
"to": "/$1"
},
{
"permanent": true,
"from": "/my-old-blog-post/",
"to": "/revamped-url/"
},
{
"from": "^\\/what(\\/?)$",
"to": "/what-does-god-say"
},
{
"from": "^\\/search\\/label\\/([^\\%20]+)$",
"to": "/tag/$1"
},
{
"from": "^\\/topic\\/",
"to": "/"
},
{
"from": "^/resources\\/download(\\/?)$",
"to": "/shubal-stearns"
},
{
"from": "^\\/[0-9]{4}\\/[0-9]{2}\\/([a-z0-9\\-]+)(\\.html)?(\\/)?$",
"to": "/$1"
},
{
"from": "^/prefix/([a-z0-9\\-]+)?",
"to": "/blog/$1"
}
]
22 changes: 22 additions & 0 deletions core/test/utils/fixtures/themes/casper/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2013-2016 Ghost Foundation

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
16 changes: 16 additions & 0 deletions core/test/utils/fixtures/themes/casper/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Casper

The default theme for [Ghost](http://github.com/tryghost/ghost/).

To download, visit the [releases](https://github.com/TryGhost/Casper/releases) page.

## Copyright & License

Copyright (c) 2013-2016 Ghost Foundation - Released under the MIT License.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Loading

0 comments on commit 3ed009a

Please sign in to comment.