Skip to content

Commit

Permalink
fix: don't do browser language detection during Nuxt generate (nuxt-m…
Browse files Browse the repository at this point in the history
…odules#718)

During generate there is no point in guessing the browser language
as it's not the target's user language anyway.

Co-authored-by: Rafal Chlodnicki
  • Loading branch information
rchl authored May 19, 2020
1 parent d2f4bff commit f1c5aca
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/templates/plugin.main.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ export default async (context) => {
}

const doDetectBrowserLanguage = () => {
// Browser detection is ignored if it is a nuxt generate.
if (process.static && process.server) {
return false
}

const { alwaysRedirect, fallbackLocale } = detectBrowserLanguage

let matchedLocale
Expand Down
53 changes: 53 additions & 0 deletions test/browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,59 @@ describe(`${browserString} (generate)`, () => {
})
})

describe(`${browserString} (generate with detectBrowserLanguage.fallbackLocale)`, () => {
let browser
let page
let port
// Local method that overrides imported one.
let url

beforeAll(async () => {
const distDir = resolve(__dirname, 'fixture', 'basic', '.nuxt-generate')

const overrides = {
generate: { dir: distDir },
i18n: {
detectBrowserLanguage: {
fallbackLocale: 'en'
}
}
}

await generate(loadConfig(__dirname, 'basic', overrides, { merge: true }))

port = await getPort()
url = path => `http://localhost:${port}${path}`

browser = await createBrowser(browserString, {
folder: distDir,
staticServer: {
folder: distDir,
port
},
extendPage (page) {
return {
navigate: createNavigator(page)
}
}
})
})

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

test('generates pages in all locales', async () => {
page = await browser.page(url('/'))
expect(await page.getText('body')).toContain('locale: en')

await page.navigate('/fr')
expect(await page.getText('body')).toContain('locale: fr')
})
})

describe(`${browserString} (no fallbackLocale, browser language not supported)`, () => {
let nuxt
let browser
Expand Down
36 changes: 35 additions & 1 deletion test/module.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { setup, loadConfig, get, url } from '@nuxtjs/module-test-utils'
import { resolve } from 'path'
import { readFileSync } from 'fs'
import { generate, setup, loadConfig, get, url } from '@nuxtjs/module-test-utils'
import { JSDOM } from 'jsdom'

import { getSeoTags } from './utils'
Expand Down Expand Up @@ -1166,3 +1168,35 @@ describe('prefix + detectBrowserLanguage + alwaysRedirect', () => {
expect(dom.querySelector('#current-locale').textContent).toBe('locale: fr')
})
})

describe('generate with detectBrowserLanguage.fallbackLocale', () => {
const distDir = resolve(__dirname, 'fixture', 'basic', '.nuxt-generate')

beforeAll(async () => {
const overrides = {
generate: { dir: distDir },
i18n: {
detectBrowserLanguage: {
fallbackLocale: 'en'
}
}
}

await generate(loadConfig(__dirname, 'basic', overrides, { merge: true }))
})

test('pre-renders all locales', () => {
let dom
let contents

contents = readFileSync(resolve(distDir, 'index.html'), 'utf-8')
dom = getDom(contents)
expect(dom.querySelector('#current-page')).toBeDefined()
expect(dom.querySelector('#current-page').textContent).toBe('page: Homepage')

contents = readFileSync(resolve(distDir, 'fr/index.html'), 'utf-8')
dom = getDom(contents)
expect(dom.querySelector('#current-page')).toBeDefined()
expect(dom.querySelector('#current-page').textContent).toBe('page: Accueil')
})
})

0 comments on commit f1c5aca

Please sign in to comment.