Skip to content

Commit

Permalink
fix: not work to wait for page transition (#2285)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon authored Aug 1, 2023
1 parent 7341bd2 commit 03b020b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
4 changes: 2 additions & 2 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export default defineNuxtConfig({
}
],
// trailingSlash: true,
// debug: true,
debug: true,
defaultLocale: 'en',
// strategy: 'no_prefix',
// strategy: 'prefix',
Expand All @@ -166,7 +166,7 @@ export default defineNuxtConfig({
}
},
// differentDomains: true,
// skipSettingLocaleOnNavigate: true,
skipSettingLocaleOnNavigate: true,
detectBrowserLanguage: false,
// detectBrowserLanguage: {
// // useCookie: true
Expand Down
10 changes: 7 additions & 3 deletions specs/lang_switcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,19 @@ test('switching', async () => {
expect(await getText(page, '#lang-switcher-with-nuxt-link a')).toMatch('English')
expect(await getText(page, '#set-locale-link-en')).toMatch('English')

// click `en` lang switch with `setLocale`
await page.locator('#set-locale-link-en').click()
await page.waitForTimeout(1000)

// page path
expect(await getData(page, '#home-use-async-data')).toMatchObject({
aboutPath: '/fr/about',
aboutTranslation: 'À propos'
aboutPath: '/about',
aboutTranslation: 'About us'
})
expect(await page.getAttribute('#lang-switcher-with-nuxt-link a', 'href')).toMatch('/')

// current locale
expect(await getText(page, '#lang-switcher-current-locale code')).toEqual('fr')
expect(await getText(page, '#lang-switcher-current-locale code')).toEqual('en')
})

describe('dynamic route parameter', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/plugins/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ export default defineNuxtPlugin(async nuxt => {
{
differentDomains,
skipSettingLocaleOnNavigate,
rootRedirect
rootRedirect,
enableNavigate: true
}
)
}
Expand Down
18 changes: 15 additions & 3 deletions src/runtime/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,15 +370,25 @@ export async function navigate<Context extends NuxtApp = NuxtApp>(
status = 301,
rootRedirect = nuxtI18nOptionsDefault.rootRedirect,
differentDomains = nuxtI18nOptionsDefault.differentDomains,
skipSettingLocaleOnNavigate = nuxtI18nOptionsDefault.skipSettingLocaleOnNavigate
skipSettingLocaleOnNavigate = nuxtI18nOptionsDefault.skipSettingLocaleOnNavigate,
enableNavigate = false
}: {
status?: number
enableNavigate?: boolean
} & Pick<NuxtI18nOptions<Context>, 'skipSettingLocaleOnNavigate' | 'differentDomains' | 'rootRedirect'> = {}
) {
const { i18n, locale, route } = args
let { redirectPath } = args

__DEBUG__ && console.log('navigate options ', status, rootRedirect, differentDomains, skipSettingLocaleOnNavigate)
__DEBUG__ &&
console.log(
'navigate options ',
status,
rootRedirect,
differentDomains,
skipSettingLocaleOnNavigate,
enableNavigate
)
__DEBUG__ && console.log('navigate isSSG', isSSG)

if (route.path === '/' && rootRedirect) {
Expand All @@ -397,7 +407,9 @@ export async function navigate<Context extends NuxtApp = NuxtApp>(
i18n.__pendingLocalePromise = new Promise(resolve => {
i18n.__resolvePendingLocalePromise = resolve
})
return
if (!enableNavigate) {
return
}
}

if (!differentDomains) {
Expand Down

0 comments on commit 03b020b

Please sign in to comment.