Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
feat(SessionApi): Send save request via sendBeacon at beforeunload
This will send a final save request on unsaved changes via the browsers
native `navigator.sendBeacon()` function when navigating away from the
website or the tab/browser is closed.

Fixes: #6606

Implementation details:
* While `beforeunload` event is less reliable than `visibilitychange`
  according to different sources on the internet, tests in Firefox on
  Linux desktop revealed that `visibilitychange` event doesn't fire when
  opening a new website in the same tab. `beforeunload` on the other
  hand reliably worked when switching web page and closing tab/browser.
* We add and remove the `beforeunload` event with every `dirty` state
  change because according to MDN documentation websites with a
  registered `beforeunload` event don't benefit from bfcache optimization.
  See https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event

Signed-off-by: Jonas <[email protected]>
  • Loading branch information
mejo- committed Apr 2, 2025
commit c6990dcb45e68527a68ee71b7a203c2c83150849
11 changes: 11 additions & 0 deletions src/components/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,13 @@ export default {
this.contentWrapper = this.$refs.contentWrapper
})
},
dirty(val) {
if (val) {
window.addEventListener('beforeunload', this.saveBeforeUnload)
} else {
window.removeEventListener('beforeunload', this.saveBeforeUnload)
}
},
},
mounted() {
if (this.active && (this.hasDocumentParameters)) {
Expand Down Expand Up @@ -835,6 +842,10 @@ export default {
})
this.translateModal = false
},

saveBeforeUnload() {
this.$syncService?.saveViaSendBeacon()
},
},
}
</script>
Expand Down
30 changes: 22 additions & 8 deletions src/services/SessionApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import axios from '@nextcloud/axios'
import { getRequestToken } from '@nextcloud/auth'
import { generateUrl } from '@nextcloud/router'

export class ConnectionClosedError extends Error {
Expand Down Expand Up @@ -106,17 +107,30 @@ export class Connection {
})
}

save({ version, autosaveContent, documentState, force, manualSave }) {
return this.#post(this.#url(`session/${this.#document.id}/save`), {
save(data) {
const url = this.#url(`session/${this.#document.id}/save`)
const postData = {
...this.#defaultParams,
filePath: this.#options.filePath,
baseVersionEtag: this.#document.baseVersionEtag,
version,
autosaveContent,
documentState,
force,
manualSave,
})
...data,
}

return this.#post(url, postData)
}

saveViaSendBeacon(data) {
const url = this.#url(`session/${this.#document.id}/save`)
const postData = {
...this.#defaultParams,
filePath: this.#options.filePath,
baseVersionEtag: this.#document.baseVersionEtag,
...data,
requestToken: getRequestToken() ?? '',
}

const blob = new Blob([JSON.stringify(postData)], { type: 'application/json' })
return navigator.sendBeacon(url, blob)
}

push({ steps, version, awareness }) {
Expand Down
11 changes: 11 additions & 0 deletions src/services/SyncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,17 @@ class SyncService {
}
}

saveViaSendBeacon() {
this.#connection.saveViaSendBeacon({
version: this.version,
autosaveContent: this._getContent(),
documentState: this.getDocumentState(),
force: false,
manualSave: true,
})
logger.debug('[SyncService] saved using sendBeacon')
}

forceSave() {
return this.save({ force: true })
}
Expand Down
Loading