|
| 1 | +# URL Encoding |
| 2 | + |
| 3 | +::: warning Experimental |
| 4 | +This page describes the [Experimental Router](./router-resolver.md). The behavior may change. |
| 5 | +::: |
| 6 | + |
| 7 | +The router encodes and decodes URLs for you. The rule to remember: **give decoded values** (`params`, `query`) and **read decoded values** (`route.params`, `route.query`). A `path` in an object location must already be encoded. String locations and the `hash` are only normalized: the router encodes the characters that need it and keeps the existing percent encoded sequences. |
| 8 | + |
| 9 | +[[toc]] |
| 10 | + |
| 11 | +## Summary |
| 12 | + |
| 13 | +| Part of the location | You give | The router gives in `route` | |
| 14 | +| -------------------- | -------------- | --------------------------- | |
| 15 | +| String location | encoded or not | normalized `fullPath` | |
| 16 | +| Object `path` | encoded | the same `path` | |
| 17 | +| `params` | decoded | decoded `params` | |
| 18 | +| `query` values | decoded | decoded `query` | |
| 19 | +| `hash` | encoded or not | normalized `hash` | |
| 20 | + |
| 21 | +## Named locations and params |
| 22 | + |
| 23 | +The examples below use the default names generated from the page files. The router encodes each param. In a regular path param, a `/` becomes `%2F`, so the param stays one segment: |
| 24 | + |
| 25 | +```ts |
| 26 | +// user/[username].vue |
| 27 | +router.push({ |
| 28 | + name: '/user/[username]', |
| 29 | + params: { username: 'eduardo/san martin' }, |
| 30 | +}) |
| 31 | +// /user/eduardo%2Fsan%20martin |
| 32 | +// route.params.username === 'eduardo/san martin' |
| 33 | +``` |
| 34 | + |
| 35 | +Two kinds of path params keep a `/` as a separator: |
| 36 | + |
| 37 | +```ts |
| 38 | +// repeatable param, articles/[slugs]+.vue: each value is one segment |
| 39 | +router.push({ name: '/articles/[slugs]+', params: { slugs: ['a/b', 'c d'] } }) |
| 40 | +// /articles/a%2Fb/c%20d |
| 41 | + |
| 42 | +// catch-all param, docs/[...path].vue: the value can span segments |
| 43 | +router.push({ name: '/docs/[...path]', params: { path: 'guide/intro?' } }) |
| 44 | +// /docs/guide/intro%3F |
| 45 | +``` |
| 46 | + |
| 47 | +In all cases, `route.params` has the decoded values. This is the safest form of location. Use it when possible. |
| 48 | + |
| 49 | +## String locations |
| 50 | + |
| 51 | +The router normalizes a string location like a browser does with [`new URL()`](https://developer.mozilla.org/en-US/docs/Web/API/URL/URL). It encodes the characters that need it, keeps percent encoded sequences, and resolves `.` and `..` segments: |
| 52 | + |
| 53 | +```ts |
| 54 | +router.push('/user/café') // /user/caf%C3%A9 |
| 55 | +router.push('/user/caf%C3%A9') // same location |
| 56 | +router.push('/docs/./guide/../api') // /docs/api |
| 57 | +``` |
| 58 | + |
| 59 | +::: details Why are string locations normalized but object paths are not? |
| 60 | + |
| 61 | +A string location often comes from outside the app: a link in some content, a URL that a user wrote, or `route.fullPath`. Normalizing it makes it safe to use as is. `new URL()` is slow compared to the other forms, so the router does not use it for an object `path`. Do not build a `path` from dynamic values. Use a named location and let the router encode the params: |
| 62 | + |
| 63 | +```ts |
| 64 | +router.push({ name: '/user/[username]', params: { username } }) // [!code ++] |
| 65 | +router.push({ path: `/user/${username}` }) // [!code --] |
| 66 | +``` |
| 67 | + |
| 68 | +::: |
| 69 | + |
| 70 | +## Query |
| 71 | + |
| 72 | +Give decoded query values. The router encodes them, and it decodes them again in `route.query`: |
| 73 | + |
| 74 | +```ts |
| 75 | +router.push({ path: '/search', query: { q: ['a&b=c'] } }) |
| 76 | +// /search?q=a%26b=c |
| 77 | +// route.query.q → ['a&b=c'] |
| 78 | +``` |
| 79 | + |
| 80 | +Do not encode query values yourself. The router encodes them a second time: |
| 81 | + |
| 82 | +```ts |
| 83 | +router.push({ path: '/search', query: { q: ['a b'] } }) // [!code ++] |
| 84 | +router.push({ path: '/search', query: { q: ['a%20b'] } }) // [!code --] |
| 85 | +// /search?q=a%2520b, route.query.q → ['a%20b'] |
| 86 | +``` |
| 87 | + |
| 88 | +### Spaces and `+` |
| 89 | + |
| 90 | +In the query, the router writes a space as `+` and a `+` as `%2B`. When it reads a URL, `+` becomes a space. For spaces, all forms of a location give the same URL: |
| 91 | + |
| 92 | +```ts |
| 93 | +router.push({ path: '/search', query: { q: ['a b'] } }) // /search?q=a+b |
| 94 | +router.push('/search?q=a b') // /search?q=a+b |
| 95 | +router.push('/search?q=a%20b') // /search?q=a+b |
| 96 | +router.push('/search?q=a+b') // /search?q=a+b |
| 97 | +// route.query.q → ['a b'] for all of them |
| 98 | +``` |
| 99 | + |
| 100 | +To get a literal `+` from a string location, write `%2B`. In a query object, write `+`: |
| 101 | + |
| 102 | +```ts |
| 103 | +router.push('/search?q=c%2B%2B') // route.query.q → ['c++'] |
| 104 | +router.push({ path: '/search', query: { q: ['c++'] } }) // /search?q=c%2B%2B |
| 105 | +``` |
| 106 | + |
| 107 | +::: details Why `+` and not `%20`? |
| 108 | + |
| 109 | +`new URL()` writes a space in the query as `%20`, but `URLSearchParams`, HTML forms, and search engines write `+`. For example, when you use a website as a search engine in Chrome's address bar, Chrome creates links like `/search?q=a+b` ([#561](https://github.com/vuejs/router/issues/561)). The router reads `+` as a space, like `URLSearchParams`, so these links work. |
| 110 | + |
| 111 | +The router also always writes `+`, so a space in a query value always gives the same URL, whatever the form of the location. Pushing `route.fullPath` again does not change it. The router prefers this consistency over the exact output of `new URL()`. |
| 112 | + |
| 113 | +This applies only to the query. In the path and the hash, a `+` is a `+` and a space is `%20`. |
| 114 | + |
| 115 | +::: |
| 116 | + |
| 117 | +## Hash |
| 118 | + |
| 119 | +The hash is not a decoded value. The router normalizes it like setting [`url.hash`](https://developer.mozilla.org/en-US/docs/Web/API/URL/hash): it encodes the characters that need it and keeps every `%` as it is. `route.hash` is never decoded: |
| 120 | + |
| 121 | +```ts |
| 122 | +router.push({ path: '/docs', hash: '#a b' }) // /docs#a%20b |
| 123 | +router.push({ path: '/docs', hash: '#a%20b' }) // same location |
| 124 | +// route.hash → '#a%20b' |
| 125 | +router.push({ path: '/docs', hash: '#100%' }) // /docs#100% |
| 126 | +``` |
| 127 | + |
| 128 | +Because a `%` is never encoded, `#%20` always means a space. To put the literal text `%20` in a hash, encode the `%` yourself as `%25`: |
| 129 | + |
| 130 | +```ts |
| 131 | +router.push({ path: '/docs', hash: '#%2520' }) // /docs#%2520 |
| 132 | +// route.hash → '#%2520' |
| 133 | +decodeURIComponent(route.hash) // '#%20' |
| 134 | +``` |
| 135 | + |
| 136 | +::: details Why is `route.hash` not decoded? |
| 137 | + |
| 138 | +`route.hash` is the same as [`location.hash`](https://developer.mozilla.org/en-US/docs/Web/API/Location/hash). Decoding it can lose information when the hash has its own structure, for example an OAuth response like `#token=a%26b&state=c`. Use `decodeURIComponent()` if you need the decoded value. It throws on a lone `%` like `#100%`. |
| 139 | + |
| 140 | +::: |
0 commit comments