diff --git a/lib/module.js b/lib/module.js index 729dfbc..3d3d767 100644 --- a/lib/module.js +++ b/lib/module.js @@ -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) diff --git a/package.json b/package.json index 33a6653..516a1a8 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "access": "public" }, "scripts": { - "dev": "nuxt test/fixture", + "dev": "nuxt test/fixture/univesal", "lint": "eslint lib test", "test": "yarn lint && jest", "release": "yarn test && standard-version && git push --follow-tags && npm publish" diff --git a/test/fixture/nuxt.config.js b/test/fixture/spa/nuxt.config.js similarity index 70% rename from test/fixture/nuxt.config.js rename to test/fixture/spa/nuxt.config.js index 1d77dae..a25f33a 100644 --- a/test/fixture/nuxt.config.js +++ b/test/fixture/spa/nuxt.config.js @@ -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') diff --git a/test/fixture/pages/index.vue b/test/fixture/spa/pages/index.vue similarity index 100% rename from test/fixture/pages/index.vue rename to test/fixture/spa/pages/index.vue diff --git a/test/fixture/pages/posts/_id.vue b/test/fixture/spa/pages/posts/_id.vue similarity index 100% rename from test/fixture/pages/posts/_id.vue rename to test/fixture/spa/pages/posts/_id.vue diff --git a/test/fixture/spa/redirects.js b/test/fixture/spa/redirects.js new file mode 100644 index 0000000..765919d --- /dev/null +++ b/test/fixture/spa/redirects.js @@ -0,0 +1,6 @@ +module.exports = [ + { from: '/redirected', to: '/' }, + { from: '/äßU<', to: '/' }, + { path: '/many/(.*)', to: '/posts/abcde' }, + { from: '/mapped/:id', redirect: '/posts/:id' } +] diff --git a/test/fixture/universal/nuxt.config.js b/test/fixture/universal/nuxt.config.js new file mode 100644 index 0000000..fa22ccc --- /dev/null +++ b/test/fixture/universal/nuxt.config.js @@ -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') + } +} diff --git a/test/fixture/universal/pages/index.vue b/test/fixture/universal/pages/index.vue new file mode 100644 index 0000000..6218ede --- /dev/null +++ b/test/fixture/universal/pages/index.vue @@ -0,0 +1,11 @@ + + + diff --git a/test/fixture/universal/pages/posts/_id.vue b/test/fixture/universal/pages/posts/_id.vue new file mode 100644 index 0000000..bf220aa --- /dev/null +++ b/test/fixture/universal/pages/posts/_id.vue @@ -0,0 +1,13 @@ + + + diff --git a/test/fixture/redirects.js b/test/fixture/universal/redirects.js similarity index 100% rename from test/fixture/redirects.js rename to test/fixture/universal/redirects.js diff --git a/test/spa.test.js b/test/spa.test.js new file mode 100644 index 0000000..5b5175e --- /dev/null +++ b/test/spa.test.js @@ -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() +}) diff --git a/test/module.test.js b/test/universal.test.js similarity index 96% rename from test/module.test.js rename to test/universal.test.js index 2034a26..01088ee 100644 --- a/test/module.test.js +++ b/test/universal.test.js @@ -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 @@ -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)