fix(v3): prevent "Unexpected token '<'" error from missing custom.js in dev mode#5070
Draft
fix(v3): prevent "Unexpected token '<'" error from missing custom.js in dev mode#5070
Conversation
In dev mode with Vite, loadOptionalScript('/wails/custom.js') was causing
a 'SyntaxError: Unexpected token' error because Vite's SPA fallback
returns HTTP 200 with index.html content (Content-Type: text/html) when
the file doesn't exist.
Fix #1 (defence-in-depth): Add Content-Type check in loadOptionalScript.
Only load the script if the server responds with a JavaScript content
type (application/javascript or text/javascript). Updated the TypeScript
source (index.ts) and both pre-built bundles (runtime.debug.js and
runtime.js).
Fix #2 (server-side): Add a 404 handler for /wails/custom.js in the
asset server middleware (application.go). This intercepts the request
before it reaches the Vite dev server SPA fallback. custom.js is only
meaningful in server mode, which has its own HTTP mux handler for it.
Co-authored-by: leaanthony <1943904+leaanthony@users.noreply.github.com>
Agent-Logs-Url: https://github.com/wailsapp/wails/sessions/13291f55-6fb5-4e7d-8728-e4ed5f7f6f90
Deploying wails with
|
| Latest commit: |
d73d9dd
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://d3dcce8f.wails.pages.dev |
| Branch Preview URL: | https://copilot-fix-unexpected-token.wails.pages.dev |
Copilot
AI
changed the title
[WIP] Fix unexpected token error caused by missing custom.js
fix(v3): prevent "Unexpected token '<'" error from missing custom.js in dev mode
Mar 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When
/wails/custom.jsdoesn't exist, Vite's SPA fallback returns HTTP 200 withindex.html(Content-Type: text/html) instead of 404. The runtime'sloadOptionalScriptonly checkedresponse.ok, so it injected a<script>tag pointing at HTML content, causingSyntaxError: Unexpected token '<'atcustom.js:1.Changes
v3/internal/runtime/desktop/@wailsio/runtime/src/index.ts—loadOptionalScriptnow validatesContent-Typebefore injecting the script tag; onlyapplication/javascriptortext/javascriptresponses proceed:v3/internal/assetserver/bundledassets/runtime.debug.js/runtime.js— Pre-built bundles updated with the same fix.v3/pkg/application/application.go— Asset server middleware now interceptsGET /wails/custom.jsand returns 404 in non-server mode.custom.jsis only meaningful in server mode, which has its own mux handler for that route; this prevents the request from ever reaching Vite's SPA fallback.Type of change
How Has This Been Tested?
Code inspection and tracing the request path through the Vite dev proxy → Go asset server middleware → runtime JS. The two fixes are defence-in-depth: the Go 404 stops the bad request at the server; the Content-Type check in the runtime is a client-side guard for any environment where the server returns a non-JS 200.
Test Configuration
N/A
Checklist:
website/src/pages/changelog.mdxwith details of this PROriginal prompt
This section details on the original issue you should resolve
<issue_title>[v3] "Unexpected token '<'" caused by missing file 'custom.js'</issue_title>
<issue_description>### Description
In dev mode, I get an error in the web inspector "[Error] SyntaxError: Unexpected token '<' (anonymous function) (custom.js:1)". Looking at the contents of the served
custom.jsit seems to contain a duplicate of the source inindex.html(which starts with "<", producing the error).I traced this to line 68 of
frontend/node_modules/@wailsio/runtime/dist/index.js:// Load custom.js if available (used by server mode for WebSocket events, etc.)loadOptionalScript('/wails/custom.js');(The following is my understanding about the situation. But I'm not an expert so it can be wrong.)
loadOptionalScript('/wails/custom.js');is instructing Vite to serve the filefrontend/dist/wails/custom.js, and if it is not available Vite falls back to servingindex.html. This is not javascript, hence the error.In my sourcecode there is no folder
frontend/dist/wails, nor any file atfrontend/dist/wails/custom.js. Creating the folder and file once solves the problem. In fact—if I then try to delete the folder and file, they reappear during compilation.To Reproduce
I'm not sure how to reproduce this since deleting the folder and file does not reset to the previous state where
custom.jscannot be served. I'm hoping that my description of the problem still makes it possible to suss out the cause, or at least how to provide some redundant check for the existence of the folder and file.Expected behaviour
Suggestion: Check for the existence of
custom.jsbefore serving it. Then, if it's missing, either create it or don't serve it.Screenshots
Attempted Fixes
No response
System Details
Wails (v3.0.0-alpha.74) Wails Doctor # System ┌──────────────────────────────────────────────────┐ | Name | MacOS | | Version | 26.3.1 | | ID | 25D2128 | | Branding | MacOS 26.3.1 | | Platform | darwin | | Architecture | arm64 | | Apple Silicon | true | | CPU | Apple M3 | | CPU 1 | Apple M3 | | CPU 2 | Apple M3 | | GPU | 10 cores, Metal Support: Metal 4 | | Memory | 16 GB | └──────────────────────────────────────────────────┘ # Build Environment ┌──────────────────────────────────────────────────────────────────────┐ | Wails CLI | v3.0.0-alpha.74 | | Go Version | go1.26.0 | | -buildmode | exe | | -compiler | gc | | CGO_CFLAGS | | | CGO_CPPFLAGS | | | CGO_CXXFLAGS | | | CGO_ENABLED | 1 | | CGO_LDFLAGS | | | DefaultGODEBUG | cryptocustomrand=1,tlssecpmlkem=0,urlstrictcolons=0 | | GOARCH | arm64 | | GOARM64 | v8.0 | | GOOS | darwin | └──────────────────────────────────────────────────────────────────────┘ # Dependencies ┌────────────────────────────────────────────────────────────────────────┐ | Xcode cli tools | 2416 | | npm | 11.12.0 | | *NSIS | Not Installed. Install with `brew install makensis`. | | docker | *Not installed (optional - for cross-compilation) | | | └─────────────────────── * - Optional Dependency ────────────────────────┘ # Checking for issues SUCCESS No issues found # Diagnosis SUCCESS Your system is ready for Wails development!Additional context
No response</issue_description>
Comments on the Issue (you are @copilot in this section)
<comments...
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.