Skip to content

Commit

Permalink
refactor: update module (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardogobbosouza authored and manniL committed Feb 18, 2019
1 parent 9650ae9 commit 7212fa8
Show file tree
Hide file tree
Showing 20 changed files with 2,196 additions and 2,518 deletions.
16 changes: 10 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
version: 2
jobs:
build:
working_directory: /usr/src/app
docker:
- image: banian/node
- image: circleci/node
steps:
# Checkout repository
- checkout

# Restore cache
- restore_cache:
key: yarn-{{ checksum "yarn.lock" }}
key: yarn-cache-{{ checksum "yarn.lock" }}

# Install dependencies
- run:
Expand All @@ -19,14 +18,19 @@ jobs:

# Keep cache
- save_cache:
key: yarn-{{ checksum "yarn.lock" }}
key: yarn-cache-{{ checksum "yarn.lock" }}
paths:
- "node_modules"

# Test
# Lint
- run:
name: Lint
command: yarn lint

# Tests
- run:
name: Tests
command: yarn test
command: yarn jest

# Coverage
- run:
Expand Down
8 changes: 8 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Common
node_modules
dist
.nuxt
coverage

# Plugin
lib/plugin.js
4 changes: 3 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ module.exports = {
parser: 'babel-eslint',
sourceType: 'module'
},
extends: '@nuxtjs'
extends: [
'@nuxtjs'
]
}
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ node_modules
*.log*
.nuxt
.vscode
.DS_STORE
.DS_Store
coverage
dist
dist
46 changes: 31 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# Redirect Module 🔀 No more **cumbersome** redirects!
[![npm (scoped with tag)](https://img.shields.io/npm/v/@nuxtjs/redirect-module/latest.svg?style=flat-square)](https://npmjs.com/package/@nuxtjs/redirect-module)
[![npm](https://img.shields.io/npm/dt/@nuxtjs/redirect-module.svg?style=flat-square)](https://npmjs.com/package/@nuxtjs/redirect-module)
[![CircleCI](https://img.shields.io/circleci/project/github/nuxt-community/redirect-module.svg?style=flat-square)](https://circleci.com/gh/nuxt-community/redirect-module)
[![Codecov](https://img.shields.io/codecov/c/github/nuxt-community/redirect-module.svg?style=flat-square)](https://codecov.io/gh/nuxt-community/redirect-module)
[![Dependencies](https://david-dm.org/nuxt-community/redirect-module/status.svg?style=flat-square)](https://david-dm.org/nuxt-community/redirect-module)
[![js-standard-style](https://img.shields.io/badge/code_style-standard-brightgreen.svg?style=flat-square)](http://standardjs.com)

>
[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]
[![Circle CI][circle-ci-src]][circle-ci-href]
[![Codecov][codecov-src]][codecov-href]
[![Dependencies][david-dm-src]][david-dm-href]
[![Standard JS][standard-js-src]][standard-js-href]

> Nuxt module to dynamically redirect initial requests
[📖 **Release Notes**](./CHANGELOG.md)

Expand All @@ -16,23 +17,24 @@ 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:

### Inline options
1. Add `@nuxtjs/redirect-module` dependency with `yarn` or `npm` into your project
2. Add `@nuxtjs/redirect-module` to `modules` section of `nuxt.config.js`:
3. Configure it:

```js
{
modules: [
['@nuxtjs/redirect-module', [ /* Redirect option here */]],
['@nuxtjs/redirect-module', {
// Redirect option here
}]
]
}
```

### Dedicated option array
### Using top level options

```js
{
modules: [
Expand Down Expand Up @@ -115,3 +117,17 @@ The redirect module will not work in combination with `nuxt generate`. Redirects
[MIT License](./LICENSE)

Copyright (c) Alexander Lichter <npm@lichter.io>

<!-- Badges -->
[npm-version-src]: https://img.shields.io/npm/dt/@nuxtjs/redirect-module.svg?style=flat-square
[npm-version-href]: https://npmjs.com/package/@nuxtjs/redirect-module
[npm-downloads-src]: https://img.shields.io/npm/v/@nuxtjs/redirect-module/latest.svg?style=flat-square
[npm-downloads-href]: https://npmjs.com/package/@nuxtjs/redirect-module
[circle-ci-src]: https://img.shields.io/circleci/project/github/nuxt-community/redirect-module.svg?style=flat-square
[circle-ci-href]: https://circleci.com/gh/nuxt-community/redirect-module
[codecov-src]: https://img.shields.io/codecov/c/github/nuxt-community/redirect-module.svg?style=flat-square
[codecov-href]: https://codecov.io/gh/nuxt-community/redirect-module
[david-dm-src]: https://david-dm.org/nuxt-community/redirect-module/status.svg?style=flat-square
[david-dm-href]: https://david-dm.org/nuxt-community/redirect-module
[standard-js-src]: https://img.shields.io/badge/code_style-standard-brightgreen.svg?style=flat-square
[standard-js-href]: https://standardjs.com
11 changes: 11 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
presets: [
[
'@babel/preset-env', {
targets: {
esmodules: true
}
}
]
]
}
5 changes: 5 additions & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
extends: [
'@commitlint/config-conventional'
]
}
7 changes: 7 additions & 0 deletions husky.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
hooks: {
'commit-msg': 'commitlint -E HUSKY_GIT_PARAMS',
'pre-commit': 'yarn lint',
'pre-push': 'yarn lint'
}
}
17 changes: 17 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
testEnvironment: 'node',
collectCoverage: true,
collectCoverageFrom: [
'lib/**/*.js',
'!lib/plugin.js'
],
moduleNameMapper: {
'^~/(.*)$': '<rootDir>/lib/$1',
'^~~$': '<rootDir>',
'^@@$': '<rootDir>',
'^@/(.*)$': '<rootDir>/lib/$1'
},
transform: {
'^.+\\.js$': 'babel-jest'
}
}
8 changes: 2 additions & 6 deletions lib/middleware.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
/* eslint-disable no-console */

// Creates new middleware using provided options
function create(rules) {
module.exports = function (rules) {
return async function redirectRoute(req, res, next) {
const decodedBaseUrl = decodeURI(req.url)
const foundRule = rules.find(o => o.from.test(decodedBaseUrl))

if (!foundRule) {
return next()
}

// Expect rule 'to' to either a
// 1) regex
// 2) string
// 3) function taking from & req (when from is regex, req might be more interesting)

const toTarget = typeof foundRule.to === 'function' ? await foundRule.to(foundRule.from, req) : foundRule.to

const toUrl = decodedBaseUrl.replace(foundRule.from, toTarget)

res.statusCode = foundRule.statusCode || 302
res.setHeader('Location', toUrl)
res.end()
}
}

module.exports = create
6 changes: 5 additions & 1 deletion lib/module.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export default async function nuxtRedirect(moduleOptions) {
async function redirectModule(moduleOptions) {
if (typeof moduleOptions === 'function') {
moduleOptions = await moduleOptions()
}

if (typeof this.options.redirect === 'function') {
this.options.redirect = await this.options.redirect()
}
Expand All @@ -14,3 +15,6 @@ export default async function nuxtRedirect(moduleOptions) {

this.addServerMiddleware(middleware)
}

module.exports = redirectModule
module.exports.meta = require('../package.json')
85 changes: 33 additions & 52 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,73 +1,54 @@
{
"name": "@nuxtjs/redirect-module",
"version": "0.2.0",
"description": "",
"description": "Nuxt module to dynamically redirect initial requests",
"keywords": [
"nuxtjs",
"nuxt",
"redirect",
"seo"
],
"license": "MIT",
"contributors": [
{
"name": "Alexander Lichter <npm@lichter.io>"
}
"Alexander Lichter <npm@lichter.io>"
],
"main": "lib/module.js",
"repository": "https://github.com/nuxt-community/redirect-module",
"publishConfig": {
"access": "public"
},
"scripts": {
"dev": "nuxt test/fixture",
"lint": "eslint lib test",
"commitlint": "commitlint -e $GIT_PARAMS",
"test": "yarn lint && jest",
"release": "standard-version && git push --follow-tags && npm publish"
"release": "yarn test && standard-version && git push --follow-tags && npm publish"
},
"eslintIgnore": [
"lib/templates/*.*"
],
"files": [
"lib"
],
"keywords": [
"nuxtjs",
"nuxt",
"redirect",
"seo"
],
"engines": {
"node": ">=8.0.0",
"npm": ">=5.0.0"
},
"jest": {
"testEnvironment": "node",
"collectCoverage": true,
"coveragePathIgnorePatterns": [
"/node_modules/",
"/test/fixture"
]
},
"devDependencies": {
"@commitlint/cli": "^7.2.1",
"@commitlint/config-conventional": "^7.1.2",
"@nuxtjs/eslint-config": "^0.0.1",
"babel-eslint": "^10.0.1",
"codecov": "^3.1.0",
"eslint": "^5.9.0",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-jest": "^22.0.0",
"eslint-plugin-node": "^8.0.0",
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-standard": "^4.0.0",
"eslint-plugin-vue": "^4.7.1",
"husky": "^1.2.0",
"jest": "^24.0.0",
"jsdom": "^13.0.0",
"nuxt": "^2.3.2",
"standard-version": "^4.4.0"
},
"husky": {
"hooks": {
"pre-commit": "npm run lint",
"commit-msg": "npm run commitlint"
}
"@babel/core": "latest",
"@babel/preset-env": "latest",
"@commitlint/cli": "latest",
"@commitlint/config-conventional": "latest",
"@nuxtjs/eslint-config": "latest",
"babel-eslint": "latest",
"babel-jest": "latest",
"codecov": "latest",
"eslint": "latest",
"eslint-config-standard": "latest",
"eslint-plugin-import": "latest",
"eslint-plugin-jest": "latest",
"eslint-plugin-node": "latest",
"eslint-plugin-promise": "latest",
"eslint-plugin-standard": "latest",
"eslint-plugin-vue": "latest",
"get-port": "latest",
"husky": "latest",
"jest": "latest",
"jsdom": "latest",
"nuxt-edge": "latest",
"request": "latest",
"request-promise-native": "latest",
"standard-version": "latest"
}
}
6 changes: 3 additions & 3 deletions test/fixture/pages/index.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<template>
<div>
<div>
Works!
</div>
</div>
</template>

<script>
export default {
}
</script>
</script>
2 changes: 1 addition & 1 deletion test/fixture/pages/posts/_id.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<script>
export default {
asyncData ({ params }) {
asyncData({ params }) {
return { id: params.id }
}
}
Expand Down
3 changes: 3 additions & 0 deletions test/fixture/simple_function.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const { redirects, baseConfig } = require('./utils')

module.exports = Object.assign({}, baseConfig, {
modules: [
{ handler: require('../../') }
],
redirect: async () => {
await Promise.resolve(r => setTimeout(r, 100))
return redirects
Expand Down
2 changes: 1 addition & 1 deletion test/fixture/simple_function_inline.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ const { redirects, baseConfig } = require('./utils')

module.exports = Object.assign({}, baseConfig, {
modules: [
['@@', () => redirects]
[require('../../'), () => redirects]
]
})
7 changes: 6 additions & 1 deletion test/fixture/simple_object.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
const { redirects, baseConfig } = require('./utils')

module.exports = Object.assign({}, baseConfig, { redirect: redirects })
module.exports = Object.assign({}, baseConfig, {
modules: [
{ handler: require('../../') }
],
redirect: redirects
})
Loading

0 comments on commit 7212fa8

Please sign in to comment.