-
Notifications
You must be signed in to change notification settings - Fork 12
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
POC Monkey-patch history pushState and replaceState to listen to location changes #455
Open
zanderle
wants to merge
4
commits into
main
Choose a base branch
from
zanderle/157-listen-on-pushState-and-replaceState-changes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+57
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7aee62e
Monkey-patch history pushState and replaceState to listen to location…
zanderle eca74eb
Update src/utils.js
zanderle 861e2c2
Change the name of the event triggered when url is changed
zanderle bae8d79
Add a simple check on index.html to confirm it works
zanderle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -24,6 +24,8 @@ export const IS_TESTING = | |||||
export const IS_PRODUCTION = | ||||||
typeof WEBPACK_IS_PRODUCTION === "undefined" ? false : WEBPACK_IS_PRODUCTION; | ||||||
|
||||||
export const READTHEDOCS_URL_CHANGED_EVENT = "readthedocsUrlChanged"; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
We are using |
||||||
|
||||||
export const domReady = new Promise((resolve) => { | ||||||
if ( | ||||||
document.readyState === "interactive" || | ||||||
|
@@ -155,6 +157,44 @@ export class AddonBase { | |||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
* Setup events firing on history `pushState` and `replaceState` | ||||||
* | ||||||
* This is needed when addons are used in SPA. A lot of addons rely | ||||||
* on the current URL. However in the SPA, the pages are not reloaded, so | ||||||
* the addons never get notified of the changes in the URL. | ||||||
* | ||||||
* While History API does have `popstate` event, the only way to listen to | ||||||
* changes via `pushState` and `replaceState` is using monkey-patching, which is | ||||||
* what this function does. (See https://stackoverflow.com/a/4585031) | ||||||
* It will fire a `READTHEDOCS_URL_CHANGED` event, on `pushState` and `replaceState`. | ||||||
* | ||||||
*/ | ||||||
export function setupHistoryEvents() { | ||||||
// Let's ensure that the history will be patched only once, so we create a Symbol to check by | ||||||
const patchKey = Symbol.for("addons_history"); | ||||||
|
||||||
if ( | ||||||
typeof history !== "undefined" && | ||||||
typeof window[patchKey] === "undefined" | ||||||
) { | ||||||
for (const methodName of ["pushState", "replaceState"]) { | ||||||
const originalMethod = history[methodName]; | ||||||
history[methodName] = function () { | ||||||
const result = originalMethod.apply(this, arguments); | ||||||
humitos marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
const event = new Event(READTHEDOCS_URL_CHANGED_EVENT); | ||||||
event.arguments = arguments; | ||||||
|
||||||
dispatchEvent(event); | ||||||
return result; | ||||||
}; | ||||||
} | ||||||
|
||||||
// Let's leave a flag, so we know that history has been patched | ||||||
Object.defineProperty(window, patchKey, { value: true }); | ||||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
* Debounce a function. | ||||||
* | ||||||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.