Skip to content

Commit

Permalink
🐛 Remove trailing slash for {{@blog.url}} (#8596)
Browse files Browse the repository at this point in the history
closes #8569

- remove the trailing slash for `{{@blog.url}}` data in theme middleware
  • Loading branch information
aileen authored and kirrg001 committed Jun 19, 2017
1 parent 35bd0ae commit c3dbd0e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
3 changes: 2 additions & 1 deletion core/server/themes/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ themeMiddleware.updateTemplateData = function updateTemplateData(req, res, next)

// Request-specific information
// These things are super dependent on the request, so they need to be in middleware
blogData.url = utils.url.urlFor('home', {secure: req.secure}, true);
// Serve the blog url without trailing slash
blogData.url = utils.url.urlFor('home', {secure: req.secure, trailingSlash: false}, true);

// Pass 'secure' flag to the view engine
// so that templates can choose to render https or http 'url', see url utility
Expand Down
6 changes: 6 additions & 0 deletions core/server/utils/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,12 @@ function urlFor(context, data, absolute) {
if (data && data.cors) {
urlPath = urlPath.replace(/^.*?:\/\//g, '//');
}

// CASE: there are cases where urlFor('home') needs to be returned without trailing
// slash e. g. the `{{@blog.url}}` helper. See https://github.com/TryGhost/Ghost/issues/8569
if (data && data.trailingSlash === false) {
urlPath = urlPath.replace(/\/$/, '');
}
} else if (context === 'admin') {
urlPath = getAdminUrl() || getBlogUrl();

Expand Down
4 changes: 2 additions & 2 deletions core/test/functional/routes/frontend_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ describe('Frontend Routing', function () {
request.get('/')
.expect(200)
.expect(/<link rel="canonical" href="http:\/\/localhost:2370\/" \/\>/)
.expect(/<a href="http:\/\/localhost:2370\/">Ghost<\/a\>/)
.expect(/<a href="http:\/\/localhost:2370">Ghost<\/a\>/)
.end(doEnd(done));
});

Expand All @@ -738,7 +738,7 @@ describe('Frontend Routing', function () {
.set('X-Forwarded-Proto', 'https')
.expect(200)
.expect(/<link rel="canonical" href="http:\/\/localhost:2370\/" \/\>/)
.expect(/<a href="https:\/\/localhost:2370\/">Ghost<\/a\>/)
.expect(/<a href="https:\/\/localhost:2370">Ghost<\/a\>/)
.end(doEnd(done));
});
});
Expand Down
6 changes: 3 additions & 3 deletions core/test/unit/themes/middleware_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ describe('Themes', function () {
.with.properties(blogDataExpectedProps)
.and.size(blogDataExpectedProps.length);
// url should be correct
templateOptions.data.blog.url.should.eql('http://127.0.0.1:2369/');
templateOptions.data.blog.url.should.eql('http://127.0.0.1:2369');
// should get the title
templateOptions.data.blog.title.should.eql('Bloggy McBlogface');

Expand Down Expand Up @@ -173,7 +173,7 @@ describe('Themes', function () {
.with.properties(blogDataExpectedProps)
.and.size(blogDataExpectedProps.length);
// url should be correct
templateOptions.data.blog.url.should.eql('http://127.0.0.1:2369/');
templateOptions.data.blog.url.should.eql('http://127.0.0.1:2369');
// should get the title
templateOptions.data.blog.title.should.eql('Bloggy McBlogface');

Expand Down Expand Up @@ -209,7 +209,7 @@ describe('Themes', function () {
.with.properties(blogDataExpectedProps)
.and.size(blogDataExpectedProps.length);
// url should be correct HTTPS!
templateOptions.data.blog.url.should.eql('https://127.0.0.1:2369/');
templateOptions.data.blog.url.should.eql('https://127.0.0.1:2369');
// should get the title
templateOptions.data.blog.title.should.eql('Bloggy McBlogface');

Expand Down
21 changes: 21 additions & 0 deletions core/test/unit/utils/url_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,27 @@ describe('Url', function () {
utils.url.urlFor(testContext).should.equal('/blog/');
utils.url.urlFor(testContext, true).should.equal('http://my-ghost-blog.com/blog/');
utils.url.urlFor(testContext, {secure: true}, true).should.equal('https://my-ghost-blog.com/blog/');

// Output blog url without trailing slash
configUtils.set({url: 'http://my-ghost-blog.com'});
utils.url.urlFor(testContext).should.equal('/');
utils.url.urlFor(testContext, true).should.equal('http://my-ghost-blog.com/');
utils.url.urlFor(testContext, {secure: true, trailingSlash: false}, true).should.equal('https://my-ghost-blog.com');

configUtils.set({url: 'http://my-ghost-blog.com/'});
utils.url.urlFor(testContext).should.equal('/');
utils.url.urlFor(testContext, true).should.equal('http://my-ghost-blog.com/');
utils.url.urlFor(testContext, {secure: true, trailingSlash: false}, true).should.equal('https://my-ghost-blog.com');

configUtils.set({url: 'http://my-ghost-blog.com/blog'});
utils.url.urlFor(testContext).should.equal('/blog/');
utils.url.urlFor(testContext, true).should.equal('http://my-ghost-blog.com/blog/');
utils.url.urlFor(testContext, {secure: true, trailingSlash: false}, true).should.equal('https://my-ghost-blog.com/blog');

configUtils.set({url: 'http://my-ghost-blog.com/blog/'});
utils.url.urlFor(testContext).should.equal('/blog/');
utils.url.urlFor(testContext, true).should.equal('http://my-ghost-blog.com/blog/');
utils.url.urlFor(testContext, {secure: true, trailingSlash: false}, true).should.equal('https://my-ghost-blog.com/blog');
});

it('should return rss url when asked for', function () {
Expand Down

0 comments on commit c3dbd0e

Please sign in to comment.