Skip to content

Commit

Permalink
🎨 Dynamic Routing Beta: Enabled redirect by default when using data…
Browse files Browse the repository at this point in the history
… key

refs #9601

- when using the short form `data: tag.welcome` the redirect is enabled by default
  - /tag/welcome/ will redirect to the channel/collection which makes use of the data key
- you can disable the redirect by using the long form
  e.g. data:
	tag:
	  resource: tags
          type: read
          slug: welcome
          redirect: false
  • Loading branch information
kirrg001 committed Aug 16, 2018
1 parent 220d045 commit 17833a6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 28 deletions.
14 changes: 3 additions & 11 deletions core/server/services/settings/validate.js
Expand Up @@ -46,22 +46,14 @@ _private.validateData = function validateData(object) {
});
}

let redirect = false;

// CASE: user wants to redirect traffic from resource to route
// @TODO: enable redirect feature if confirmed
if (false && shortForm.match(/^->/)) { // eslint-disable-line no-constant-condition
shortForm = shortForm.replace(/^->/, '');
redirect = true;
}

let [resourceKey, slug] = shortForm.split('.');

longForm.query[options.resourceKey || resourceKey] = {};
longForm.query[options.resourceKey || resourceKey] = _.omit(_.cloneDeep(RESOURCE_CONFIG.QUERY[resourceKey]), 'alias');

// redirect is enabled by default when using the short form
longForm.router = {
[RESOURCE_CONFIG.QUERY[resourceKey].alias]: [{slug: slug, redirect: redirect}]
[RESOURCE_CONFIG.QUERY[resourceKey].alias]: [{slug: slug, redirect: true}]
};

longForm.query[options.resourceKey || resourceKey].options.slug = slug;
Expand All @@ -80,7 +72,7 @@ _private.validateData = function validateData(object) {
const allowedQueryOptions = ['limit', 'filter', 'include', 'slug', 'visibility', 'status'];
const allowedRouterOptions = ['redirect', 'slug'];
const defaultRouterOptions = {
redirect: false
redirect: true
};

let data = {
Expand Down
13 changes: 7 additions & 6 deletions core/test/integration/web/site_spec.js
Expand Up @@ -1267,12 +1267,13 @@ describe('Integration - Web - Site', function () {
resource: 'users',
type: 'read',
options: {
slug: 'joe-bloggs'
slug: 'joe-bloggs',
redirect: false
}
}
},
router: {
users: [{redirect: true, slug: 'joe-bloggs'}]
users: [{redirect: false, slug: 'joe-bloggs'}]
}
}
},
Expand All @@ -1290,12 +1291,13 @@ describe('Integration - Web - Site', function () {
resource: 'users',
type: 'read',
options: {
slug: 'joe-bloggs'
slug: 'joe-bloggs',
redirect: false
}
}
},
router: {
users: [{redirect: true, slug: 'joe-bloggs'}]
users: [{redirect: false, slug: 'joe-bloggs'}]
}
}
}
Expand Down Expand Up @@ -1478,8 +1480,7 @@ describe('Integration - Web - Site', function () {

return testUtils.mocks.express.invoke(app, req)
.then(function (response) {
response.statusCode.should.eql(301);
response.headers.location.should.eql('/channel3/');
response.statusCode.should.eql(200);
});
});
});
Expand Down
21 changes: 10 additions & 11 deletions core/test/unit/services/settings/validate_spec.js
Expand Up @@ -323,7 +323,6 @@ describe('UNIT: services/settings/validate', function () {
'/food/': {
data: 'tag.food'
},
// @TODO: enable redirect
'/music/': {
data: 'tag.music'
},
Expand Down Expand Up @@ -370,7 +369,7 @@ describe('UNIT: services/settings/validate', function () {
}
},
router: {
tags: [{redirect: false, slug: 'food'}]
tags: [{redirect: true, slug: 'food'}]
}
},
templates: []
Expand All @@ -388,7 +387,7 @@ describe('UNIT: services/settings/validate', function () {
}
},
router: {
tags: [{redirect: false, slug: 'music'}]
tags: [{redirect: true, slug: 'music'}]
}
},
templates: []
Expand All @@ -414,7 +413,7 @@ describe('UNIT: services/settings/validate', function () {
}
},
router: {
tags: [{redirect: false, slug: 'bed'}, {redirect: false, slug: 'dream'}]
tags: [{redirect: true, slug: 'bed'}, {redirect: true, slug: 'dream'}]
}
},
templates: []
Expand All @@ -436,7 +435,7 @@ describe('UNIT: services/settings/validate', function () {
}
},
router: {
pages: [{redirect: false, slug: 'home'}]
pages: [{redirect: true, slug: 'home'}]
}
},
templates: []
Expand All @@ -455,7 +454,7 @@ describe('UNIT: services/settings/validate', function () {
}
},
router: {
tags: [{redirect: false, slug: 'something'}]
tags: [{redirect: true, slug: 'something'}]
}
},
templates: []
Expand All @@ -474,7 +473,7 @@ describe('UNIT: services/settings/validate', function () {
}
},
router: {
tags: [{redirect: false, slug: 'sport'}]
tags: [{redirect: true, slug: 'sport'}]
}
},
templates: []
Expand All @@ -499,7 +498,7 @@ describe('UNIT: services/settings/validate', function () {
posts: {
resource: 'posts',
type: 'read',
redirect: true
redirect: false
}
}
},
Expand Down Expand Up @@ -542,7 +541,7 @@ describe('UNIT: services/settings/validate', function () {
}
},
router: {
posts: [{redirect: false}]
posts: [{redirect: true}]
}
},
templates: []
Expand All @@ -561,7 +560,7 @@ describe('UNIT: services/settings/validate', function () {
}
},
router: {
posts: [{redirect: true}]
posts: [{redirect: false}]
}
},
templates: []
Expand Down Expand Up @@ -601,7 +600,7 @@ describe('UNIT: services/settings/validate', function () {
}
},
router: {
posts: [{redirect: false, slug: 'ups'}]
posts: [{redirect: true, slug: 'ups'}]
}
},
templates: []
Expand Down

0 comments on commit 17833a6

Please sign in to comment.