From ca696b756240f73390bbc2d7f85746ee89fbfd9f Mon Sep 17 00:00:00 2001 From: Elvin G Marmol Date: Fri, 12 Feb 2016 14:10:17 -0500 Subject: [PATCH 1/4] Make redirects and rewrites accept an array of rules in order defined by user --- lib/index.coffee | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/lib/index.coffee b/lib/index.coffee index f760509..ee4577a 100644 --- a/lib/index.coffee +++ b/lib/index.coffee @@ -4,8 +4,6 @@ _ = require 'lodash' W = require 'when' module.exports = (opts) -> - codes = ['200', '301', '302', '404'] - class RootsNetlify constructor: (@roots) -> @util = new RootsUtil(@roots) @@ -13,7 +11,7 @@ module.exports = (opts) -> setup: -> W(opts).with(@) .then (opts) -> - @opts = _.defaults(opts, {redirects: {}, rewrites: {}, headers: {}}) + @opts = _.defaults(opts, {redirects: {}, headers: {}}) .then -> W.all([write_headers.call(@), write_redirects.call(@)]) @@ -26,15 +24,5 @@ module.exports = (opts) -> @util.write '_headers', res write_redirects = -> - redirects = _.pick(@opts.redirects, codes) - redirects['200'] ?= {} - redirects['301'] ?= {} - _.merge(redirects['200'], @opts.rewrites) - _.merge(redirects['301'], _.omit(@opts.redirects, codes)) - - res = _.reduce redirects, (str, conf, code) -> - for k, v of conf - str += "#{k} #{v} #{code}\n" - return str - , '' + res = @opts.redirects.join('\n') @util.write '_redirects', res From 9ecf57a717cec1188f1ecfeb5ea4d0b5a89510fc Mon Sep 17 00:00:00 2001 From: Elvin G Marmol Date: Fri, 12 Feb 2016 14:32:07 -0500 Subject: [PATCH 2/4] Update Readme with explanation for new approach --- readme.md | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/readme.md b/readme.md index 97e309f..7cec42f 100644 --- a/readme.md +++ b/readme.md @@ -27,16 +27,14 @@ netlify = require 'roots-netlify' module.exports = extensions: [ netlify - redirects: - '/news': '/blog' - '/news/:year/:month:/:date/:slug': '/blog/:year/:month/:date/:story_id' - '/news/*': '/blog/:splat' - '302': - '/temp_redirect': '/' - '404': - '/ecommerce': '/closed' - rewrites: - '/*': '/index.html' + redirects: [ + '/news /blog 200' + '/news/:year/:month:/:date/:slug /blog/:year/:month/:date/:story_id 200' + '/news/* /blog/:splat 200' + '/redirect / 301' + '/temp_redirect / 302' + '/ecommerce /closed 404' + ] headers: '/protected/path': 'Cache-Control': 'max-age: 3000' @@ -49,7 +47,7 @@ module.exports = Read the Netlify documentation on [redirects](https://docs.netlify.com/redirects/) and [headers](https://docs.netlify.com/headers_and_basic_auth) to learn more. -Redirects added to the `redirects` object return a status code of `301` while those added to the `rewrites` object will return `200` (a rewrite). Netlify also [supports](https://docs.netlify.com/redirects#http-status-codes) two other status codes: `302` and `404`. In order to configure your redirects for these, add a `302` or `404` key to `redirects` and nest your configuration object there (see example above). +The `redirects` property accepts an array of redirects or rewrite rules (in order) with their respective HTTP code appended at the end as described in Netlify's documentation. ### Promises From 8cb2a45e0fbfddb7c9bb28865723914d9ce242f3 Mon Sep 17 00:00:00 2001 From: Elvin G Marmol Date: Fri, 12 Feb 2016 14:36:45 -0500 Subject: [PATCH 3/4] Update test fixtures --- test/fixtures/basic/app.coffee | 18 ++++++++---------- test/fixtures/promises/app.coffee | 18 ++++++++---------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/test/fixtures/basic/app.coffee b/test/fixtures/basic/app.coffee index 9b18d6f..7187324 100644 --- a/test/fixtures/basic/app.coffee +++ b/test/fixtures/basic/app.coffee @@ -4,16 +4,14 @@ module.exports = ignores: ["**/.DS_Store"] extensions: [ netlify - redirects: - '/news': '/blog' - '/news/:year/:month:/:date/:slug': '/blog/:year/:month/:date/:story_id' - '/news/*': '/blog/:splat' - '302': - '/temp_redirect': '/' - '404': - '/ecommerce': '/closed' - rewrites: - '/*': '/index.html' + redirects: [ + '/* /index.html 200' + '/news /blog 301' + '/news/:year/:month:/:date/:slug /blog/:year/:month/:date/:story_id 301' + '/news/* /blog/:splat 301' + '/temp_redirect / 302' + '/ecommerce /closed 404' + ] headers: '/protected/path': 'Cache-Control': 'max-age: 3000' diff --git a/test/fixtures/promises/app.coffee b/test/fixtures/promises/app.coffee index 54c06b2..589ed15 100644 --- a/test/fixtures/promises/app.coffee +++ b/test/fixtures/promises/app.coffee @@ -2,16 +2,14 @@ netlify = require '../../..' W = require 'when' config = - redirects: - '/news': '/blog' - '/news/:year/:month:/:date/:slug': '/blog/:year/:month/:date/:story_id' - '/news/*': '/blog/:splat' - '302': - '/temp_redirect': '/' - '404': - '/ecommerce': '/closed' - rewrites: - '/*': '/index.html' + redirects: [ + '/* /index.html 200' + '/news /blog 301' + '/news/:year/:month:/:date/:slug /blog/:year/:month/:date/:story_id 301' + '/news/* /blog/:splat 301' + '/temp_redirect / 302' + '/ecommerce /closed 404' + ] headers: '/protected/path': 'Cache-Control': 'max-age: 3000' From 75ea468756da20b0c33f0959452e2b3de0412731 Mon Sep 17 00:00:00 2001 From: Elvin G Marmol Date: Fri, 12 Feb 2016 15:06:34 -0500 Subject: [PATCH 4/4] Change order of redirects to show they are unsorted --- test/fixtures/promises/app.coffee | 2 +- test/fixtures/promises/expected/_redirects | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/fixtures/promises/app.coffee b/test/fixtures/promises/app.coffee index 589ed15..17af22d 100644 --- a/test/fixtures/promises/app.coffee +++ b/test/fixtures/promises/app.coffee @@ -3,12 +3,12 @@ W = require 'when' config = redirects: [ + '/ecommerce /closed 404' '/* /index.html 200' '/news /blog 301' '/news/:year/:month:/:date/:slug /blog/:year/:month/:date/:story_id 301' '/news/* /blog/:splat 301' '/temp_redirect / 302' - '/ecommerce /closed 404' ] headers: '/protected/path': diff --git a/test/fixtures/promises/expected/_redirects b/test/fixtures/promises/expected/_redirects index f06066a..321f8db 100644 --- a/test/fixtures/promises/expected/_redirects +++ b/test/fixtures/promises/expected/_redirects @@ -1,6 +1,6 @@ +/ecommerce /closed 404 /* /index.html 200 /news /blog 301 /news/:year/:month:/:date/:slug /blog/:year/:month/:date/:story_id 301 /news/* /blog/:splat 301 /temp_redirect / 302 -/ecommerce /closed 404