Skip to content

Commit 46f93de

Browse files
committed
fix(transform,react): remove overlay auto-init from Vite preamble
The preamble's overlay import via /node_modules/ path creates a separate module instance from Vite's pre-bundled version, breaking RuntimeManager singleton sharing with the React adapter. This caused componentName to be null in e2e tests. Fix: only set window globals in the preamble. Move overlay auto-init to the react-init virtual module (which uses bare specifiers resolved through Vite's pre-bundling pipeline, sharing singletons correctly). transformIndexHtml continues to handle overlay init for SPA setups.
1 parent 72fad53 commit 46f93de

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

packages/domscribe-react/src/vite/vite-plugin.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ export function domscribe(options?: DomscribeReactPluginOptions): Plugin {
7272
: `const _resolvers = new Map();`;
7373

7474
// Bare specifiers here get rewritten by Vite's transform pipeline
75-
// to pre-bundled paths, matching what the overlay resolves internally
75+
// to pre-bundled paths, matching what the overlay resolves internally.
76+
// This module also handles overlay init as a fallback for SSR setups
77+
// where transformIndexHtml doesn't fire (e.g. React Router 7).
7678
return [
7779
`import { RuntimeManager } from '@domscribe/runtime';`,
7880
`import { createReactAdapter } from '@domscribe/react';`,
@@ -92,6 +94,13 @@ export function domscribe(options?: DomscribeReactPluginOptions): Plugin {
9294
` hookNameResolvers: _resolvers,`,
9395
` }),`,
9496
`}).catch(e => console.warn('[domscribe] Failed to init React runtime:', e.message));`,
97+
``,
98+
`// Init overlay if configured (SSR fallback — transformIndexHtml may not fire).`,
99+
`// Uses bare specifier so Vite resolves to the same pre-bundled module instance`,
100+
`// that RuntimeManager uses, ensuring singleton sharing.`,
101+
`if (typeof window !== 'undefined' && window.__DOMSCRIBE_OVERLAY_OPTIONS__) {`,
102+
` import('@domscribe/overlay').then(m => m.initOverlay()).catch(() => {});`,
103+
`}`,
95104
].join('\n');
96105
}
97106
return baseLoad?.call(this, id, ...args) ?? null;

packages/domscribe-transform/src/plugins/vite/vite.plugin.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,14 @@ function buildVitePreamble(opts: {
7272
);
7373
}
7474

75-
// Auto-init overlay (once per page load) for SSR scenarios where
76-
// transformIndexHtml never fires
77-
let autoInit = '';
78-
if (opts.overlayEnabled && opts.relayPort !== undefined) {
79-
autoInit =
80-
`if(!window.__DOMSCRIBE_AUTO_INIT__){` +
81-
`window.__DOMSCRIBE_AUTO_INIT__=true;` +
82-
`import('/node_modules/@domscribe/overlay/index.js').then(function(m){return m.initOverlay()}).catch(function(){})` +
83-
`}`;
84-
}
85-
86-
return (
87-
`if(typeof window!=='undefined'){${parts.join(';')};` + autoInit + `}\n`
88-
);
75+
// Overlay auto-init is NOT done here — it's handled by:
76+
// • transformIndexHtml (SPA setups)
77+
// • Framework adapter virtual modules (SSR setups, e.g. react-init.js)
78+
// Importing the overlay from the preamble via /node_modules/ path creates
79+
// a separate module instance from Vite's pre-bundled version, which breaks
80+
// RuntimeManager singleton sharing with the adapter.
81+
82+
return `if(typeof window!=='undefined'){${parts.join(';')}}\n`;
8983
}
9084

9185
/**

0 commit comments

Comments
 (0)