Skip to content

Add prevent_unload/allow_unload actions#336

Open
myabc wants to merge 2 commits into
marcoroth:mainfrom
myabc:feature/prevent_unload
Open

Add prevent_unload/allow_unload actions#336
myabc wants to merge 2 commits into
marcoroth:mainfrom
myabc:feature/prevent_unload

Conversation

@myabc

@myabc myabc commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

Implements two Browser actions to arm and disarm a beforeunload guard, so the server can warn the user before they leave a tab with unsaved changes.

<turbo-stream action="prevent_unload"></turbo-stream>
<turbo-stream action="allow_unload"></turbo-stream>

Approach

  • Uses addEventListener("beforeunload", …) with a single module-level named handler, not the window.onbeforeunload property. This composes with any other beforeunload listeners and removes only its own handler — assigning the property would clobber (and be clobbered by) other scripts.
  • The handler calls preventDefault() (the modern trigger) and sets returnValue for broader browser support. No browser supported by Turbo 7.2+ displays the returnValue string, so the generic confirmation dialog is shown.
  • prevent_unload is idempotent (re-adding the same handler reference is a no-op).
  • No custom message attribute: modern browsers ignore custom onbeforeunload strings and show a generic dialog.

Changes

  • Added prevent_unload / allow_unload to src/actions/browser.ts and registered them.
  • Added test/browser/before_unload.test.js — arm, disarm, and idempotency cases (asserts event.defaultPrevented).
  • Documented both under Browser Actions in the README.

Closes #24

🤖 Generated with Claude Code

Copilot AI review requested due to automatic review settings June 27, 2026 13:24

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds two TurboPower Browser actions that let the server enable/disable a beforeunload guard so users get a browser-native warning dialog when leaving a tab with unsaved changes. This integrates as a composable beforeunload event listener (via addEventListener / removeEventListener) rather than clobbering window.onbeforeunload.

Changes:

  • Implement prevent_unload and allow_unload actions using a shared module-level beforeunload handler.
  • Register both actions in Browser action registration.
  • Add browser tests covering arming, disarming, and idempotent re-arming, plus README documentation entries.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
test/browser/before_unload.test.js Adds tests validating the beforeunload guard can be armed/disarmed and remains removable after repeated arming.
src/actions/browser.ts Implements and registers prevent_unload/allow_unload via a named beforeunload event handler.
README.md Documents the two new Browser actions in the public action list.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@myabc
myabc force-pushed the feature/prevent_unload branch from 5532301 to 3f3ae84 Compare June 27, 2026 13:50
@myabc
myabc requested a review from Copilot June 27, 2026 14:01

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Comment thread src/actions/browser.ts
myabc added 2 commits June 27, 2026 16:21
Arm and disarm a `beforeunload` guard via a named `addEventListener`
handler, so the browser warns before leaving with unsaved changes.

Closes marcoroth#24.
Add the two new Browser Actions to the README action list.
@myabc
myabc force-pushed the feature/prevent_unload branch from 3f3ae84 to 0b60436 Compare June 27, 2026 14:21

@marcoroth marcoroth left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about set_beforeunload and clear_beforeunload?

@myabc

myabc commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

Hi @marcoroth

What do you think about set_beforeunload and clear_beforeunload?

I considered those names as well. I guess that would make the underlying implementation clearer (and align better with other action naming).

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.

Implement Action to set a window.onbeforeunload callback

3 participants