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

Opting out of beforeunload and using BFCache #31

Closed
agundermann opened this issue Aug 18, 2015 · 4 comments
Closed

Opting out of beforeunload and using BFCache #31

agundermann opened this issue Aug 18, 2015 · 4 comments

Comments

@agundermann
Copy link
Contributor

I think it would be great to allow opting out of using beforeunload for the following reasons:

  1. Usability is controversial. Personally, I would avoid it in most cases and save the data to localStorage to restore it on the next visit rather than annoying the user with a modal when he tries to close the browser window. There's even a discussion about disabling it by default at bugzilla.
  2. It comes with the non-apparent side effect of disabling BFCache which freezes the DOM and scripts when the page is left so they can be quickly restored/resumed when navigating back to the page.

Incidentally, using beforeunload to disable BFCache fixes this bug which occurs when re-entering the cached page at a different URL. But as far as I can tell we can fix this in a better way by using the pageshow event. Something like

function pageShowListener(event) {
  if (event.persisted)
    history.transitionTo(getCurrentLocation());
} 

That way, when using a browser with BFCache, re-entering our app at the same URL wouldn't require any re-renders, and re-entering at a different URL would allow for render optimizations such as React's reconciliation (DOM diffing).

@mjackson
Copy link
Member

Hmm, interesting. What do you think about adding a flag to the options object?

var history = createHistory({
  useBeforeUnload: false
});

I'd think if you want to disable something like this you'd want to disable it for all transition hooks, so setting the option globally makes sense.

As far as listening for the pageshow event, let's get that added in a separate PR and close the router bug.

@mjackson
Copy link
Member

Personally, I would avoid it in most cases

Just re-read this. I think you're right. Maybe we make it opt-in instead in that case?

var history = createHistory({
  useBeforeUnload: true
});

@agundermann
Copy link
Contributor Author

Yeah, I'd actually prefer opt-in. Also agree with your reasoning for using an options flag.

@mjackson
Copy link
Member

Taking a look at this today. I think it makes sense to opt-in to beforeunload handling using an enhancer, ala enableQueries. It's different enough from normal transitions that I think it warrants its own API.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants