Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server, vue-app): address encoding issues with query params #8748

Merged
merged 13 commits into from
Feb 4, 2021
Merged
3 changes: 2 additions & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"pify": "^5.0.0",
"serve-placeholder": "^1.2.3",
"serve-static": "^1.14.1",
"server-destroy": "^1.0.1"
"server-destroy": "^1.0.1",
"ufo": "^0.5.4"
},
"publishConfig": {
"access": "public"
Expand Down
3 changes: 2 additions & 1 deletion packages/server/src/middleware/nuxt.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import generateETag from 'etag'
import fresh from 'fresh'
import consola from 'consola'
import { normalizeURL } from 'ufo'

import { getContext, TARGETS } from '@nuxt/utils'

Expand All @@ -9,7 +10,7 @@ export default ({ options, nuxt, renderRoute, resources }) => async function nux
const context = getContext(req, res)

try {
const url = decodeURI(req.url)
const url = normalizeURL(req.url)
res.statusCode = 200
const result = await renderRoute(url, context)

Expand Down
14 changes: 1 addition & 13 deletions packages/vue-app/template/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,6 @@ export const routerOptions = {
fallback: <%= router.fallback %>
}

function decodeObj(obj) {
for (const key in obj) {
if (typeof obj[key] === 'string') {
obj[key] = decode(obj[key])
}
}
}

export function createRouter (ssrContext, config) {
const base = (config.app && config.app.basePath) || routerOptions.base
const router = new Router({ ...routerOptions, base })
Expand All @@ -124,11 +116,7 @@ export function createRouter (ssrContext, config) {
if (typeof to === 'string') {
to = normalizeURL(to)
}
const r = resolve(to, current, append)
if (r && r.resolved && r.resolved.query) {
decodeObj(r.resolved.query)
}
return r
return resolve(to, current, append)
}

return router
Expand Down
17 changes: 16 additions & 1 deletion test/dev/encoding.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('encoding', () => {
})

test('/ö/dynamic?q=food,coffee (encodeURIComponent)', async () => {
const { body: response } = await rp(url('/ö/dynamic?q=food%252Ccoffee'))
const { body: response } = await rp(url('/ö/dynamic?q=food,coffee'))
pi0 marked this conversation as resolved.
Show resolved Hide resolved

expect(response).toContain('food,coffee')
})
Expand All @@ -33,6 +33,21 @@ describe('encoding', () => {
expect(response).toContain('About')
})

test('query params', async () => {
const queryStrings = {
'?email=some%20email.com': { email: 'some email.com' },
'?str=%26&str2=%2526': { str: '&', str2: '%26' }
}
for (const [param, result] of Object.entries(queryStrings)) {
const { body: response } = await rp(url('/ö/dynamic/test') + param)
expect(response).toContain(
JSON.stringify(result)
.replace(/&/g, '&amp;')
.replace(/"/g, '&quot;')
)
}
})

// Close server and ask nuxt to stop listening to file changes
afterAll(async () => {
await nuxt.close()
Expand Down