mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-11 16:26:20 +00:00
Reducing browser spinner time (#1157)
This commit is contained in:
@@ -14,28 +14,40 @@ import { promises as fs } from 'fs';
|
|||||||
|
|
||||||
import { lookup as lookupMime } from 'mime-types';
|
import { lookup as lookupMime } from 'mime-types';
|
||||||
|
|
||||||
const prefix = 'data-url:';
|
const prefix = /^data-url(-text)?:/;
|
||||||
|
|
||||||
export default function dataURLPlugin() {
|
export default function dataURLPlugin() {
|
||||||
return {
|
return {
|
||||||
name: 'data-url-plugin',
|
name: 'data-url-plugin',
|
||||||
async resolveId(id, importer) {
|
async resolveId(id, importer) {
|
||||||
if (!id.startsWith(prefix)) return;
|
const match = prefix.exec(id);
|
||||||
|
if (!match) return;
|
||||||
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) {
|
async load(id) {
|
||||||
if (!id.startsWith(prefix)) return;
|
const match = prefix.exec(id);
|
||||||
const realId = id.slice(prefix.length);
|
if (!match) return;
|
||||||
|
|
||||||
|
const isText = !!match[1];
|
||||||
|
const realId = id.slice(match[0].length);
|
||||||
this.addWatchFile(realId);
|
this.addWatchFile(realId);
|
||||||
|
|
||||||
const source = await fs.readFile(realId);
|
const source = await fs.readFile(realId);
|
||||||
const type = lookupMime(realId) || 'text/plain';
|
const type = lookupMime(realId) || 'text/plain';
|
||||||
|
|
||||||
return `export default 'data:${type};base64,${source.toString(
|
if (isText) {
|
||||||
'base64',
|
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
lib/omt.ejs
15
lib/omt.ejs
@@ -28,10 +28,17 @@ if (!self.<%- amdFunctionName %>) {
|
|||||||
<% } else { %>
|
<% } else { %>
|
||||||
new Promise(resolve => {
|
new Promise(resolve => {
|
||||||
if ("document" in self) {
|
if ("document" in self) {
|
||||||
const script = document.createElement("script");
|
const link = document.createElement("link");
|
||||||
script.src = uri;
|
link.rel = "preload";
|
||||||
script.onload = resolve;
|
link.as = "script";
|
||||||
document.head.appendChild(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 {
|
} else {
|
||||||
self.nextDefineUri = uri;
|
self.nextDefineUri = uri;
|
||||||
importScripts(uri);
|
importScripts(uri);
|
||||||
|
|||||||
5
missing-types.d.ts
vendored
5
missing-types.d.ts
vendored
@@ -44,6 +44,11 @@ declare module 'data-url:*' {
|
|||||||
export default url;
|
export default url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
declare module 'data-url-text:*' {
|
||||||
|
const url: string;
|
||||||
|
export default url;
|
||||||
|
}
|
||||||
|
|
||||||
declare module 'service-worker:*' {
|
declare module 'service-worker:*' {
|
||||||
const url: string;
|
const url: string;
|
||||||
export default url;
|
export default url;
|
||||||
|
|||||||
@@ -37,8 +37,10 @@ main();
|
|||||||
ga('set', 'transport', 'beacon');
|
ga('set', 'transport', 'beacon');
|
||||||
ga('set', 'dimension1', displayMode);
|
ga('set', 'dimension1', displayMode);
|
||||||
ga('send', 'pageview', '/index.html', { title: 'Squoosh' });
|
ga('send', 'pageview', '/index.html', { title: 'Squoosh' });
|
||||||
// Load the GA script
|
// Load the GA script without keeping the browser spinner going.
|
||||||
const script = document.createElement('script');
|
addEventListener('load', () => {
|
||||||
script.src = 'https://www.google-analytics.com/analytics.js';
|
const script = document.createElement('script');
|
||||||
document.head.appendChild(script);
|
script.src = 'https://www.google-analytics.com/analytics.js';
|
||||||
|
document.head.appendChild(script);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
|
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 11 KiB |
@@ -14,7 +14,7 @@ import smallSectionAsset from 'url:./imgs/info-content/small.svg';
|
|||||||
import simpleSectionAsset from 'url:./imgs/info-content/simple.svg';
|
import simpleSectionAsset from 'url:./imgs/info-content/simple.svg';
|
||||||
import secureSectionAsset from 'url:./imgs/info-content/secure.svg';
|
import secureSectionAsset from 'url:./imgs/info-content/secure.svg';
|
||||||
import logoIcon from 'url:./imgs/demos/icon-demo-logo.png';
|
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 * as style from './style.css';
|
||||||
import type SnackBarElement from 'shared/custom-els/snack-bar';
|
import type SnackBarElement from 'shared/custom-els/snack-bar';
|
||||||
import 'shared/custom-els/snack-bar';
|
import 'shared/custom-els/snack-bar';
|
||||||
|
|||||||
Reference in New Issue
Block a user