Skip to content
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

Wait for service worker to start before doing anything else #960

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion src/platform/web/Platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ export class Platform {
this._serviceWorkerHandler = null;
if (assetPaths.serviceWorker && "serviceWorker" in navigator) {
this._serviceWorkerHandler = new ServiceWorkerHandler();
this._serviceWorkerHandler.registerAndStart(assetPaths.serviceWorker);
}
this.notificationService = undefined;
// Only try to use crypto when olm is provided
Expand Down Expand Up @@ -172,6 +171,12 @@ export class Platform {

async init() {
try {
if (this._serviceWorkerHandler instanceof ServiceWorkerHandler) {
// Start the service worker before doing anything else,
// to make sure all requests are intercepted by the service worker.
await this._serviceWorkerHandler.registerAndStart(this._assetPaths.serviceWorker)
}

await this.logger.run("Platform init", async (log) => {
if (!this._config) {
if (!this._configURL) {
Expand Down
4 changes: 2 additions & 2 deletions src/platform/web/dom/ServiceWorkerHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export class ServiceWorkerHandler {
this._navigation = navigation;
}

registerAndStart(path) {
this._registrationPromise = (async () => {
async registerAndStart(path) {
return this._registrationPromise = (async () => {
navigator.serviceWorker.addEventListener("message", this);
navigator.serviceWorker.addEventListener("controllerchange", this);
this._registration = await navigator.serviceWorker.register(path);
Expand Down