Skip to content

Commit

Permalink
fix: infinite redirect on URL with special characters (#1472)
Browse files Browse the repository at this point in the history
  • Loading branch information
rchl committed Aug 1, 2022
1 parent a5f0188 commit 426af76
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 24 deletions.
25 changes: 5 additions & 20 deletions src/templates/plugin.main.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
import Vue from 'vue'
import VueI18n from 'vue-i18n'
import { isEqual as isURLEqual, joinURL } from '~i18n-ufo'
import { klona } from '~i18n-klona'
import { nuxtI18nHead } from './head-meta'
import { Constants, nuxtOptions, options } from './options'
import {
createLocaleFromRouteGetter,
getLocaleCookie,
getLocaleDomain,
getLocalesRegex,
matchBrowserLocale,
parseAcceptLanguage,
setLocaleCookie
} from './utils-common'
import {
loadLanguageAsync,
resolveBaseUrl,
registerStore,
mergeAdditionalMessages
} from './plugin.utils'
// @ts-ignore
import { joinURL } from '~i18n-ufo'
// @ts-ignore
import { klona } from '~i18n-klona'
import { createLocaleFromRouteGetter, getLocaleCookie, getLocaleDomain, getLocalesRegex, matchBrowserLocale, parseAcceptLanguage, setLocaleCookie } from './utils-common'
import { loadLanguageAsync, resolveBaseUrl, registerStore, mergeAdditionalMessages } from './plugin.utils'

Vue.use(VueI18n)

Expand Down Expand Up @@ -165,7 +150,7 @@ export default async (context) => {
// The current route could be 404 in which case attempt to find matching route using the full path since
// "switchLocalePath" can only find routes if the current route exists.
const routePath = app.switchLocalePath(newLocale) || app.localePath(route.fullPath, newLocale)
if (routePath && routePath !== route.fullPath && !routePath.startsWith('//')) {
if (routePath && !isURLEqual(routePath, route.fullPath) && !routePath.startsWith('//')) {
redirectPath = routePath
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/templates/plugin.routing.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import './middleware'
import Vue from 'vue'
import { withoutTrailingSlash, withTrailingSlash } from '~i18n-ufo'
import { Constants, nuxtOptions, options } from './options'
import { getDomainFromLocale } from './plugin.utils'
// @ts-ignore
import { withoutTrailingSlash, withTrailingSlash } from '~i18n-ufo'

/**
* @this {import('../../types/internal').PluginProxy}
Expand Down
3 changes: 1 addition & 2 deletions src/templates/plugin.utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import isHTTPS from 'is-https'
import { hasProtocol } from '~i18n-ufo'
import { localeMessages, options } from './options'
import { formatMessage } from './utils-common'
// @ts-ignore
import { hasProtocol } from '~i18n-ufo'

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

Expand Down
10 changes: 10 additions & 0 deletions test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,16 @@ describe('prefix_and_default strategy', () => {
expect(response.headers.location).toBe('/')
})

test('does not redirect when only path encoding differs', async () => {
const requestOptions = {
followRedirect: false,
resolveWithFullResponse: true,
simple: false // Don't reject on non-2xx response
}
const response = await get('/posts/a-&', requestOptions)
expect(response.statusCode).toBe(200)
})

test('localeRoute returns localized route (by route name)', async () => {
const window = await nuxt.renderAndGetWindow(url('/'))
expect(window.$nuxt.localeRoute('index', 'en')).toMatchObject({ name: 'index___en___default', fullPath: '/' })
Expand Down
6 changes: 6 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
"noEmit": true,
"baseUrl": ".",
"paths": {
"~i18n-klona": [
"node_modules/klona"
],
"~i18n-ufo": [
"node_modules/ufo"
],
"~/*": [
"./*",
],
Expand Down

0 comments on commit 426af76

Please sign in to comment.