Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(module): leverage vue-router redirect #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ async function redirectModule(moduleOptions) {
...await parseOptions(moduleOptions)
}

if (this.options.mode === 'spa') {
return this.extendRoutes((routes) => {
options.rules.forEach((route) => {
routes.push({
path: route.path || route.from,
redirect: route.redirect || route.to
})
})
})
}

options.rules = options.rules.map(rule => ({ ...rule, from: new RegExp(rule.from) }))

const middleware = require('./middleware.js')(options)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"access": "public"
},
"scripts": {
"dev": "nuxt test/fixture",
"dev": "nuxt test/fixture/univesal",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo univesal >universal

"lint": "eslint lib test",
"test": "yarn lint && jest",
"release": "yarn test && standard-version && git push --follow-tags && npm publish"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const { resolve } = require('path')

module.exports = {
rootDir: resolve(__dirname, '../..'),
mode: 'spa',
rootDir: resolve(__dirname, '../../..'),
buildDir: resolve(__dirname, '.nuxt'),
srcDir: __dirname,
render: {
resourceHints: false
},
modules: [
{ handler: require('../../') }
{ handler: require('../../../') }
],
redirect: {
rules: require('./redirects')
Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions test/fixture/spa/redirects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = [
{ from: '/redirected', to: '/' },
{ from: '/äßU<', to: '/' },
{ path: '/many/(.*)', to: '/posts/abcde' },
{ from: '/mapped/:id', redirect: '/posts/:id' }
]
17 changes: 17 additions & 0 deletions test/fixture/universal/nuxt.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { resolve } = require('path')

module.exports = {
mode: 'universal',
rootDir: resolve(__dirname, '../../..'),
buildDir: resolve(__dirname, '.nuxt'),
srcDir: __dirname,
render: {
resourceHints: false
},
modules: [
{ handler: require('../../../') }
],
redirect: {
rules: require('./redirects')
}
}
11 changes: 11 additions & 0 deletions test/fixture/universal/pages/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<template>
<div>
Works!
</div>
</template>

<script>
export default {
}
</script>
13 changes: 13 additions & 0 deletions test/fixture/universal/pages/posts/_id.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<template>
<div>
{{ id }}
</div>
</template>

<script>
export default {
asyncData({ params }) {
return { id: params.id }
}
}
</script>
File renamed without changes.
110 changes: 110 additions & 0 deletions test/spa.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
jest.setTimeout(60000)

const { Nuxt, Builder } = require('nuxt-edge')
const getPort = require('get-port')
const consola = require('consola')

const redirects = require('./fixture/spa/redirects')
const config = require('./fixture/spa/nuxt.config')
config.dev = false

let nuxt, port

consola.mockTypes(() => jest.fn())

const url = path => `http://localhost:${port}${path}`

const renderRoute = async (path) => {
const window = await nuxt.renderAndGetWindow(url(path))
const head = window.document.head.innerHTML
const html = window.document.body.innerHTML
return { window, head, html }
}

const setupNuxt = async (config) => {
const nuxt = new Nuxt(config)
await nuxt.ready()
await new Builder(nuxt).build()
port = await getPort()
await nuxt.listen(port)

return nuxt
}

const testSuite = () => {
test('render', async () => {
const { html } = await renderRoute('/')
expect(html).toContain('Works!')
})

test('simple redirect', async () => {
const { html } = await renderRoute('/redirected')
expect(html).toContain('Works!')
})

test('simple non-ascii redirect', async () => {
const { html } = await renderRoute('/äßU<')
expect(html).toContain('Works!')
})

test('many redirect', async () => {
for (const n of ['abcde', 'abcdeasd', 'raeasdsads']) {
const { html } = await renderRoute(`/many/${n}`)
expect(html).toContain('abcde')
}
})

test('mapped redirect', async () => {
for (const n of ['abcde', 'abcdeasd', 'raeasdsads']) {
const { html } = await renderRoute(`/mapped/${n}`)
expect(html).toContain(n)
}
})
}

describe('module', () => {
beforeAll(async () => {
nuxt = await setupNuxt(config)
})

afterAll(async () => {
await nuxt.close()
})

testSuite()
})

describe('function', () => {
beforeAll(async () => {
nuxt = await setupNuxt({
...config,
redirect: async () => {
await Promise.resolve(r => setTimeout(r, 100))
return redirects
}
})
})

afterAll(async () => {
await nuxt.close()
})

testSuite()
})

describe('function inline', () => {
beforeAll(async () => {
nuxt = await setupNuxt({
...config,
modules: [
[require('../'), redirects]
]
})
})

afterAll(async () => {
await nuxt.close()
})

testSuite()
})
5 changes: 3 additions & 2 deletions test/module.test.js → test/universal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const request = require('request-promise-native')
const getPort = require('get-port')
const consola = require('consola')

const redirects = require('./fixture/redirects')
const config = require('./fixture/nuxt.config')
const redirects = require('./fixture/universal/redirects')
const config = require('./fixture/universal/nuxt.config')
config.dev = false

let nuxt, port
Expand All @@ -18,6 +18,7 @@ const get = path => request(url(path))

const setupNuxt = async (config) => {
const nuxt = new Nuxt(config)
await nuxt.ready()
await new Builder(nuxt).build()
port = await getPort()
await nuxt.listen(port)
Expand Down