Skip to content

fix(v3): prevent "Unexpected token '<'" error from missing custom.js in dev mode#5070

Draft
Copilot wants to merge 2 commits intomasterfrom
copilot/fix-unexpected-token-error
Draft

fix(v3): prevent "Unexpected token '<'" error from missing custom.js in dev mode#5070
Copilot wants to merge 2 commits intomasterfrom
copilot/fix-unexpected-token-error

Conversation

Copy link
Contributor

Copilot AI commented Mar 24, 2026

When /wails/custom.js doesn't exist, Vite's SPA fallback returns HTTP 200 with index.html (Content-Type: text/html) instead of 404. The runtime's loadOptionalScript only checked response.ok, so it injected a <script> tag pointing at HTML content, causing SyntaxError: Unexpected token '<' at custom.js:1.

Changes

  • v3/internal/runtime/desktop/@wailsio/runtime/src/index.tsloadOptionalScript now validates Content-Type before injecting the script tag; only application/javascript or text/javascript responses proceed:
    const isJavaScript = contentType.startsWith('application/javascript') ||
                         contentType.startsWith('text/javascript');
    if (response.ok && isJavaScript) { /* inject */ }
  • 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 intercepts GET /wails/custom.js and returns 404 in non-server mode. custom.js is 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

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

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.

  • Windows
  • macOS
  • Linux

Test Configuration

N/A

Checklist:

  • I have updated website/src/pages/changelog.mdx with details of this PR
  • My code follows the general coding style of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
Original 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.js it seems to contain a duplicate of the source in index.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 file frontend/dist/wails/custom.js, and if it is not available Vite falls back to serving index.html. This is not javascript, hence the error.

In my sourcecode there is no folder frontend/dist/wails, nor any file at frontend/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.js cannot 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.js before serving it. Then, if it's missing, either create it or don't serve it.

Screenshots

Image Image

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.

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
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Mar 24, 2026

Deploying wails with  Cloudflare Pages  Cloudflare Pages

Latest commit: d73d9dd
Status: ✅  Deploy successful!
Preview URL: https://d3dcce8f.wails.pages.dev
Branch Preview URL: https://copilot-fix-unexpected-token.wails.pages.dev

View logs

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
Copilot AI requested a review from leaanthony March 24, 2026 11:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[v3] "Unexpected token '<'" caused by missing file 'custom.js'

2 participants