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

fix: service worker is only registered once user logs in #1712

Merged
merged 5 commits into from
Mar 2, 2023
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
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