Skip to content

Commit

Permalink
fix: don't leave out non-prefixed routes for generate + prefix strate…
Browse files Browse the repository at this point in the history
…gy (#726)

To avoid having to create server-side redirect rules, the non-prefixed
routes are not removed when using prefix strategy with Nuxt generate.
The non-prefixed routes should automatically redirect to default locale
routes.

Resolves #700
  • Loading branch information
rchl authored May 22, 2020
1 parent a853de9 commit 97fabbf
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ module.exports = {
],
rules: {
'arrow-parens': 'off',
'no-console': [
'error', {
allow: ['assert', 'warn', 'error', 'info'],
},
],
'vue/html-closing-bracket-newline': 'off',
'vue/multiline-html-element-content-newline': 'off',
'vue/singleline-html-element-content-newline': 'off'
Expand Down
8 changes: 8 additions & 0 deletions src/helpers/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ exports.makeRoutes = (baseRoutes, {
defaultLocale,
routesNameSeparator,
defaultLocaleRouteNameSuffix,
isNuxtGenerate,
strategy,
parsePages,
pages,
Expand Down Expand Up @@ -118,6 +119,13 @@ exports.makeRoutes = (baseRoutes, {

if (shouldAddPrefix) {
path = `/${locale}${path}`

if (locale === defaultLocale && strategy === STRATEGIES.PREFIX && isNuxtGenerate) {
routes.push({
path: route.path,
redirect: path
})
}
}

localizedRoute.path = path
Expand Down
13 changes: 10 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,26 @@ module.exports = function (userOptions) {

if (options.strategy !== STRATEGIES.NO_PREFIX) {
if (localeCodes.length) {
this.extendRoutes((routes) => {
let isNuxtGenerate = false
const extendRoutes = routes => {
// This import (or more specifically 'vue-template-compiler' in helpers/components.js) needs to
// be required only at build time to avoid problems when 'vue-template-compiler' dependency is
// not available (at runtime, when using nuxt-start).
const { makeRoutes } = require('./helpers/routes')

const localizedRoutes = makeRoutes(routes, {
...options,
pagesDir
pagesDir,
isNuxtGenerate
})
routes.splice(0, routes.length)
routes.unshift(...localizedRoutes)
})
}

// Doesn't seem like we can tell whether we are in nuxt generate from the module so we'll
// take advantage of the 'generate:before' hook to store variable.
this.nuxt.hook('generate:before', () => { isNuxtGenerate = true })
this.nuxt.hook('build:extendRoutes', routes => extendRoutes(routes, isNuxtGenerate))
}
} else if (options.differentDomains) {
// eslint-disable-next-line no-console
Expand Down
44 changes: 43 additions & 1 deletion test/browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ describe(`${browserString} (generate)`, () => {

beforeAll(async () => {
const distDir = resolve(__dirname, 'fixture', 'basic', '.nuxt-generate')
await generate(loadConfig(__dirname, 'basic', { generate: { dir: distDir } }))
await generate(loadConfig(__dirname, 'basic', { generate: { dir: distDir } }, { merge: true }))
server = await startHttpServer(distDir, null, true)
browser = await createBrowser()
})
Expand Down Expand Up @@ -161,6 +161,48 @@ describe(`${browserString} (generate)`, () => {
})
})

describe(`${browserString} (generate, prefix strategy)`, () => {
/** @type {import('playwright-chromium').ChromiumBrowser} */
let browser
let page
let server

beforeAll(async () => {
const distDir = resolve(__dirname, 'fixture', 'basic', '.nuxt-generate')
const overrides = {
generate: { dir: distDir },
i18n: {
strategy: 'prefix',
detectBrowserLanguage: {
fallbackLocale: 'en'
}
}
}
await generate(loadConfig(__dirname, 'basic', overrides, { merge: true }))
server = await startHttpServer(distDir, null, true)
browser = await createBrowser()
})

afterAll(async () => {
if (server) {
await server.destroy()
}
if (browser) {
await browser.close()
}
})

// Issue https://github.com/nuxt-community/nuxt-i18n/issues/700
test('non-prefixed routes are generated for redirect purposes', async () => {
page = await browser.newPage()
await page.goto(server.getUrl('/'))
expect(await (await page.$('body')).textContent()).toContain('locale: en')

await navigate(page, '/about')
expect(await (await page.$('body')).textContent()).toContain('page: About us')
})
})

describe(`${browserString} (generate with detectBrowserLanguage.fallbackLocale)`, () => {
/** @type {import('playwright-chromium').ChromiumBrowser} */
let browser
Expand Down

0 comments on commit 97fabbf

Please sign in to comment.