Skip to content

Commit 426af76

Browse files
authored
fix: infinite redirect on URL with special characters (#1472)
1 parent a5f0188 commit 426af76

File tree

5 files changed

+23
-24
lines changed

5 files changed

+23
-24
lines changed

src/templates/plugin.main.js

+5-20
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,11 @@
11
import Vue from 'vue'
22
import VueI18n from 'vue-i18n'
3+
import { isEqual as isURLEqual, joinURL } from '~i18n-ufo'
4+
import { klona } from '~i18n-klona'
35
import { nuxtI18nHead } from './head-meta'
46
import { Constants, nuxtOptions, options } from './options'
5-
import {
6-
createLocaleFromRouteGetter,
7-
getLocaleCookie,
8-
getLocaleDomain,
9-
getLocalesRegex,
10-
matchBrowserLocale,
11-
parseAcceptLanguage,
12-
setLocaleCookie
13-
} from './utils-common'
14-
import {
15-
loadLanguageAsync,
16-
resolveBaseUrl,
17-
registerStore,
18-
mergeAdditionalMessages
19-
} from './plugin.utils'
20-
// @ts-ignore
21-
import { joinURL } from '~i18n-ufo'
22-
// @ts-ignore
23-
import { klona } from '~i18n-klona'
7+
import { createLocaleFromRouteGetter, getLocaleCookie, getLocaleDomain, getLocalesRegex, matchBrowserLocale, parseAcceptLanguage, setLocaleCookie } from './utils-common'
8+
import { loadLanguageAsync, resolveBaseUrl, registerStore, mergeAdditionalMessages } from './plugin.utils'
249

2510
Vue.use(VueI18n)
2611

@@ -165,7 +150,7 @@ export default async (context) => {
165150
// The current route could be 404 in which case attempt to find matching route using the full path since
166151
// "switchLocalePath" can only find routes if the current route exists.
167152
const routePath = app.switchLocalePath(newLocale) || app.localePath(route.fullPath, newLocale)
168-
if (routePath && routePath !== route.fullPath && !routePath.startsWith('//')) {
153+
if (routePath && !isURLEqual(routePath, route.fullPath) && !routePath.startsWith('//')) {
169154
redirectPath = routePath
170155
}
171156
}

src/templates/plugin.routing.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import './middleware'
22
import Vue from 'vue'
3+
import { withoutTrailingSlash, withTrailingSlash } from '~i18n-ufo'
34
import { Constants, nuxtOptions, options } from './options'
45
import { getDomainFromLocale } from './plugin.utils'
5-
// @ts-ignore
6-
import { withoutTrailingSlash, withTrailingSlash } from '~i18n-ufo'
76

87
/**
98
* @this {import('../../types/internal').PluginProxy}

src/templates/plugin.utils.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import isHTTPS from 'is-https'
2+
import { hasProtocol } from '~i18n-ufo'
23
import { localeMessages, options } from './options'
34
import { formatMessage } from './utils-common'
4-
// @ts-ignore
5-
import { hasProtocol } from '~i18n-ufo'
65

76
/** @typedef {import('../../types/internal').ResolvedOptions} ResolvedOptions */
87

test/module.test.js

+10
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,16 @@ describe('prefix_and_default strategy', () => {
11081108
expect(response.headers.location).toBe('/')
11091109
})
11101110

1111+
test('does not redirect when only path encoding differs', async () => {
1112+
const requestOptions = {
1113+
followRedirect: false,
1114+
resolveWithFullResponse: true,
1115+
simple: false // Don't reject on non-2xx response
1116+
}
1117+
const response = await get('/posts/a-&', requestOptions)
1118+
expect(response.statusCode).toBe(200)
1119+
})
1120+
11111121
test('localeRoute returns localized route (by route name)', async () => {
11121122
const window = await nuxt.renderAndGetWindow(url('/'))
11131123
expect(window.$nuxt.localeRoute('index', 'en')).toMatchObject({ name: 'index___en___default', fullPath: '/' })

tsconfig.json

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
"noEmit": true,
1717
"baseUrl": ".",
1818
"paths": {
19+
"~i18n-klona": [
20+
"node_modules/klona"
21+
],
22+
"~i18n-ufo": [
23+
"node_modules/ufo"
24+
],
1925
"~/*": [
2026
"./*",
2127
],

0 commit comments

Comments
 (0)