fix(extension): replace dynamic import() with static import in scanner#84
Conversation
Dynamic import() is forbidden in ServiceWorkerGlobalScope by the HTML
spec. Since background.js runs as an MV3 service worker, the lazy
`await import("../dist/zhtw_mcp_wasm.js")` call in scanner.js caused
the WASM scanner to fail immediately with:
import() is disallowed on ServiceWorkerGlobalScope by the HTML specification.
Replace it with a static top-level import (permitted in module service
workers declared with "type": "module") and pass the WASM URL directly
to the init function using the current object-parameter API.
Fixes: sysprog21#83
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
1 issue found across 1 file
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="extension/src/scanner.js">
<violation number="1" location="extension/src/scanner.js:1">
P1: Static top-level import of WASM module bypasses graceful error handling — a missing or invalid WASM bundle now crashes the service worker at module load time instead of being caught and reported as a user-friendly error.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
| @@ -1,20 +1,18 @@ | |||
| let wasmModulePromise; | |||
| let scanTextBinding; | |||
| import init, { scan_text } from "../dist/zhtw_mcp_wasm.js"; | |||
There was a problem hiding this comment.
P1: Static top-level import of WASM module bypasses graceful error handling — a missing or invalid WASM bundle now crashes the service worker at module load time instead of being caught and reported as a user-friendly error.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At extension/src/scanner.js, line 1:
<comment>Static top-level import of WASM module bypasses graceful error handling — a missing or invalid WASM bundle now crashes the service worker at module load time instead of being caught and reported as a user-friendly error.</comment>
<file context>
@@ -1,20 +1,18 @@
-let wasmModulePromise;
-let scanTextBinding;
+import init, { scan_text } from "../dist/zhtw_mcp_wasm.js";
+
+let initPromise;
</file context>
|
@doggy8088 could you help to verify this fix? |
ebe1063 to
4ab5ad9
Compare
|
Thanks for the fix. I reviewed this PR and the direction looks correct. Chrome Extension MV3 module service workers support top-level static I also checked the review concern about error handling. It is true that if So I think this PR is the right fix for #83. If we want to improve the developer experience further, that can be handled separately by making the build/package step or README more explicit about requiring LGTM. |
|
Thank @JackKuo-tw for contributing! |
Problem
Clicking the extension icon fails with:
This happens even after a successful WASM build.
Root Cause
src/scanner.jsloaded the WASM ES module via a dynamicimport():The HTML specification forbids dynamic
import()inside a Service Worker global scope. Sincebackground.jsruns as an MV3 module service worker, this call fails at runtime.Fix
Replace the runtime dynamic
import()with a static top-levelimport, which is permitted in module service workers declared with"type": "module":The
scanTextBindingindirection is removed sincescan_textis now a direct static import.Closes #83