Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

service worker doesn't work on vite #20

Closed
aidenybai opened this issue Jan 17, 2025 · 4 comments
Closed

service worker doesn't work on vite #20

aidenybai opened this issue Jan 17, 2025 · 4 comments

Comments

@aidenybai
Copy link
Owner

rank bippy import below react, even though service worker file exists, not registering

repro: https://stackblitz.com/edit/vitejs-vite-ywcbkokx?file=src%2Fmain.tsx,src%2FApp.tsx&terminal=dev

@Asuka109
Copy link
Contributor

Are you trying to set up the RDH in a service worker instance, or is it something else?

bippy/src/sw.ts

Lines 1 to 28 in 572c78b

/// <reference lib="webworker" />.
import './types.js';
(() => {
try {
const NO_OP = () => {};
const renderers = new Map();
let id = 0;
globalThis.__REACT_DEVTOOLS_GLOBAL_HOOK__ = {
checkDCE: NO_OP,
supportsFiber: true,
supportsFlight: true,
hasUnsupportedRendererAttached: false,
renderers,
onCommitFiberRoot: NO_OP,
onCommitFiberUnmount: NO_OP,
onPostCommitFiberRoot: NO_OP,
inject(renderer) {
const nextID = ++id;
renderers.set(nextID, renderer);
globalThis.__REACT_DEVTOOLS_GLOBAL_HOOK__._instrumentationIsActive = true;
return nextID;
},
_instrumentationIsActive: false,
};
} catch {}
})();

@Asuka109
Copy link
Contributor

I think a common approach is to intercept and parse all HTML requests so that you can inject an initialization script tag into them if you want to use a service worker to register RDH with high priority. As the service worker instance has its own context, the RDH registered here will not affect js context from the main thread.

@pivanov
Copy link
Collaborator

pivanov commented Jan 22, 2025

Unfortunately, Vite doesn't have a built-in way to specify module load order without plugins or build configuration.

@Asuka109
Copy link
Contributor

Is it acceptable to build a unplugin so that we can add an import 'bippy' statement to the beginning of the /\/node_modules\/react\/index\.js$? So that bippy can be loaded always before react. And no need to take too much time to write transformer plugins.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants