Skip to content

Commit 7fa0f0f

Browse files
committed
trying to fix firefox
1 parent 6f4e826 commit 7fa0f0f

File tree

4 files changed

+80
-13
lines changed

4 files changed

+80
-13
lines changed

src/cta/components/cta-ui.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,19 @@ export default function BuilderRoot() {
2626
})
2727
}, [dryRun.files])
2828

29+
// Detect Firefox - disable ALS shim for Firefox as it may cause issues
30+
const isFirefox =
31+
typeof navigator !== 'undefined' && navigator.userAgent.includes('Firefox')
32+
console.log(
33+
`🌐 Browser detected - Firefox: ${isFirefox}, ALS Shim: ${!isFirefox}`
34+
)
35+
2936
return (
3037
<CTAProvider>
31-
<WebContainerProvider projectFiles={projectFiles} shouldShimALS={true}>
38+
<WebContainerProvider
39+
projectFiles={projectFiles}
40+
shouldShimALS={!isFirefox}
41+
>
3242
<main className="w-full max-w-full overflow-x-hidden">
3343
<CTABackgroundAnimation />
3444
<div className="h-dvh p-2 sm:p-4 flex flex-col gap-2 sm:gap-4 @container">

src/cta/sandbox/als-shim.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,13 @@ export default AsyncLocalStorage
7878
`
7979

8080
const ALS_SHIM_LOADER = `
81-
function alsShim(): PluginOption {
81+
function alsShim() {
82+
console.log('🔧 ALS Shim plugin loaded');
8283
return {
83-
enforce: 'pre',
84+
enforce: 'pre' as const,
8485
name: 'virtual-async-hooks',
8586
config() {
87+
console.log('🔧 ALS Shim: Configuring virtual async_hooks aliases');
8688
return {
8789
resolve: {
8890
alias: {
@@ -93,12 +95,15 @@ function alsShim(): PluginOption {
9395
},
9496
};
9597
},
96-
resolveId(id) {
97-
if (id === '\\0virtual:async_hooks') return id;
98+
resolveId(id: string) {
99+
if (id === '\\0virtual:async_hooks') {
100+
console.log('🔧 ALS Shim: Resolving virtual async_hooks module');
101+
return id;
102+
}
98103
},
99-
load(id) {
104+
load(id: string) {
100105
if (id !== '\\0virtual:async_hooks') return null;
101-
106+
console.log('🔧 ALS Shim: Loading virtual async_hooks module');
102107
return \`${ALS_SHIM}\`;
103108
},
104109
};

src/cta/sandbox/use-webcontainer-store.ts

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,43 @@ const getDependencies = (packageJsonContent: string): string => {
7979

8080
let webContainer: Promise<WebContainer> | null = null
8181

82+
// Detect Firefox for specific handling
83+
const isFirefox =
84+
typeof navigator !== 'undefined' && navigator.userAgent.includes('Firefox')
85+
8286
export default function createWebContainerStore(shouldShimALS: boolean) {
8387
if (!webContainer) {
84-
webContainer = WebContainer.boot()
88+
console.log(`🌐 Initializing WebContainer (Firefox: ${isFirefox})`)
89+
90+
// Firefox-specific boot with timeout and error handling
91+
webContainer = Promise.race([
92+
WebContainer.boot({
93+
coep: isFirefox ? 'require-corp' : 'credentialless',
94+
} as any),
95+
new Promise<never>((_, reject) => {
96+
const timeout = isFirefox ? 15000 : 10000
97+
setTimeout(() => {
98+
reject(
99+
new Error(
100+
isFirefox
101+
? 'WebContainer boot timeout. Please check Firefox privacy settings (Enhanced Tracking Protection may block Service Workers)'
102+
: 'WebContainer boot timeout'
103+
)
104+
)
105+
}, timeout)
106+
}),
107+
]).catch((err) => {
108+
console.error('WebContainer boot failed:', err)
109+
if (isFirefox) {
110+
console.warn(
111+
'Firefox users: Check that Enhanced Tracking Protection is not blocking Service Workers'
112+
)
113+
console.warn(
114+
'Firefox users: Service Workers are not available in Private Browsing mode'
115+
)
116+
}
117+
throw err
118+
})
85119
}
86120

87121
const store = createStore<WebContainerStore>((set, get) => ({
@@ -134,8 +168,9 @@ export default function createWebContainerStore(shouldShimALS: boolean) {
134168

135169
// Wait for server to be ready (set up listener first)
136170
container.on('server-ready', (port, url) => {
137-
console.log('Server ready on port', port, 'at', url)
171+
console.log('🌐 Server ready on port', port, 'at', url)
138172
const currentState = get()
173+
console.log('🌐 Setting previewUrl:', url)
139174
set({
140175
previewUrl: url,
141176
setupStep: 'ready',
@@ -145,6 +180,7 @@ export default function createWebContainerStore(shouldShimALS: boolean) {
145180
`✅ Server ready at ${url}`,
146181
],
147182
})
183+
console.log('🌐 Preview URL set successfully')
148184
})
149185

150186
// Start the dev server
@@ -168,14 +204,28 @@ export default function createWebContainerStore(shouldShimALS: boolean) {
168204

169205
// Check exit code
170206
const exitCode = await newDevProcess.exit
207+
console.log('⚠️ Dev server process exited with code:', exitCode)
171208
if (exitCode !== 0) {
172209
addTerminalOutput(`❌ Dev server exited with code ${exitCode}`)
173-
set({ error: `Dev server exited with code ${exitCode}` })
210+
console.log('❌ Clearing preview URL due to dev server exit')
211+
set({
212+
error: `Dev server exited with code ${exitCode}`,
213+
previewUrl: null,
214+
setupStep: 'error',
215+
})
216+
} else {
217+
console.log('ℹ️ Dev server exited cleanly (code 0)')
218+
set({ previewUrl: null, devProcess: null })
174219
}
175220
} catch (error) {
176221
console.error('Dev server start error:', error)
177222
addTerminalOutput(`❌ Dev server error: ${(error as Error).message}`)
178-
set({ error: (error as Error).message, setupStep: 'error' })
223+
console.log('❌ Clearing preview URL due to dev server error')
224+
set({
225+
error: (error as Error).message,
226+
setupStep: 'error',
227+
previewUrl: null,
228+
})
179229
}
180230
},
181231
updateProjectFiles: async (

src/cta/sandbox/webcontainer-preview.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,13 @@ export function WebContainerPreview() {
184184
return (
185185
<div className="flex flex-col h-full">
186186
{/* iframe with the running app */}
187-
<div className="flex-1">
187+
<div className="flex-1 flex flex-col">
188188
{previewUrl ? (
189189
<iframe
190190
src={previewUrl}
191-
className="w-full h-full border-0"
191+
className="w-full flex-1 border-0"
192192
title="Application Preview"
193+
allow="cross-origin-isolated"
193194
onLoad={() => console.log('Iframe loaded successfully')}
194195
onError={(e) => console.error('Iframe load error:', e)}
195196
/>
@@ -234,6 +235,7 @@ export function WebContainerPreview() {
234235
<button
235236
onClick={() => window.open(previewUrl, '_blank')}
236237
className="px-2 py-1 text-xs bg-blue-500 text-white rounded hover:bg-blue-600 transition-colors"
238+
title="Open in new tab"
237239
>
238240
Open in New Tab
239241
</button>

0 commit comments

Comments
 (0)