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

fix: don't leave out non-prefixed routes for generate + prefix strategy #726

Merged
merged 1 commit into from
May 22, 2020
Merged
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
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