forked from nuxt-community/redirect-module
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c8fe8b6
Showing
16 changed files
with
24,705 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
version: 2 | ||
jobs: | ||
build: | ||
working_directory: /usr/src/app | ||
docker: | ||
- image: banian/node | ||
steps: | ||
# Checkout repository | ||
- checkout | ||
|
||
# Restore cache | ||
- restore_cache: | ||
key: yarn-{{ checksum "yarn.lock" }} | ||
|
||
# Install dependencies | ||
- run: | ||
name: Install Dependencies | ||
command: NODE_ENV=dev yarn | ||
|
||
# Keep cache | ||
- save_cache: | ||
key: yarn-{{ checksum "yarn.lock" }} | ||
paths: | ||
- "node_modules" | ||
|
||
# Test | ||
- run: | ||
name: Tests | ||
command: yarn test | ||
|
||
# Coverage | ||
- run: | ||
name: Coverage | ||
command: yarn codecov |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# editorconfig.org | ||
root = true | ||
|
||
[*] | ||
indent_size = 2 | ||
indent_style = space | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module.exports = { | ||
root: true, | ||
parserOptions: { | ||
sourceType: 'module' | ||
}, | ||
env: { | ||
browser: true, | ||
node: true, | ||
jest: true | ||
}, | ||
extends: 'standard', | ||
plugins: [ | ||
'jest', | ||
'vue' | ||
], | ||
rules: { | ||
// Allow paren-less arrow functions | ||
'arrow-parens': 0, | ||
// Allow async-await | ||
'generator-star-spacing': 0, | ||
// Allow debugger during development | ||
'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0, | ||
// Do not allow console.logs etc... | ||
'no-console': 2 | ||
}, | ||
globals: { | ||
'jest/globals': true, | ||
jasmine: true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
node_modules | ||
*.iml | ||
.idea | ||
*.log* | ||
.nuxt | ||
.vscode | ||
.DS_STORE | ||
coverage | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) Alexander Lichter <[email protected]> | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# Redirect Module 🔀 No more **cumbersome** redirects! | ||
[](https://npmjs.com/package/@nuxtjs/redirect-module) | ||
[](https://npmjs.com/package/@nuxtjs/redirect-module) | ||
[](https://circleci.com/gh/nuxt-community/redirect-module) | ||
[](https://codecov.io/gh/nuxt-community/redirect-module) | ||
[](https://david-dm.org/nuxt-community/redirect-module) | ||
[](http://standardjs.com) | ||
|
||
> | ||
[📖 **Release Notes**](./CHANGELOG.md) | ||
|
||
## Features | ||
|
||
Redirecting URLs is an often discussed topic, especially when it comes to | ||
SEO. Previously it was hard to create a "real" redirect without performance | ||
loss or incorrect handling. But this time is over! | ||
|
||
With the Redirect Module setting up redirects will become easier than ever before! | ||
|
||
## Setup | ||
- Add `@nuxtjs/redirect-module` dependency using yarn or npm to your project | ||
- Add `@nuxtjs/redirect-module` to `modules` section of `nuxt.config.js` | ||
- Configure it: | ||
```js | ||
{ | ||
modules: [ | ||
// Simple usage | ||
'@nuxtjs/redirect-module', | ||
|
||
// With options | ||
['@nuxtjs/redirect-module', []], | ||
] | ||
} | ||
``` | ||
|
||
```js | ||
{ | ||
modules: [ | ||
'@nuxtjs/redirect-module' | ||
], | ||
redirect: [ | ||
// Module options | ||
] | ||
} | ||
``` | ||
|
||
## Usage | ||
|
||
Simply add the links you want to redirect as objects to the module option array: | ||
|
||
```js | ||
redirect: [ | ||
{ from: '^/myoldurl', to: '/mynewurl' } | ||
] | ||
``` | ||
|
||
You can set up a custom status code as well. By default, it's *302*! | ||
|
||
```js | ||
redirect: [ | ||
{ from: '^/myoldurl', to: '/mynewurl', statusCode: 301 } | ||
] | ||
``` | ||
|
||
As you may have already noticed, we are leveraging the benefits of | ||
*Regular Expressions*. Hence, you can fully customize your redirects. | ||
|
||
```js | ||
redirect: [ | ||
{ from: '^/myoldurl/(.*)$', to: '/comeallhere', } // Many urls to one | ||
{ from: '^/anotherold/(.*)$', to: '/new/$1', } // One to one mapping | ||
] | ||
``` | ||
|
||
And if you really need more power... okay! You can also use a factory function | ||
to generate your redirects: | ||
|
||
```js | ||
redirect: async () => { | ||
const someThings = await axios.get("/myApi") // It'll wait! | ||
return someThings.map(coolTransformFunction) | ||
} | ||
``` | ||
|
||
**ATTENTION**: The factory function **must** return an array with redirect | ||
objects (as seen above). | ||
|
||
## Development | ||
|
||
- Clone this repository | ||
- Install dependencies using `yarn install` or `npm install` | ||
- Start development server using `npm run dev` | ||
|
||
## License | ||
|
||
[MIT License](./LICENSE) | ||
|
||
Copyright (c) Alexander Lichter <[email protected]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Creates new middleware using provided options | ||
function create (rules) { | ||
return function redirectRoute (req, res, next) { | ||
const foundRule = rules.find(o => o.from.test(req.url)) | ||
|
||
if (!foundRule) { | ||
return next() | ||
} | ||
const toUrl = req.url.replace(foundRule.from, foundRule.to) | ||
|
||
res.statusCode = foundRule.statusCode || 302 | ||
res.setHeader('Location', toUrl) | ||
res.end() | ||
} | ||
} | ||
|
||
module.exports = create |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module.exports = async function module (moduleOptions) { | ||
if (typeof moduleOptions === 'function') { | ||
moduleOptions = await moduleOptions() | ||
} | ||
if (typeof this.options.redirect === 'function') { | ||
this.options.redirect = await this.options.redirect() | ||
} | ||
|
||
const initialRules = Object.assign([], this.options.redirect, moduleOptions) | ||
|
||
// Transform each "from" value to a RegExp for later test | ||
const regExpRules = initialRules.map(o => (Object.assign({}, { ...o, from: new RegExp(o.from) }))) | ||
const middleware = require('./middleware.js')(regExpRules) | ||
|
||
this.addServerMiddleware(middleware) | ||
} |
Oops, something went wrong.