Skip to content

Commit cad0916

Browse files
Reducing browser spinner time (GoogleChromeLabs#1157)
1 parent 9663c23 commit cad0916

File tree

6 files changed

+44
-27
lines changed

6 files changed

+44
-27
lines changed

lib/data-url-plugin.js

+20-8
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,40 @@ import { promises as fs } from 'fs';
1414

1515
import { lookup as lookupMime } from 'mime-types';
1616

17-
const prefix = 'data-url:';
17+
const prefix = /^data-url(-text)?:/;
1818

1919
export default function dataURLPlugin() {
2020
return {
2121
name: 'data-url-plugin',
2222
async resolveId(id, importer) {
23-
if (!id.startsWith(prefix)) return;
23+
const match = prefix.exec(id);
24+
if (!match) return;
2425
return (
25-
prefix + (await this.resolve(id.slice(prefix.length), importer)).id
26+
match[0] + (await this.resolve(id.slice(match[0].length), importer)).id
2627
);
2728
},
2829
async load(id) {
29-
if (!id.startsWith(prefix)) return;
30-
const realId = id.slice(prefix.length);
30+
const match = prefix.exec(id);
31+
if (!match) return;
32+
33+
const isText = !!match[1];
34+
const realId = id.slice(match[0].length);
3135
this.addWatchFile(realId);
3236

3337
const source = await fs.readFile(realId);
3438
const type = lookupMime(realId) || 'text/plain';
3539

36-
return `export default 'data:${type};base64,${source.toString(
37-
'base64',
38-
)}';`;
40+
if (isText) {
41+
const encodedBody = encodeURIComponent(source.toString('utf8'));
42+
43+
return `export default ${JSON.stringify(
44+
`data:${type};charset=utf-8,${encodedBody}`,
45+
)};`;
46+
}
47+
48+
return `export default ${JSON.stringify(
49+
`data:${type};base64,${source.toString('base64')}`,
50+
)};`;
3951
},
4052
};
4153
}

lib/omt.ejs

+11-4
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,17 @@ if (!self.<%- amdFunctionName %>) {
2828
<% } else { %>
2929
new Promise(resolve => {
3030
if ("document" in self) {
31-
const script = document.createElement("script");
32-
script.src = uri;
33-
script.onload = resolve;
34-
document.head.appendChild(script);
31+
const link = document.createElement("link");
32+
link.rel = "preload";
33+
link.as = "script";
34+
link.href = uri;
35+
link.onload = () => {
36+
const script = document.createElement("script");
37+
script.src = uri;
38+
script.onload = resolve;
39+
document.head.appendChild(script);
40+
};
41+
document.head.appendChild(link);
3542
} else {
3643
self.nextDefineUri = uri;
3744
importScripts(uri);

missing-types.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ declare module 'data-url:*' {
4444
export default url;
4545
}
4646

47+
declare module 'data-url-text:*' {
48+
const url: string;
49+
export default url;
50+
}
51+
4752
declare module 'service-worker:*' {
4853
const url: string;
4954
export default url;

src/client/initial-app/index.tsx

+6-4
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ main();
3737
ga('set', 'transport', 'beacon');
3838
ga('set', 'dimension1', displayMode);
3939
ga('send', 'pageview', '/index.html', { title: 'Squoosh' });
40-
// Load the GA script
41-
const script = document.createElement('script');
42-
script.src = 'https://www.google-analytics.com/analytics.js';
43-
document.head.appendChild(script);
40+
// Load the GA script without keeping the browser spinner going.
41+
addEventListener('load', () => {
42+
const script = document.createElement('script');
43+
script.src = 'https://www.google-analytics.com/analytics.js';
44+
document.head.appendChild(script);
45+
});
4446
}

src/shared/prerendered-app/Intro/imgs/logo-with-text.svg

+1-10
Loading

src/shared/prerendered-app/Intro/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import smallSectionAsset from 'url:./imgs/info-content/small.svg';
1414
import simpleSectionAsset from 'url:./imgs/info-content/simple.svg';
1515
import secureSectionAsset from 'url:./imgs/info-content/secure.svg';
1616
import logoIcon from 'url:./imgs/demos/icon-demo-logo.png';
17-
import logoWithText from 'url:./imgs/logo-with-text.svg';
17+
import logoWithText from 'data-url-text:./imgs/logo-with-text.svg';
1818
import * as style from './style.css';
1919
import type SnackBarElement from 'shared/custom-els/snack-bar';
2020
import 'shared/custom-els/snack-bar';

0 commit comments

Comments
 (0)