Skip to content

Commit

Permalink
Preload (#6062)
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob committed May 27, 2020
1 parent 78f5429 commit aa9a354
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 51 deletions.
13 changes: 1 addition & 12 deletions demo/src/html/index.html.template
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,6 @@
<link rel="manifest" href="/manifest.json" crossorigin="use-credentials" />
<link rel="icon" href="/static/icons/favicon.ico" />
<link rel="mask-icon" href="/static/icons/mask-icon.svg" color="#03a9f4" />
<link
rel="preload"
href="/static/fonts/roboto/Roboto-Regular.woff2"
as="font"
crossorigin
/>
<link
rel="preload"
href="/static/fonts/roboto/Roboto-Medium.woff2"
as="font"
crossorigin
/>
<link
rel="apple-touch-icon"
sizes="180x180"
Expand Down Expand Up @@ -96,6 +84,7 @@
<div id="ha-init-skeleton"></div>
<ha-demo></ha-demo>
<%= renderTemplate('_js_base') %>
<%= renderTemplate('_preload_roboto') %>

<script type="module" src="<%= latestDemoJS %>"></script>

Expand Down
6 changes: 6 additions & 0 deletions src/entrypoints/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { HomeAssistant } from "../types";
declare global {
interface Window {
hassConnection: Promise<{ auth: Auth; conn: Connection }>;
hassConnectionReady?: (hassConnection: Window["hassConnection"]) => void;
}
}

Expand Down Expand Up @@ -82,6 +83,11 @@ window.hassConnection = (authProm() as Promise<Auth | ExternalAuth>).then(
connProm
);

// This is set if app was somehow loaded before core.
if (window.hassConnectionReady) {
window.hassConnectionReady(window.hassConnection);
}

// Start fetching some of the data that we will need.
window.hassConnection.then(({ conn }) => {
const noop = () => {
Expand Down
16 changes: 16 additions & 0 deletions src/html/_preload_roboto.html.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<script>
if (navigator.userAgent.indexOf("Android") === -1 &&
navigator.userAgent.indexOf("CrOS") === -1) {
function _pf(src, type) {
const el = document.createElement("link");
el.rel = "preload";
el.as = "font";
el.type = "font/woff2";
el.href = src;
el.crossOrigin = "anonymous";
document.head.append(el);
}
_pf("/static/fonts/roboto/Roboto-Regular.woff2");
_pf("/static/fonts/roboto/Roboto-Medium.woff2");
}
</script>
13 changes: 1 addition & 12 deletions src/html/authorize.html.template
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,6 @@
<head>
<title>Home Assistant</title>
<link rel="preload" href="<%= latestPageJS %>" as="script" crossorigin="use-credentials" />
<link
rel="preload"
href="/static/fonts/roboto/Roboto-Light.woff2"
as="font"
crossorigin
/>
<link
rel="preload"
href="/static/fonts/roboto/Roboto-Regular.woff2"
as="font"
crossorigin
/>
<%= renderTemplate('_header') %>
<style>
.content {
Expand Down Expand Up @@ -46,6 +34,7 @@
</div>

<%= renderTemplate('_js_base') %>
<%= renderTemplate('_preload_roboto') %>

<script type="module" crossorigin="use-credentials">
import "<%= latestPageJS %>";
Expand Down
20 changes: 6 additions & 14 deletions src/html/index.html.template
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,7 @@
<html>
<head>
<link rel="preload" href="<%= latestCoreJS %>" as="script" crossorigin="use-credentials" />
<link
rel="preload"
href="/static/fonts/roboto/Roboto-Regular.woff2"
as="font"
crossorigin
/>
<link
rel="preload"
href="/static/fonts/roboto/Roboto-Medium.woff2"
as="font"
crossorigin
/>
<link rel="preload" href="<%= latestAppJS %>" as="script" crossorigin="use-credentials" />
<%= renderTemplate('_header') %>
<title>Home Assistant</title>
<link
Expand Down Expand Up @@ -61,10 +50,11 @@
<home-assistant> </home-assistant>

<%= renderTemplate('_js_base') %>
<%= renderTemplate('_preload_roboto') %>

<script type="module" crossorigin="use-credentials">
import "<%= latestCoreJS %>";
import "<%= latestAppJS %>";
import("<%= latestCoreJS %>");
import("<%= latestAppJS %>");
window.customPanelJS = "<%= latestCustomPanelJS %>";
</script>
{% for extra_module in extra_modules -%}
Expand All @@ -81,6 +71,8 @@

<% if (useRollup) { %>
_ls("/static/js/s.min.js").onload = function() {
// Although core and app can load in any order, we need to
// force loading core first because it contains polyfills
return System.import("<%= es5CoreJS %>").then(function() {
System.import("<%= es5AppJS %>");
});
Expand Down
13 changes: 1 addition & 12 deletions src/html/onboarding.html.template
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,6 @@
<head>
<title>Home Assistant</title>
<link rel="preload" href="<%= latestPageJS %>" as="script" crossorigin="use-credentials" />
<link
rel="preload"
href="/static/fonts/roboto/Roboto-Light.woff2"
as="font"
crossorigin
/>
<link
rel="preload"
href="/static/fonts/roboto/Roboto-Regular.woff2"
as="font"
crossorigin
/>
<%= renderTemplate('_header') %>
<style>
.content {
Expand Down Expand Up @@ -48,6 +36,7 @@
</div>

<%= renderTemplate('_js_base') %>
<%= renderTemplate('_preload_roboto') %>

<script type="module" crossorigin="use-credentials">
import "<%= latestPageJS %>";
Expand Down
13 changes: 12 additions & 1 deletion src/layouts/home-assistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,18 @@ export class HomeAssistantAppEl extends HassElement {

protected async _initialize() {
try {
const { auth, conn } = await window.hassConnection;
let result;

if (window.hassConnection) {
result = await window.hassConnection;
} else {
// In the edge case that
result = await new Promise((resolve) => {
window.hassConnectionReady = resolve;
});
}

const { auth, conn } = result;
this._haVersion = conn.haVersion;
this.initializeHass(auth, conn);
} catch (err) {
Expand Down

0 comments on commit aa9a354

Please sign in to comment.