Page MenuHomePhabricator

Display cancel dialog (after edits) on browser interactions
Closed, ResolvedPublicBUG REPORT

Description

we should display a cancel dialog when users try to leave edit mode (via a browser action) with unpublished changes.

image.png (378×1 px, 39 KB)

this might happen when:

  • they reload/refresh the browser tab
  • they close the browser tab/window
  • they go back/forward with the browser history stack.

Event Timeline

Jdforrester-WMF changed the subtype of this task from "Task" to "Bug Report".Jun 29 2023, 5:52 PM
Jdforrester-WMF changed the task status from Open to In Progress.Jul 24 2023, 5:09 PM
Jdforrester-WMF assigned this task to gengh.

I went to https://wikifunctions.beta.wmflabs.org/view/en/Z10680

I clicked on Edit source and got to https://wikifunctions.beta.wmflabs.org/wiki/Z10680?action=edit&uselang=en

I changed the string.

Now, whether I try to reload, or close the page, or go back, the dialog does not show up for me.

Currently only events on the document that attempt to leave the page are captured and handled. This includes:

  • Clicking cancel button
  • Clicking links that will navigate out of the page

However, browser events are a bit more complicated and are heavily restricted by browsers.


beforeunload event

We can use this event to show a confirmation dialog by running event.preventDefault(). However, for security reasons, the compatibility is minimal and the dialog is handled by the browser and does not allow customization of the dialog message:

  • Firefox displays the string, "This page is asking you to confirm that you want to leave - data you have entered may not be saved."
  • Chrome displays the string, "Do you want to leave the site? Changes you made may not be saved."

This is the displayed dialog in Chrome:

Screenshot from 2023-07-25 13-30-26.png (1×1 px, 138 KB)

And in Firefox:

Screenshot from 2023-07-25 14-04-16.png (944×1 px, 139 KB)

This is the event that we use in mediawiki to handle browser exits, for example see this unsaved edit in a mediawiki.org page:

Exit through clicking a document link:

Screenshot from 2023-07-25 14-05-53.png (955×1 px, 268 KB)

Exit on browser events (back button):

Screenshot from 2023-07-25 14-03-30.png (1×1 px, 333 KB)

This has a series of performance and compatibility issues. From the Chrome documentation:

Never add a beforeunload listener unconditionally or use it as an end-of-session signal. Only add it when a user has unsaved work, and remove it as soon as that work has been saved.

This might be challenging for Function Editor, as the changes are not tracked at every moment but on validation when capturing "Cancel" or link click.


Other events

Other events that I've contemplated and not reliable or highly discouraged are: unload, pagehide and visibilitychange


Solution

I will try to address this issue using the beforeunload event considering that this is the approach used by mediawiki. The dialog will be different and browser-provided. I will also try to avoid performance issues described in the mozilla and chrome doc but this might not be possible.

Change 941426 had a related patch set uploaded (by Genoveva Galarza; author: Genoveva Galarza):

[mediawiki/extensions/WikiLambda@master] Add exit cancel handling for browser events

https://gerrit.wikimedia.org/r/941426

Change 941426 merged by jenkins-bot:

[mediawiki/extensions/WikiLambda@master] Add exit cancel handling for browser events

https://gerrit.wikimedia.org/r/941426

Thanks for the research! Considering this closed.

Change 941848 had a related patch set uploaded (by Jforrester; author: Genoveva Galarza):

[mediawiki/extensions/WikiLambda@wmf/1.41.0-wmf.19] Add exit cancel handling for browser events

https://gerrit.wikimedia.org/r/941848

Change 941848 merged by jenkins-bot:

[mediawiki/extensions/WikiLambda@wmf/1.41.0-wmf.19] Add exit cancel handling for browser events

https://gerrit.wikimedia.org/r/941848