Skip to content

playground: add beforeunload event listener to prevent data loss #12961

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

Open
wants to merge 1 commit into
base: 2.1.x
Choose a base branch
from

Conversation

calebdw
Copy link
Contributor

@calebdw calebdw commented May 2, 2025

Hello!

Related to: #12956

This adds a beforeunload event listener to the PlaygroundViewModel to prevent data loss when navigating away from the page. The listener is added when the preanalyse method is called and removed after the analysis is shared.

See: https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event#examples

Thanks!

This adds a beforeunload event listener to the PlaygroundViewModel to
prevent data loss when navigating away from the page. The listener
is added when the preanalyse method is called and removed after
the analysis is shared.

See: https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event#examples
@ondrejmirtes
Copy link
Member

Hi, I imagined we should back up and restore the unsaved code with the localStorage API.

@calebdw
Copy link
Contributor Author

calebdw commented May 3, 2025

I looked into that as well and we can do both, but this was easiest path to not accidentally lose unsaved changes. Of course, this does not cover scenarios like a browser crash but that's more rare in my opinion than accidentally clicking the back button / closing the tab.

With the local storage option there's other things to consider like:

  • do you also back up and restore the analysis results?
  • how does the user intentionally reset the editor?
    • sharing the result will obviously clear the local storage but there probably needs to be a reset button (with an additional prompt) for the user to intentionally clear changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants