From c3c95cd71bb7008d99931dcbbf9ebf54c44b40f9 Mon Sep 17 00:00:00 2001 From: Azer Date: Thu, 4 Mar 2021 16:58:01 +0400 Subject: [PATCH 01/15] fix localePath, translation custom paths. --- src/templates/plugin.routing.js | 38 +++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/src/templates/plugin.routing.js b/src/templates/plugin.routing.js index 03525d5e7..8a77ee2e7 100644 --- a/src/templates/plugin.routing.js +++ b/src/templates/plugin.routing.js @@ -21,7 +21,11 @@ function localePath (route, locale) { return } - return localizedRoute.fullPath + if(typeof localizedRoute === 'string') { + return localizedRoute; + } + + return localizedRoute.fullPath; } function localeRoute (route, locale) { @@ -55,16 +59,28 @@ function localeRoute (route, locale) { const isDefaultLocale = locale === defaultLocale // if route has a path defined but no name, resolve full route using the path const isPrefixed = - // don't prefix default locale - !(isDefaultLocale && [STRATEGIES.PREFIX_EXCEPT_DEFAULT, STRATEGIES.PREFIX_AND_DEFAULT].includes(strategy)) && - // no prefix for any language - !(strategy === STRATEGIES.NO_PREFIX) && - // no prefix for different domains - !i18n.differentDomains - - let path = (isPrefixed ? `/${locale}${route.path}` : route.path) - path = path.replace(/\/+$/, '') + (trailingSlash ? '/' : '') || '/' - localizedRoute.path = path + // don't prefix default locale + !(isDefaultLocale && [STRATEGIES.PREFIX_EXCEPT_DEFAULT, STRATEGIES.PREFIX_AND_DEFAULT].includes(strategy)) && + // no prefix for any language + !(strategy === STRATEGIES.NO_PREFIX) && + // no prefix for different domains + !i18n.differentDomains + let thisRoute = this.router.resolve(route.path).route; + let routeName = this.getRouteBaseName(thisRoute); + + let path; + if(isPrefixed && !routeName) { + path = `/${locale}${route.path}`; + path = path.replace(/\/+$/, '') + (trailingSlash ? '/' : '') || '/' + localizedRoute.path = path + }else { + return localePath.call(this,{ + name: routeName, + params: thisRoute.params, + query:thisRoute.query + },locale); + } + } else { if (!route.name && !route.path) { localizedRoute.name = this.getRouteBaseName() From 4ffc6c9bcd4969e4550adfc96fd4f66078e28961 Mon Sep 17 00:00:00 2001 From: Azer Date: Thu, 4 Mar 2021 16:59:22 +0400 Subject: [PATCH 02/15] fix localePath, translation custom paths. --- src/templates/plugin.routing.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/templates/plugin.routing.js b/src/templates/plugin.routing.js index 8a77ee2e7..4ab46eed1 100644 --- a/src/templates/plugin.routing.js +++ b/src/templates/plugin.routing.js @@ -68,9 +68,9 @@ function localeRoute (route, locale) { let thisRoute = this.router.resolve(route.path).route; let routeName = this.getRouteBaseName(thisRoute); - let path; + if(isPrefixed && !routeName) { - path = `/${locale}${route.path}`; + let path = `/${locale}${route.path}`; path = path.replace(/\/+$/, '') + (trailingSlash ? '/' : '') || '/' localizedRoute.path = path }else { From cf5dea7242edaf496555cfc072f36157b5afe0ff Mon Sep 17 00:00:00 2001 From: Azer Date: Thu, 4 Mar 2021 18:10:33 +0400 Subject: [PATCH 03/15] fix localePath, translation custom paths. --- src/templates/plugin.routing.js | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/templates/plugin.routing.js b/src/templates/plugin.routing.js index 4ab46eed1..54a3a4792 100644 --- a/src/templates/plugin.routing.js +++ b/src/templates/plugin.routing.js @@ -21,11 +21,11 @@ function localePath (route, locale) { return } - if(typeof localizedRoute === 'string') { - return localizedRoute; + if (typeof localizedRoute === 'string') { + return localizedRoute } - return localizedRoute.fullPath; + return localizedRoute.fullPath } function localeRoute (route, locale) { @@ -65,22 +65,17 @@ function localeRoute (route, locale) { !(strategy === STRATEGIES.NO_PREFIX) && // no prefix for different domains !i18n.differentDomains - let thisRoute = this.router.resolve(route.path).route; - let routeName = this.getRouteBaseName(thisRoute); + const thisRoute = this.router.resolve(route.path).route + const routeName = this.getRouteBaseName(thisRoute) - - if(isPrefixed && !routeName) { - let path = `/${locale}${route.path}`; + if (isPrefixed && !routeName) { + let path = `/${locale}${route.path}` path = path.replace(/\/+$/, '') + (trailingSlash ? '/' : '') || '/' localizedRoute.path = path - }else { - return localePath.call(this,{ - name: routeName, - params: thisRoute.params, - query:thisRoute.query - },locale); + } else { + localizedRoute.name = getLocaleRouteName(routeName, locale) + return this.router.resolve(localizedRoute).route } - } else { if (!route.name && !route.path) { localizedRoute.name = this.getRouteBaseName() From 2a3cc9cd5d52242eab1a5f32f37a222143972bcc Mon Sep 17 00:00:00 2001 From: Azer Date: Thu, 4 Mar 2021 18:12:47 +0400 Subject: [PATCH 04/15] fix localePath, translation custom paths. --- src/templates/plugin.routing.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/templates/plugin.routing.js b/src/templates/plugin.routing.js index 54a3a4792..bb0efcac6 100644 --- a/src/templates/plugin.routing.js +++ b/src/templates/plugin.routing.js @@ -74,6 +74,8 @@ function localeRoute (route, locale) { localizedRoute.path = path } else { localizedRoute.name = getLocaleRouteName(routeName, locale) + localizedRoute.params = thisRoute.params + localizedRoute.query = thisRoute.query return this.router.resolve(localizedRoute).route } } else { From 1fcae845f574e49432dc15f6b0e6eb8eb910ef15 Mon Sep 17 00:00:00 2001 From: Azer Date: Thu, 4 Mar 2021 18:22:24 +0400 Subject: [PATCH 05/15] fix localePath, translation custom paths. --- src/templates/plugin.routing.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/templates/plugin.routing.js b/src/templates/plugin.routing.js index bb0efcac6..61904857e 100644 --- a/src/templates/plugin.routing.js +++ b/src/templates/plugin.routing.js @@ -73,6 +73,7 @@ function localeRoute (route, locale) { path = path.replace(/\/+$/, '') + (trailingSlash ? '/' : '') || '/' localizedRoute.path = path } else { + localizedRoute.path = route.path.replace(/\/+$/, '') + (trailingSlash ? '/' : '') || '/' localizedRoute.name = getLocaleRouteName(routeName, locale) localizedRoute.params = thisRoute.params localizedRoute.query = thisRoute.query From 41137aa2bef3b77f171f00eabd48161ba533585d Mon Sep 17 00:00:00 2001 From: Azer Date: Thu, 4 Mar 2021 18:25:39 +0400 Subject: [PATCH 06/15] fix localePath, translation custom paths. --- src/templates/plugin.routing.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/templates/plugin.routing.js b/src/templates/plugin.routing.js index 61904857e..ce11333db 100644 --- a/src/templates/plugin.routing.js +++ b/src/templates/plugin.routing.js @@ -21,10 +21,6 @@ function localePath (route, locale) { return } - if (typeof localizedRoute === 'string') { - return localizedRoute - } - return localizedRoute.fullPath } From b5e289177fc63a93489d23eae3d77a89548c7a98 Mon Sep 17 00:00:00 2001 From: Azer Mammadov Date: Fri, 5 Mar 2021 00:54:00 +0400 Subject: [PATCH 07/15] fix 404 pages --- src/templates/plugin.routing.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/templates/plugin.routing.js b/src/templates/plugin.routing.js index ce11333db..06f180a80 100644 --- a/src/templates/plugin.routing.js +++ b/src/templates/plugin.routing.js @@ -70,10 +70,13 @@ function localeRoute (route, locale) { localizedRoute.path = path } else { localizedRoute.path = route.path.replace(/\/+$/, '') + (trailingSlash ? '/' : '') || '/' - localizedRoute.name = getLocaleRouteName(routeName, locale) - localizedRoute.params = thisRoute.params - localizedRoute.query = thisRoute.query - return this.router.resolve(localizedRoute).route + if(routeName) { + localizedRoute.name = getLocaleRouteName(routeName, locale) + localizedRoute.params = thisRoute.params + localizedRoute.query = thisRoute.query + localizedRoute.hash = thisRoute.hash + return this.router.resolve(localizedRoute).route + } } } else { if (!route.name && !route.path) { From 7f831a3618869a60a29c7e06a0eac3095a40b279 Mon Sep 17 00:00:00 2001 From: Azer Mammadov Date: Fri, 5 Mar 2021 01:09:12 +0400 Subject: [PATCH 08/15] lint --- src/templates/plugin.routing.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/templates/plugin.routing.js b/src/templates/plugin.routing.js index 06f180a80..2a41b3231 100644 --- a/src/templates/plugin.routing.js +++ b/src/templates/plugin.routing.js @@ -70,7 +70,7 @@ function localeRoute (route, locale) { localizedRoute.path = path } else { localizedRoute.path = route.path.replace(/\/+$/, '') + (trailingSlash ? '/' : '') || '/' - if(routeName) { + if (routeName) { localizedRoute.name = getLocaleRouteName(routeName, locale) localizedRoute.params = thisRoute.params localizedRoute.query = thisRoute.query From 2fde8e83d3ded2ea6e7475b80c32781a0a71e3fe Mon Sep 17 00:00:00 2001 From: Azer Mammadov Date: Fri, 5 Mar 2021 02:35:35 +0400 Subject: [PATCH 09/15] refactor code --- src/templates/plugin.routing.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/templates/plugin.routing.js b/src/templates/plugin.routing.js index 2a41b3231..f6ba8057a 100644 --- a/src/templates/plugin.routing.js +++ b/src/templates/plugin.routing.js @@ -63,19 +63,20 @@ function localeRoute (route, locale) { !i18n.differentDomains const thisRoute = this.router.resolve(route.path).route const routeName = this.getRouteBaseName(thisRoute) - if (isPrefixed && !routeName) { let path = `/${locale}${route.path}` - path = path.replace(/\/+$/, '') + (trailingSlash ? '/' : '') || '/' + path = getReplacedPath(path) localizedRoute.path = path } else { - localizedRoute.path = route.path.replace(/\/+$/, '') + (trailingSlash ? '/' : '') || '/' + localizedRoute.path = getReplacedPath(route.path) if (routeName) { - localizedRoute.name = getLocaleRouteName(routeName, locale) - localizedRoute.params = thisRoute.params - localizedRoute.query = thisRoute.query - localizedRoute.hash = thisRoute.hash - return this.router.resolve(localizedRoute).route + return this.router.resolve({ + name: getLocaleRouteName(routeName, locale), + params: thisRoute.params, + query: thisRoute.query, + hash: thisRoute.hash, + path: localizedRoute.path + }).route } } } else { @@ -94,6 +95,10 @@ function localeRoute (route, locale) { return this.router.resolve(localizedRoute).route } +function getReplacedPath (path) { + return path.replace(/\/+$/, '') + (trailingSlash ? '/' : '') || '/' +} + function switchLocalePath (locale) { const name = this.getRouteBaseName() if (!name) { From 7e9a9280b6709fba6bec7915d21aa55958f440b7 Mon Sep 17 00:00:00 2001 From: Azer Date: Fri, 5 Mar 2021 10:05:21 +0400 Subject: [PATCH 10/15] try to fix indents --- src/templates/plugin.routing.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/templates/plugin.routing.js b/src/templates/plugin.routing.js index f6ba8057a..53df91c8d 100644 --- a/src/templates/plugin.routing.js +++ b/src/templates/plugin.routing.js @@ -55,12 +55,12 @@ function localeRoute (route, locale) { const isDefaultLocale = locale === defaultLocale // if route has a path defined but no name, resolve full route using the path const isPrefixed = - // don't prefix default locale - !(isDefaultLocale && [STRATEGIES.PREFIX_EXCEPT_DEFAULT, STRATEGIES.PREFIX_AND_DEFAULT].includes(strategy)) && - // no prefix for any language - !(strategy === STRATEGIES.NO_PREFIX) && - // no prefix for different domains - !i18n.differentDomains + // don't prefix default locale + !(isDefaultLocale && [STRATEGIES.PREFIX_EXCEPT_DEFAULT, STRATEGIES.PREFIX_AND_DEFAULT].includes(strategy)) && + // no prefix for any language + !(strategy === STRATEGIES.NO_PREFIX) && + // no prefix for different domains + !i18n.differentDomains const thisRoute = this.router.resolve(route.path).route const routeName = this.getRouteBaseName(thisRoute) if (isPrefixed && !routeName) { From 6187d0c554ee7d816fc7ef38fba2aada91eb77c7 Mon Sep 17 00:00:00 2001 From: Azer Mammadov Date: Sat, 6 Mar 2021 09:38:11 +0400 Subject: [PATCH 11/15] refactor code --- src/templates/plugin.routing.js | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/src/templates/plugin.routing.js b/src/templates/plugin.routing.js index f6ba8057a..e171fd376 100644 --- a/src/templates/plugin.routing.js +++ b/src/templates/plugin.routing.js @@ -49,7 +49,7 @@ function localeRoute (route, locale) { } } - const localizedRoute = Object.assign({}, route) + let localizedRoute = Object.assign({}, route) if (route.path && !route.name) { const isDefaultLocale = locale === defaultLocale @@ -63,20 +63,18 @@ function localeRoute (route, locale) { !i18n.differentDomains const thisRoute = this.router.resolve(route.path).route const routeName = this.getRouteBaseName(thisRoute) + let path = route.path if (isPrefixed && !routeName) { - let path = `/${locale}${route.path}` - path = getReplacedPath(path) - localizedRoute.path = path - } else { - localizedRoute.path = getReplacedPath(route.path) - if (routeName) { - return this.router.resolve({ - name: getLocaleRouteName(routeName, locale), - params: thisRoute.params, - query: thisRoute.query, - hash: thisRoute.hash, - path: localizedRoute.path - }).route + path = `/${locale}${path}` + } + localizedRoute.path = path.replace(/\/+$/, '') + (trailingSlash ? '/' : '') || '/' + if (routeName) { + localizedRoute = { + name: getLocaleRouteName(routeName, locale), + params: thisRoute.params, + query: thisRoute.query, + hash: thisRoute.hash, + path: localizedRoute.path } } } else { @@ -95,10 +93,6 @@ function localeRoute (route, locale) { return this.router.resolve(localizedRoute).route } -function getReplacedPath (path) { - return path.replace(/\/+$/, '') + (trailingSlash ? '/' : '') || '/' -} - function switchLocalePath (locale) { const name = this.getRouteBaseName() if (!name) { From 5e01b1888683f79f791ac5aba695250ffcaea45f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Ch=C5=82odnicki?= Date: Sat, 6 Mar 2021 21:36:43 +0100 Subject: [PATCH 12/15] refactor and add some tests --- src/templates/plugin.routing.js | 30 ++++++++++++++++-------------- test/browser.test.js | 8 ++++++++ test/module.test.js | 14 ++++++++++++++ 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/src/templates/plugin.routing.js b/src/templates/plugin.routing.js index 881ee18eb..a4d77ea12 100644 --- a/src/templates/plugin.routing.js +++ b/src/templates/plugin.routing.js @@ -61,21 +61,21 @@ function localeRoute (route, locale) { !(strategy === STRATEGIES.NO_PREFIX) && // no prefix for different domains !i18n.differentDomains - const thisRoute = this.router.resolve(route.path).route - const routeName = this.getRouteBaseName(thisRoute) - let path = route.path - if (isPrefixed && !routeName) { - path = `/${locale}${path}` - } - localizedRoute.path = path.replace(/\/+$/, '') + (trailingSlash ? '/' : '') || '/' - if (routeName) { + const resolvedRoute = this.router.resolve(route.path).route + const resolvedRouteName = this.getRouteBaseName(resolvedRoute) + if (resolvedRouteName) { localizedRoute = { - name: getLocaleRouteName(routeName, locale), - params: thisRoute.params, - query: thisRoute.query, - hash: thisRoute.hash, - path: localizedRoute.path + name: getLocaleRouteName(resolvedRouteName, locale), + params: resolvedRoute.params, + query: resolvedRoute.query, + hash: resolvedRoute.hash, + path: route.path + } + } else { + if (isPrefixed) { + localizedRoute.path = `/${locale}${route.path}` } + localizedRoute.path = localizedRoute.path.replace(/\/+$/, '') + (trailingSlash ? '/' : '') || '/' } } else { if (!route.name && !route.path) { @@ -90,7 +90,9 @@ function localeRoute (route, locale) { } } - return this.router.resolve(localizedRoute).route + const resolved = this.router.resolve(localizedRoute).route + // console.info({ resolved }) + return resolved } function switchLocalePath (locale) { diff --git a/test/browser.test.js b/test/browser.test.js index 77715ec48..02bbe041b 100644 --- a/test/browser.test.js +++ b/test/browser.test.js @@ -637,6 +637,14 @@ describe(`${browserString} (SPA)`, () => { expect(await (await page.$('body'))?.textContent()).toContain('page could not be found') expect(await getRouteFullPath(page)).toBe(path) }) + + test('preserves the URL on 404 page with non-default locale', async () => { + const path = '/nopage?a#h' + page = await browser.newPage({ locale: 'fr' }) + await page.goto(url(path)) + expect(await (await page.$('body'))?.textContent()).toContain('page could not be found') + expect(await getRouteFullPath(page)).toBe(`/fr${path}`) + }) }) describe(`${browserString} (SPA with router in hash mode)`, () => { diff --git a/test/module.test.js b/test/module.test.js index e3a337775..d96538d2f 100644 --- a/test/module.test.js +++ b/test/module.test.js @@ -1035,6 +1035,20 @@ describe('prefix_and_default strategy', () => { expect(window.$nuxt.localeRoute('/simple', 'fr')).toMatchObject({ name: 'simple___fr', fullPath: '/fr/simple' }) }) + test('localeRoute returns customized localized route (by route path)', async () => { + const window = await nuxt.renderAndGetWindow(url('/')) + // Prefer unprefixed path for default locale: + expect(window.$nuxt.localeRoute('/about-us', 'en')).toMatchObject({ name: 'about___en___default', fullPath: '/about-us' }) + expect(window.$nuxt.localeRoute('/en/about-us', 'en')).toMatchObject({ name: 'about___en___default', fullPath: '/about-us' }) + expect(window.$nuxt.localeRoute('/about-us', 'fr')).toMatchObject({ name: 'about___fr', fullPath: '/fr/a-propos' }) + expect(window.$nuxt.localeRoute('/about-us?q=1#hash', 'en')).toMatchObject({ + name: 'about___en___default', + fullPath: '/about-us?q=1#hash', + query: { q: '1' }, + hash: '#hash' + }) + }) + test('canonical SEO link is added to prefixed default locale', async () => { const html = await get('/en') const dom = getDom(html) From f621f7418ceaf273002411739172dc810919c625 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Ch=C5=82odnicki?= Date: Sat, 6 Mar 2021 21:48:38 +0100 Subject: [PATCH 13/15] remove debug code --- src/templates/plugin.routing.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/templates/plugin.routing.js b/src/templates/plugin.routing.js index a4d77ea12..f99d6ddd7 100644 --- a/src/templates/plugin.routing.js +++ b/src/templates/plugin.routing.js @@ -90,9 +90,7 @@ function localeRoute (route, locale) { } } - const resolved = this.router.resolve(localizedRoute).route - // console.info({ resolved }) - return resolved + return this.router.resolve(localizedRoute).route } function switchLocalePath (locale) { From d53a8c4d5c8cabcfa0f4f7454a624fed5bd081ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Ch=C5=82odnicki?= Date: Sat, 6 Mar 2021 22:19:55 +0100 Subject: [PATCH 14/15] The path should auto-resolve --- src/templates/plugin.routing.js | 3 +-- test/fixture/basic/assets/main.css | 8 ++++++++ 2 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 test/fixture/basic/assets/main.css diff --git a/src/templates/plugin.routing.js b/src/templates/plugin.routing.js index f99d6ddd7..f7bbdecf7 100644 --- a/src/templates/plugin.routing.js +++ b/src/templates/plugin.routing.js @@ -68,8 +68,7 @@ function localeRoute (route, locale) { name: getLocaleRouteName(resolvedRouteName, locale), params: resolvedRoute.params, query: resolvedRoute.query, - hash: resolvedRoute.hash, - path: route.path + hash: resolvedRoute.hash } } else { if (isPrefixed) { diff --git a/test/fixture/basic/assets/main.css b/test/fixture/basic/assets/main.css new file mode 100644 index 000000000..7e6bd808c --- /dev/null +++ b/test/fixture/basic/assets/main.css @@ -0,0 +1,8 @@ +.page-enter-active, +.page-leave-active { + transition: opacity 0.5s; +} +.page-enter, +.page-leave-to { + opacity: 0; +} From 64a1a3c1fcb589f005b8d6c427215e8be2ddf267 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Ch=C5=82odnicki?= Date: Sat, 6 Mar 2021 22:21:20 +0100 Subject: [PATCH 15/15] accidental commit --- test/fixture/basic/assets/main.css | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 test/fixture/basic/assets/main.css diff --git a/test/fixture/basic/assets/main.css b/test/fixture/basic/assets/main.css deleted file mode 100644 index 7e6bd808c..000000000 --- a/test/fixture/basic/assets/main.css +++ /dev/null @@ -1,8 +0,0 @@ -.page-enter-active, -.page-leave-active { - transition: opacity 0.5s; -} -.page-enter, -.page-leave-to { - opacity: 0; -}