Skip to content

Commit

Permalink
Merge 3d4e4f1 into 9b62cde
Browse files Browse the repository at this point in the history
  • Loading branch information
elvingm committed Feb 12, 2016
2 parents 9b62cde + 3d4e4f1 commit 55efbb3
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 46 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_js:
- 0.10
after_script:
- npm run coveralls
sudo: false
16 changes: 2 additions & 14 deletions lib/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ _ = require 'lodash'
W = require 'when'

module.exports = (opts) ->
codes = ['200', '301', '302', '404']

class RootsNetlify
constructor: (@roots) ->
@util = new RootsUtil(@roots)

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(@)])

Expand All @@ -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
20 changes: 9 additions & 11 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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

Expand Down
18 changes: 8 additions & 10 deletions test/fixtures/basic/app.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
18 changes: 8 additions & 10 deletions test/fixtures/promises/app.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
'/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'
]
headers:
'/protected/path':
'Cache-Control': 'max-age: 3000'
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/promises/expected/_redirects
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 55efbb3

Please sign in to comment.