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: normalize routes and decode resolved query #8430

Merged
merged 29 commits into from Dec 6, 2020
Merged

fix: normalize routes and decode resolved query #8430

merged 29 commits into from Dec 6, 2020

Conversation

pi0
Copy link
Member

@pi0 pi0 commented Dec 2, 2020

Types of changes

  • Bug fix (a non-breaking change which fixes an issue)
  • New feature (a non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Description

Since vue-router@2.14.9 makes it mandatory to use encodeURI when using RouterLink, NuxtLink and router.push, we introduced a fix by automatically encoding URIs. But seems it makes regression when a query param (and probably path param) is encoded and not being properly decoded.

Changes:

  • Use normalizeURL from @nuxt/ufo
  • Decode route.resolved.query with decodeURIComponent
  • Encode redirect path with normalizeURL since http headers should be encoded

Foot note: Case of #8429 only happens when manually using encodeURIComponent on query parameters instead of encodeURI and at least chrome auto fixes this. (still this is a regression that we should fixed)

record

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly. (PR: #)
  • I have added tests to cover my changes (if not applicable, please state why)
  • All new and existing tests are passing.

@pi0 pi0 changed the title fix: decode vue-router query params fix: don't encode with router.resolve Dec 2, 2020
@ydnar
Copy link

ydnar commented Dec 3, 2020

Note: we see this issue when a browser visits the encoded URL (via typing, for instance), without any router push.

@codecov-io
Copy link

codecov-io commented Dec 3, 2020

Codecov Report

Merging #8430 (1da49d5) into 2.x (e9f380c) will decrease coverage by 0.03%.
The diff coverage is 45.45%.

Impacted file tree graph

@@            Coverage Diff             @@
##              2.x    #8430      +/-   ##
==========================================
- Coverage   68.27%   68.23%   -0.04%     
==========================================
  Files          91       91              
  Lines        3902     3904       +2     
  Branches     1066     1066              
==========================================
  Hits         2664     2664              
- Misses       1004     1005       +1     
- Partials      234      235       +1     
Flag Coverage Δ
unittests 68.23% <45.45%> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
packages/vue-renderer/src/renderer.js 0.00% <0.00%> (ø)
packages/webpack/src/config/base.js 64.45% <ø> (ø)
packages/server/src/server.js 79.39% <28.57%> (-0.86%) ⬇️
packages/config/src/options.js 94.58% <100.00%> (ø)
packages/utils/src/route.js 93.16% <100.00%> (-0.05%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e9f380c...1da49d5. Read the comment docs.

@pi0 pi0 changed the title fix: don't encode with router.resolve fix: route.query is not decoded Dec 3, 2020
@pi0 pi0 changed the base branch from dev to 2.x December 3, 2020 18:54
@pi0 pi0 changed the title fix: route.query is not decoded fix: route encoding improvements Dec 4, 2020
@pi0 pi0 marked this pull request as ready for review December 4, 2020 12:31
@pi0 pi0 requested review from danielroe, Atinux and a team December 4, 2020 12:34
@pi0 pi0 requested a review from clarkdo December 4, 2020 13:14
@@ -280,3 +279,11 @@ export const promisifyRoute = function promisifyRoute (fn, ...args) {
}
return promise
}

export function safeEncodeComponent (str) {
return /%[0-9a-fA-F]{2}/.test(str) ? str : encodeURI(str)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if url includes %.. as path or parameter ?

Like: /rate/%30?max=%90

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/%[0-9a-fA-F]{2}/.test('/rate/%30?max=%90') > true

In this case we avoid encoding

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if url also includes special characters, we’ll skip encoding, is that expected?

Like: /rate/%30?max=%90&name= тест

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last update: /rate/%30?max=%90&name=%20%D1%82%D0%B5%D1%81%D1%82

Copy link
Member

@danielroe danielroe Dec 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any merit in using something like this as our util - not reinventing url parsing?

function safeEncode (str) {
  const { pathname, search } = new URL(`http://localhost${str}`)
  return `${pathname}${search}`
}

safeEncode('/rate/%30?max=%90&name= тест')
// "/rate/%30?max=%90&name=%20%D1%82%D0%B5%D1%81%D1%82"

Copy link
Member Author

@pi0 pi0 Dec 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed we can but it costs bundle size for rest of the browsers and actually it is not enough to make url utils (for joining or ensure there is no leading slash for example) -- not specific to this PR's case

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact IE11 works fine for me - just have to wrap in brackets.
Screenshot from 2020-12-04 16-23-20

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough :) Switching to URL

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is just one downside that it is implicit it is full url or URI :))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for bringing it up. Working on util before merge ;)

@pi0 pi0 changed the title fix: route encoding improvements fix: normalize route urls Dec 6, 2020
@pi0 pi0 changed the title fix: normalize route urls fix: normalize routes and decode resolved query Dec 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Query string parameters are not URI-decoded in route.query
5 participants