Skip to content

Commit

Permalink
Reducing browser spinner time (#1157)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakearchibald committed Sep 20, 2021
1 parent 9663c23 commit cad0916
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 27 deletions.
28 changes: 20 additions & 8 deletions lib/data-url-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,40 @@ import { promises as fs } from 'fs';

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

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

export default function dataURLPlugin() {
return {
name: 'data-url-plugin',
async resolveId(id, importer) {
if (!id.startsWith(prefix)) return;
const match = prefix.exec(id);
if (!match) return;
return (
prefix + (await this.resolve(id.slice(prefix.length), importer)).id
match[0] + (await this.resolve(id.slice(match[0].length), importer)).id
);
},
async load(id) {
if (!id.startsWith(prefix)) return;
const realId = id.slice(prefix.length);
const match = prefix.exec(id);
if (!match) return;

const isText = !!match[1];
const realId = id.slice(match[0].length);
this.addWatchFile(realId);

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

return `export default 'data:${type};base64,${source.toString(
'base64',
)}';`;
if (isText) {
const encodedBody = encodeURIComponent(source.toString('utf8'));

return `export default ${JSON.stringify(
`data:${type};charset=utf-8,${encodedBody}`,
)};`;
}

return `export default ${JSON.stringify(
`data:${type};base64,${source.toString('base64')}`,
)};`;
},
};
}
15 changes: 11 additions & 4 deletions lib/omt.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,17 @@ if (!self.<%- amdFunctionName %>) {
<% } else { %>
new Promise(resolve => {
if ("document" in self) {
const script = document.createElement("script");
script.src = uri;
script.onload = resolve;
document.head.appendChild(script);
const link = document.createElement("link");
link.rel = "preload";
link.as = "script";
link.href = uri;
link.onload = () => {
const script = document.createElement("script");
script.src = uri;
script.onload = resolve;
document.head.appendChild(script);
};
document.head.appendChild(link);
} else {
self.nextDefineUri = uri;
importScripts(uri);
Expand Down
5 changes: 5 additions & 0 deletions missing-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ declare module 'data-url:*' {
export default url;
}

declare module 'data-url-text:*' {
const url: string;
export default url;
}

declare module 'service-worker:*' {
const url: string;
export default url;
Expand Down
10 changes: 6 additions & 4 deletions src/client/initial-app/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ main();
ga('set', 'transport', 'beacon');
ga('set', 'dimension1', displayMode);
ga('send', 'pageview', '/index.html', { title: 'Squoosh' });
// Load the GA script
const script = document.createElement('script');
script.src = 'https://www.google-analytics.com/analytics.js';
document.head.appendChild(script);
// Load the GA script without keeping the browser spinner going.
addEventListener('load', () => {
const script = document.createElement('script');
script.src = 'https://www.google-analytics.com/analytics.js';
document.head.appendChild(script);
});
}
11 changes: 1 addition & 10 deletions src/shared/prerendered-app/Intro/imgs/logo-with-text.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/shared/prerendered-app/Intro/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import smallSectionAsset from 'url:./imgs/info-content/small.svg';
import simpleSectionAsset from 'url:./imgs/info-content/simple.svg';
import secureSectionAsset from 'url:./imgs/info-content/secure.svg';
import logoIcon from 'url:./imgs/demos/icon-demo-logo.png';
import logoWithText from 'url:./imgs/logo-with-text.svg';
import logoWithText from 'data-url-text:./imgs/logo-with-text.svg';
import * as style from './style.css';
import type SnackBarElement from 'shared/custom-els/snack-bar';
import 'shared/custom-els/snack-bar';
Expand Down

0 comments on commit cad0916

Please sign in to comment.