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 1df2abd57bf62adfa950f5ea9f8b231308e70d2e
11 changes: 11 additions & 0 deletions src/components/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,13 @@ export default {
this.updateEditorWidth(newWidth)
},
},
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 @@ -883,6 +890,10 @@ export default {
updateEditorWidth(newWidth) {
document.documentElement.style.setProperty('--text-editor-max-width', newWidth)
},

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 @@ -105,17 +106,30 @@
})
}

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 = {

Check warning on line 111 in src/services/SessionApi.js

View check run for this annotation

Codecov / codecov/patch

src/services/SessionApi.js#L109-L111

Added lines #L109 - L111 were not covered by tests
...this.#defaultParams,
filePath: this.#options.filePath,
baseVersionEtag: this.#document.baseVersionEtag,
version,
autosaveContent,
documentState,
force,
manualSave,
})
...data,
}

Check warning on line 116 in src/services/SessionApi.js

View check run for this annotation

Codecov / codecov/patch

src/services/SessionApi.js#L115-L116

Added lines #L115 - L116 were not covered by tests

return this.#post(url, postData)
}

Check warning on line 119 in src/services/SessionApi.js

View check run for this annotation

Codecov / codecov/patch

src/services/SessionApi.js#L118-L119

Added lines #L118 - L119 were not covered by tests

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() ?? '',
}

Check warning on line 129 in src/services/SessionApi.js

View check run for this annotation

Codecov / codecov/patch

src/services/SessionApi.js#L121-L129

Added lines #L121 - L129 were not covered by tests

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

Check warning on line 132 in src/services/SessionApi.js

View check run for this annotation

Codecov / codecov/patch

src/services/SessionApi.js#L131-L132

Added lines #L131 - L132 were not covered by tests
}

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 @@ -218,7 +218,7 @@
this.emit('error', { type: ERROR_TYPE.PUSH_FORBIDDEN, data: {} })
}
// TODO: does response.data ever have a document? maybe for errors?
// TODO: `currentVersion` is always 0 nowadays. Check if this is still needed.

Check warning on line 221 in src/services/SyncService.js

View check run for this annotation

Codecov / codecov/patch

src/services/SyncService.js#L221

Added line #L221 was not covered by tests
// Only emit conflict event if we have synced until the latest version
if (response.data.document?.currentVersion === this.version) {
this.emit('error', { type: ERROR_TYPE.PUSH_FAILURE, data: {} })
Expand Down Expand Up @@ -300,6 +300,17 @@
}
}

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

Check warning on line 312 in src/services/SyncService.js

View check run for this annotation

Codecov / codecov/patch

src/services/SyncService.js#L303-L312

Added lines #L303 - L312 were not covered by tests

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