fix(v3): resolve 'Unexpected token <' error from missing custom.js#5110
fix(v3): resolve 'Unexpected token <' error from missing custom.js#5110leaanthony merged 2 commits intov3-alphafrom
Conversation
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (5)
✅ Files skipped from review due to trivial changes (1)
🚧 Files skipped from review as they are similar to previous changes (2)
WalkthroughVerifies HEAD responses' case-insensitive Content-Type contains "javascript" before injecting optional scripts, and adds an explicit 404 response for Changes
Sequence Diagram(s)sequenceDiagram
participant Client as Client Runtime
participant DevServer as Dev/Asset Server
participant App as App HTTP Handler
Client->>DevServer: HEAD /wails/custom.js
alt DevServer 200 and Content-Type contains "javascript"
DevServer-->>Client: 200, Content-Type: application/javascript
Client->>Client: insert <script src="/wails/custom.js">
else DevServer 200 with non-JS or 404
DevServer->>App: (if routed) GET /wails/custom.js
App-->>DevServer: 404 Not Found
DevServer-->>Client: 404 or 200 with non-JS
Client->>Client: do not inject script
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Suggested labels
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
This PR addresses the Unexpected token '<' runtime error caused by /wails/custom.js being missing in desktop dev mode (and being served as an HTML SPA fallback), by preventing the HTML fallback from being injected as JavaScript.
Changes:
- Adds an explicit
404handler for/wails/custom.jsin the application asset middleware so the request doesn’t reach Vite/user SPA fallback handlers in desktop mode. - Hardens
loadOptionalScript()to only inject the script when theContent-Typeindicates JavaScript. - Rebuilds the production runtime bundle.
Reviewed changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
v3/pkg/application/application.go |
Intercepts /wails/custom.js and returns 404 to avoid SPA fallbacks being treated as JS. |
v3/internal/runtime/desktop/@wailsio/runtime/src/index.ts |
Adds Content-Type validation before injecting an “optional” script. |
v3/internal/assetserver/bundledassets/runtime.js |
Updates the bundled production runtime output to include the new optional-script behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
… content-type in loadOptionalScript In desktop mode, no handler existed for /wails/custom.js. When using Vite dev server, its SPA fallback returned index.html (200 OK) for the unknown route. loadOptionalScript only checked response.ok, so it injected the HTML as a script tag, causing "Unexpected token '<'" errors. Two-pronged fix: - Server: add explicit 404 response for /wails/custom.js in the desktop asset middleware, preventing the request from reaching user/Vite handlers - Client: validate Content-Type header includes "javascript" before injecting the script tag, guarding against any HTML fallback scenario Fixes #5068 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…t check
Addresses Copilot review comment: Content-Type headers are case-insensitive
but the previous check was case-sensitive. Normalize to lowercase before
calling includes('javascript') to handle headers like Application/JavaScript.
Also adds UNRELEASED_CHANGELOG.md entry for the fix.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
052168e to
722ee9a
Compare
Summary
/wails/custom.jsin desktop mode asset middleware, preventing the request from reaching Vite/user handlersloadOptionalScriptto only inject actual JavaScript, not HTML fallback pagesRoot Cause
In desktop mode, no handler existed for
/wails/custom.js. Vite's SPA fallback returnedindex.htmlwith status 200 for the unknown route.loadOptionalScriptonly checkedresponse.ok, so it injected HTML as JavaScript, causing theUnexpected token '<'parse error.Test plan
wails3 devwith Vite frontend no longer shows "Unexpected token '<'" in consolecustom.jscorrectly (WebSocket events work)Fixes #5068
🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
Documentation