add(method)[FE-896]: Add method for executing code without listening for popstate#13
Conversation
17e0f31 to
1ee1a77
Compare
1ee1a77 to
ff35c92
Compare
|
This approach requires the caller to know the side effect of What if instead we configured nuclear-router to ignore hash changes? That could be a bit overkill, but are there any reasons we'd want to re-route on pop-state of a hash change? |
f0aa8aa to
4cfb0df
Compare
|
@jamesopti that could be a reasonable way to go about it, but it seems a lot more risky. What in our app invisibly relies on route re-evaluation for hash changes, I definitely don't know where these happen off the top of my head. My vote is for a more gradual approach, where we have to explicitly change the behavior by using this new function. This approach seems like it de-risks such a global change. |
| */ | ||
| executeWithoutPopstateListener(fn) { | ||
| this.__shouldHandlePopstateEvents = false; | ||
| fn(); |
There was a problem hiding this comment.
Might there be a reason to return this result to the caller?
Fair. But I'd imagine @zachpwr plans to introduce this in the dialog manager's |
Summary:
Background:
nuclear-routerlistens forpopstateevents. The browser emits these events when the "Back" button is pressed or when the application executeswindow.history.back().nuclear-routerhandles these events by re-evaluating the URL and executing the corresponding route handler.The Optimizely application uses URL hashes (
/my-page#dialog) to represent when a full page dialog is being shown. This allows the user to close the full page dialog by hitting the "Back" button on their browser. However, in order to close a full page dialog in any other scenario (i.e. the user clicks "Submit" or "Cancel"), the application callswindow.history.back()to close the dialog and remove the hash from the URL (/my-page#dialog->/my-page).When the application performs this navigation,
nuclear-routerre-evaluates the route handler for that page even though removing the URL hash does not change the underlying route being accessed. This can potentially cause unnecessary API calls and initialization work to be executed.Implementation:
This PR adds a method to the
Routerinterface calledexecuteWithoutPopstateListener. It accepts one parameter,fn. This method allows the consuming application to run code without thepopstatelistener active. The code to be executed without thepopstatelistener enabled is placed in the argumentfn.This will allow the Optimizely application to close dialogs using
window.history.back()without the router re-evaluating the route for the page underneath the dialog. For example:Test Plan:
This PR adds a test case to ensure that the popstate listener is deactivated while executing the function passed to
executeWithoutPopstateListener.