Skip to content

feat: remove page hide event from triggering unload event listener #31853

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

Merged
merged 4 commits into from
Jun 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ _Released 07/01/2025 (PENDING)_
- `@cypress/webpack-dev-server` and `@cypress/webpack-preprocessor` no longer support `webpack` version 4. Addresses [#31344](https://github.com/cypress-io/cypress/issues/31344). If you still need to use `webpack` version 4, please see our [migration guide](https://docs.cypress.io/app/references/migration-guide#Migrating-to-Cypress-150).
- `@cypress/webpack-dev-server` no longer supports `webpack-dev-server` version 4. Addresses [#31605](https://github.com/cypress-io/cypress/issues/31605). If you still need to use `webpack-dev-server` version 4, please see our [migration guide](https://docs.cypress.io/app/references/migration-guide#Migrating-to-Cypress-150).
- The `@cypress/webpack-batteries-included-preprocessor` no longer includes browser built-ins that were automatically provided by Webpack 4. To add built-ins manually, refer to the Webpack [resolve.fallback](https://webpack.js.org/configuration/resolve/#resolvefallback) documentation and the [`@cypress/webpack-batteries-included-preprocessor` README](../npm/webpack-batteries-included-preprocessor/README.md). Addresses [#31039](https://github.com/cypress-io/cypress/issues/31039).
- The application under test's `pagehide` event in Chromium browsers will no longer trigger Cypress's `window:unload` event. Addressed in [#31853](https://github.com/cypress-io/cypress/pull/31853).

**Features:**

Expand Down
8 changes: 2 additions & 6 deletions packages/driver/cypress/e2e/commands/navigation.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,13 @@ describe('src/cy/commands/navigation', () => {
})

it('removes listeners', () => {
cy.log(Cypress.browser)
const unloadEvent = Cypress.browser.family === 'chromium' ? 'pagehide' : 'unload'
const win = cy.state('window')

const rel = cy.stub(win, 'removeEventListener')

cy.reload().then(() => {
expect(rel).to.be.calledWith('beforeunload')
expect(rel).to.be.calledWith(unloadEvent)
expect(rel).to.be.calledWith('unload')
})
})

Expand Down Expand Up @@ -415,10 +413,8 @@ describe('src/cy/commands/navigation', () => {
const rel = cy.stub(win, 'removeEventListener')

cy.go('back').then(() => {
const unloadEvent = cy.browser.family === 'chromium' ? 'pagehide' : 'unload'

expect(rel).to.be.calledWith('beforeunload')
expect(rel).to.be.calledWith(unloadEvent)
expect(rel).to.be.calledWith('unload')
})
})
})
Expand Down
5 changes: 1 addition & 4 deletions packages/driver/src/cross-origin/cypress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,10 @@ const attachToWindow = (autWindow: Window) => {
Cypress.specBridgeCommunicator.toPrimary('window:load', { url: remoteLocation.href })
cy.isStable(true, 'load')
},
onPageHide (e) {
onUnload (e) {
cy.state('window', undefined)
cy.state('document', undefined)

// unload is being actively deprecated/removed by chrome, so for
// compatibility, we are using `window`'s `pagehide` event as a proxy
// for the `window:unload` event that we emit. See: https://github.com/cypress-io/cypress/pull/29525
return Cypress.action('app:window:unload', e)
},
onNavigation (...args) {
Expand Down
12 changes: 3 additions & 9 deletions packages/driver/src/cy/listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type BoundCallbacks = {
onSubmit: (e) => any
onLoad: (e) => any
onBeforeUnload: (e) => undefined | Promise<undefined>
onPageHide: (e: PageTransitionEvent) => any
onUnload: (e: BeforeUnloadEvent) => any
onNavigation: (...args) => any
onAlert: (str) => any
onConfirm: (str) => boolean
Expand Down Expand Up @@ -93,18 +93,12 @@ export const bindToListeners = (contentWindow, callbacks: BoundCallbacks) => {
await callbacks.onBeforeUnload(e)
})

// While we must move to pagehide for Chromium, it does not work for our
// needs in Firefox. Until that is addressed, only Chromium uses the pagehide
// event as a proxy for AUT unloads.

const unloadEvent = Cypress.browser.family === 'chromium' ? 'pagehide' : 'unload'

addListener(contentWindow, unloadEvent, (e) => {
addListener(contentWindow, 'unload', (e) => {
// when we unload we need to remove all of the event listeners
removeAllListeners()

// else we know to proceed onwards!
callbacks.onPageHide(e)
callbacks.onUnload(e)
})

addListener(contentWindow, 'hashchange', (e) => {
Expand Down
5 changes: 1 addition & 4 deletions packages/driver/src/cypress/cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1130,10 +1130,7 @@ export class $Cy extends EventEmitter2 implements ITimeouts, IStability, IAssert
// doesn't trigger a confirmation dialog
return undefined
},
onPageHide (e) {
// unload is being actively deprecated/removed by chrome, so for
// compatibility, we are using `window`'s `pagehide` event as a proxy
// for the `window:unload` event that we emit. See: https://github.com/cypress-io/cypress/pull/29525
onUnload (e) {
return cy.Cypress.action('app:window:unload', e)
},
onNavigation (...args) {
Expand Down
Loading