mirror of
https://github.com/home-assistant/frontend.git
synced 2025-04-27 06:47:20 +00:00
Register service worker during login (#3036)
This commit is contained in:
parent
56e3514e40
commit
e3896c359a
@ -1,7 +1,15 @@
|
|||||||
import { litLocalizeLiteMixin } from "../mixins/lit-localize-lite-mixin";
|
import { litLocalizeLiteMixin } from "../mixins/lit-localize-lite-mixin";
|
||||||
import { LitElement, html, PropertyDeclarations } from "lit-element";
|
import {
|
||||||
|
LitElement,
|
||||||
|
html,
|
||||||
|
PropertyDeclarations,
|
||||||
|
PropertyValues,
|
||||||
|
CSSResult,
|
||||||
|
css,
|
||||||
|
} from "lit-element";
|
||||||
import "./ha-auth-flow";
|
import "./ha-auth-flow";
|
||||||
import { AuthProvider } from "../data/auth";
|
import { AuthProvider } from "../data/auth";
|
||||||
|
import { registerServiceWorker } from "../util/register-service-worker";
|
||||||
|
|
||||||
import(/* webpackChunkName: "pick-auth-provider" */ "../auth/ha-pick-auth-provider");
|
import(/* webpackChunkName: "pick-auth-provider" */ "../auth/ha-pick-auth-provider");
|
||||||
|
|
||||||
@ -76,7 +84,6 @@ class HaAuthorize extends litLocalizeLiteMixin(LitElement) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
return html`
|
return html`
|
||||||
${this.renderStyle()}
|
|
||||||
<p>
|
<p>
|
||||||
${this.localize(
|
${this.localize(
|
||||||
"ui.panel.page-authorize.authorizing_client",
|
"ui.panel.page-authorize.authorizing_client",
|
||||||
@ -108,7 +115,24 @@ class HaAuthorize extends litLocalizeLiteMixin(LitElement) {
|
|||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async firstUpdated() {
|
protected firstUpdated(changedProps: PropertyValues) {
|
||||||
|
super.firstUpdated(changedProps);
|
||||||
|
this._fetchAuthProviders();
|
||||||
|
|
||||||
|
if (!this.redirectUri) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we are logging into the instance that is hosting this auth form
|
||||||
|
// we will register the service worker to start preloading.
|
||||||
|
const tempA = document.createElement("a");
|
||||||
|
tempA.href = this.redirectUri!;
|
||||||
|
if (tempA.host === location.host) {
|
||||||
|
registerServiceWorker(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async _fetchAuthProviders() {
|
||||||
// Fetch auth providers
|
// Fetch auth providers
|
||||||
try {
|
try {
|
||||||
const response = await (window as any).providersPromise;
|
const response = await (window as any).providersPromise;
|
||||||
@ -136,19 +160,17 @@ class HaAuthorize extends litLocalizeLiteMixin(LitElement) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected renderStyle() {
|
private async _handleAuthProviderPick(ev) {
|
||||||
return html`
|
this._authProvider = ev.detail;
|
||||||
<style>
|
}
|
||||||
|
|
||||||
|
static get styles(): CSSResult {
|
||||||
|
return css`
|
||||||
ha-pick-auth-provider {
|
ha-pick-auth-provider {
|
||||||
display: block;
|
display: block;
|
||||||
margin-top: 48px;
|
margin-top: 48px;
|
||||||
}
|
}
|
||||||
</style>
|
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async _handleAuthProviderPick(ev) {
|
|
||||||
this._authProvider = ev.detail;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
customElements.define("ha-authorize", HaAuthorize);
|
customElements.define("ha-authorize", HaAuthorize);
|
||||||
|
@ -14,6 +14,7 @@ import { genClientId } from "home-assistant-js-websocket";
|
|||||||
import { litLocalizeLiteMixin } from "../mixins/lit-localize-lite-mixin";
|
import { litLocalizeLiteMixin } from "../mixins/lit-localize-lite-mixin";
|
||||||
import { OnboardingStep, onboardUserStep } from "../data/onboarding";
|
import { OnboardingStep, onboardUserStep } from "../data/onboarding";
|
||||||
import { PolymerChangedEvent } from "../polymer-types";
|
import { PolymerChangedEvent } from "../polymer-types";
|
||||||
|
import { registerServiceWorker } from "../util/register-service-worker";
|
||||||
|
|
||||||
@customElement("ha-onboarding")
|
@customElement("ha-onboarding")
|
||||||
class HaOnboarding extends litLocalizeLiteMixin(LitElement) {
|
class HaOnboarding extends litLocalizeLiteMixin(LitElement) {
|
||||||
@ -122,14 +123,18 @@ class HaOnboarding extends litLocalizeLiteMixin(LitElement) {
|
|||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async firstUpdated(changedProps: PropertyValues) {
|
protected firstUpdated(changedProps: PropertyValues) {
|
||||||
super.firstUpdated(changedProps);
|
super.firstUpdated(changedProps);
|
||||||
this.addEventListener("keypress", (ev) => {
|
this.addEventListener("keypress", (ev) => {
|
||||||
if (ev.keyCode === 13) {
|
if (ev.keyCode === 13) {
|
||||||
this._submitForm();
|
this._submitForm();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
this._fetchOnboardingSteps();
|
||||||
|
registerServiceWorker(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async _fetchOnboardingSteps() {
|
||||||
try {
|
try {
|
||||||
const response = await window.stepsPromise;
|
const response = await window.stepsPromise;
|
||||||
|
|
||||||
|
@ -1,12 +1,20 @@
|
|||||||
const serviceWorkerUrl =
|
const serviceWorkerUrl =
|
||||||
__BUILD__ === "latest" ? "/service_worker.js" : "/service_worker_es5.js";
|
__BUILD__ === "latest" ? "/service_worker.js" : "/service_worker_es5.js";
|
||||||
|
|
||||||
export const registerServiceWorker = () => {
|
export const registerServiceWorker = (notifyUpdate = true) => {
|
||||||
if (!("serviceWorker" in navigator)) return;
|
if (
|
||||||
|
!("serviceWorker" in navigator) ||
|
||||||
|
(location.protocol !== "https:" && location.hostname !== "localhost")
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
navigator.serviceWorker.register(serviceWorkerUrl).then((reg) => {
|
navigator.serviceWorker.register(serviceWorkerUrl).then((reg) => {
|
||||||
reg.addEventListener("updatefound", () => {
|
reg.addEventListener("updatefound", () => {
|
||||||
const installingWorker = reg.installing;
|
const installingWorker = reg.installing;
|
||||||
|
if (!installingWorker || !notifyUpdate) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
installingWorker.addEventListener("statechange", () => {
|
installingWorker.addEventListener("statechange", () => {
|
||||||
if (
|
if (
|
||||||
installingWorker.state === "installed" &&
|
installingWorker.state === "installed" &&
|
Loading…
x
Reference in New Issue
Block a user