mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-12 08:47:31 +00:00
Service Worker and Web Worker workarounds
This commit is contained in:
13
src/index.ts
13
src/index.ts
@@ -1,18 +1,23 @@
|
|||||||
declare module '@webcomponents/custom-elements';
|
declare module '@webcomponents/custom-elements';
|
||||||
|
|
||||||
|
// Patch Worker to ignore `importScripts("x.css")` generated by Parcel:
|
||||||
|
const W = self.Worker;
|
||||||
|
self.Worker = function (url: string | URL, options?: WorkerOptions) {
|
||||||
|
const code = `importScripts = (function(){return this.apply(self,[].slice.call(arguments).filter(function(x){return !/\\.css$/i.test(x)}))}).bind(importScripts)\nimportScripts(${JSON.stringify(url)})`;
|
||||||
|
return new W(URL.createObjectURL(new Blob([code], { type: 'text/javascript' })), options);
|
||||||
|
} as any as (typeof Worker);
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
require('./init-app.tsx');
|
require('./init-app.tsx');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!('customElements' in self)) {
|
if (!('customElements' in self)) {
|
||||||
import(
|
import('@webcomponents/custom-elements').then(init);
|
||||||
/* webpackChunkName: "wc-polyfill" */
|
|
||||||
'@webcomponents/custom-elements').then(init);
|
|
||||||
} else {
|
} else {
|
||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof PRERENDER === 'undefined') {
|
if (typeof process.env.PRERENDER === 'undefined') {
|
||||||
// Determine the current display mode.
|
// Determine the current display mode.
|
||||||
let displayMode = 'browser';
|
let displayMode = 'browser';
|
||||||
const mqStandAlone = '(display-mode: standalone)';
|
const mqStandAlone = '(display-mode: standalone)';
|
||||||
|
|||||||
@@ -60,12 +60,14 @@ export function getSharedImage(): Promise<File> {
|
|||||||
/** Set up the service worker and monitor changes */
|
/** Set up the service worker and monitor changes */
|
||||||
export async function offliner(showSnack: SnackBarElement['showSnackbar']) {
|
export async function offliner(showSnack: SnackBarElement['showSnackbar']) {
|
||||||
// This needs to be a typeof because Webpack.
|
// This needs to be a typeof because Webpack.
|
||||||
if (typeof PRERENDER === 'boolean') return;
|
if (process.env.PRERENDER) return;
|
||||||
|
|
||||||
if (process.env.NODE_ENV === 'production') {
|
if (process.env.NODE_ENV === 'production') {
|
||||||
navigator.serviceWorker.register('../sw');
|
navigator.serviceWorker.register('../sw');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!navigator.serviceWorker) return;
|
||||||
|
|
||||||
const hasController = !!navigator.serviceWorker.controller;
|
const hasController = !!navigator.serviceWorker.controller;
|
||||||
|
|
||||||
// Look for changes in the controller
|
// Look for changes in the controller
|
||||||
|
|||||||
Reference in New Issue
Block a user