Normalize fragment encoding to avoid duplicate route fires (#4132)#4303
Open
BigBalli wants to merge 1 commit intojashkenas:masterfrom
Open
Normalize fragment encoding to avoid duplicate route fires (#4132)#4303BigBalli wants to merge 1 commit intojashkenas:masterfrom
BigBalli wants to merge 1 commit intojashkenas:masterfrom
Conversation
…#4132) In Firefox/Safari, `location.href` returns the hash with percent- encoded non-ASCII characters even when `navigate()` was called with the decoded form. As a result, after navigating to e.g. `#search/大阪`, the next `hashchange`/`popstate` causes `checkUrl()` to read back `%E5%A4%A7%E9%98%AA`, see it as different from the cached `this.fragment`, and fire the route a second time. Normalize on the decoded form everywhere we cache or compare a fragment: - `History#start` stores the initial fragment in decoded form. - `History#checkUrl` decodes both `getFragment()` and the iframe hash before comparing against `this.fragment`. - `History#loadUrl` decodes the fragment before caching it on `this.fragment` and dispatching to handlers. `decodeFragment` is idempotent (it preserves literal `%25`), so calling it on a value that is already decoded is a safe no-op. Also fixes jashkenas#4085 and jashkenas#3941, which are the same encoding mismatch manifesting through different code paths.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4132. Also addresses #4085 and #3941, which are the same
encoding mismatch through different code paths.
Problem
In Firefox (and historically Safari),
location.hrefreturns the hashwith percent-encoded non-ASCII characters even when
navigate()wascalled with the decoded form. After navigating to
#search/大阪, thenext
hashchange/popstate:checkUrl()callsgetFragment()→getHash()→ readssearch/%E5%A4%A7%E9%98%AAfromlocation.href.this.fragment, whichnavigate()setto the decoded form
search/大阪.loadUrl()runs → the route fires a second time.This breaks any i18n app whose URLs contain CJK, Cyrillic, accented
Latin, etc.: every navigation triggers analytics/fetches/state changes
twice.
Fix
Normalize on the decoded form everywhere a fragment is cached or
compared:
History#startstores the initial fragment asdecodeFragment(getFragment()).History#checkUrlcomparesdecodeFragment(getFragment())(and theiframe hash) against
this.fragment.History#loadUrlwritesdecodeFragment(getFragment(fragment))tothis.fragmentbefore dispatching.decodeFragmentis already designed to preserve literal%25, so itis idempotent on values that are already decoded — safe to call on
the pushState path that already decodes via
getPath().Test
Added a regression test that constructs a
Historyinstance with amocked
location.hrefcontaining the encoded form ofsearch/大阪,sets
this.fragmentto the decoded form (asnavigatewould), andasserts that
checkUrl()does not re-invoke the matching routehandler.
npm run lintpasses.