Skip to content

Commit

Permalink
fix: service worker is only registered once user logs in (#1712)
Browse files Browse the repository at this point in the history
closes #1663
  • Loading branch information
TheSlimvReal authored Mar 2, 2023
1 parent 5599ab2 commit 62fadce
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ import { SessionModule } from "./core/session/session.module";
import { LatestChangesModule } from "./core/latest-changes/latest-changes.module";

import { ChildrenModule } from "./child-dev-project/children/children.module";
import { ServiceWorkerModule } from "@angular/service-worker";
import {
ServiceWorkerModule,
SwRegistrationOptions,
} from "@angular/service-worker";
import { environment } from "../environments/environment";
import { LoggingErrorHandler } from "./core/logging/logging-error-handler";
import { AnalyticsService } from "./core/analytics/analytics.service";
Expand Down Expand Up @@ -72,6 +75,9 @@ import { ProgressDashboardWidgetModule } from "./features/progress-dashboard-wid
import { ReportingModule } from "./features/reporting/reporting.module";
import { RouterModule } from "@angular/router";
import { TodosModule } from "./features/todos/todos.module";
import { SessionService } from "./core/session/session-service/session.service";
import { waitForChangeTo } from "./core/session/session-states/session-utils";
import { LoginState } from "./core/session/session-states/login-state.enum";

/**
* Main entry point of the application.
Expand All @@ -82,9 +88,7 @@ import { TodosModule } from "./features/todos/todos.module";
declarations: [AppComponent],
imports: [
// Global Angular modules
ServiceWorkerModule.register("ngsw-worker.js", {
enabled: environment.production,
}),
ServiceWorkerModule.register("ngsw-worker.js"),
Angulartics2Module.forRoot({
developerMode: !environment.production,
}),
Expand Down Expand Up @@ -141,6 +145,15 @@ import { TodosModule } from "./features/todos/todos.module";
provide: MAT_DATE_FORMATS,
useValue: DATE_FORMATS,
},
{
provide: SwRegistrationOptions,
useFactory: (session: SessionService) => ({
enabled: environment.production,
registrationStrategy: () =>
session.loginState.pipe(waitForChangeTo(LoginState.LOGGED_IN)),
}),
deps: [SessionService],
},
],
bootstrap: [AppComponent],
})
Expand Down
2 changes: 2 additions & 0 deletions src/app/utils/database-testing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { AppModule } from "../app.module";
import { ComponentRegistry } from "../dynamic-components";
import { ConfigurableEnumService } from "../core/configurable-enum/configurable-enum.service";
import { createTestingConfigurableEnumService } from "../core/configurable-enum/configurable-enum-testing";
import { SwRegistrationOptions } from "@angular/service-worker";

/**
* Utility module that creates a simple environment where a correctly configured database and session is set up.
Expand All @@ -30,6 +31,7 @@ import { createTestingConfigurableEnumService } from "../core/configurable-enum/
provide: ConfigurableEnumService,
useValue: createTestingConfigurableEnumService(),
},
{ provide: SwRegistrationOptions, useValue: { enabled: false } },
],
})
export class DatabaseTestingModule {
Expand Down
2 changes: 2 additions & 0 deletions src/app/utils/mocked-testing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { AppModule } from "../app.module";
import { ComponentRegistry } from "../dynamic-components";
import { ConfigurableEnumService } from "../core/configurable-enum/configurable-enum.service";
import { createTestingConfigurableEnumService } from "../core/configurable-enum/configurable-enum-testing";
import { SwRegistrationOptions } from "@angular/service-worker";

export const TEST_USER = "test";
export const TEST_PASSWORD = "pass";
Expand All @@ -48,6 +49,7 @@ export const TEST_PASSWORD = "pass";
ReactiveFormsModule,
],
providers: [
{ provide: SwRegistrationOptions, useValue: { enabled: false } },
{
provide: AnalyticsService,
useValue: { eventTrack: () => undefined },
Expand Down

0 comments on commit 62fadce

Please sign in to comment.