Skip to content

fix(v3): resolve 'Unexpected token <' error from missing custom.js#5110

Merged
leaanthony merged 2 commits intov3-alphafrom
v3-alpha-bugfix/issue-5068-custom-js
Apr 15, 2026
Merged

fix(v3): resolve 'Unexpected token <' error from missing custom.js#5110
leaanthony merged 2 commits intov3-alphafrom
v3-alpha-bugfix/issue-5068-custom-js

Conversation

@leaanthony
Copy link
Copy Markdown
Member

@leaanthony leaanthony commented Apr 3, 2026

Summary

  • Adds explicit 404 response for /wails/custom.js in desktop mode asset middleware, preventing the request from reaching Vite/user handlers
  • Adds Content-Type validation in loadOptionalScript to only inject actual JavaScript, not HTML fallback pages
  • Rebuilds runtime bundles via esbuild

Root Cause

In desktop mode, no handler existed for /wails/custom.js. Vite's SPA fallback returned index.html with status 200 for the unknown route. loadOptionalScript only checked response.ok, so it injected HTML as JavaScript, causing the Unexpected token '<' parse error.

Test plan

  • Verify wails3 dev with Vite frontend no longer shows "Unexpected token '<'" in console
  • Verify server mode still loads custom.js correctly (WebSocket events work)
  • Verify desktop mode gracefully skips the optional script without errors

Fixes #5068

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Only injects scripts when the response is OK and confirmed JavaScript, preventing HTML or other content from being loaded as scripts.
    • Returns 404 for missing custom runtime script paths to avoid SPA HTML being served as JS in desktop dev mode.
    • Corrected mismapped dialog and screen behaviors so dialogs and screens appear with the intended types.
  • Documentation

    • Updated unreleased changelog to record these fixes.

Copilot AI review requested due to automatic review settings April 3, 2026 07:03
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 3, 2026

⚠️ Missing Changelog Update

Hi @leaanthony, please update v3/UNRELEASED_CHANGELOG.md with a description of your changes.

This helps us keep track of changes for the next release.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 3, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 6e5fe943-aa68-4b3c-b6d2-2840d7e47ae1

📥 Commits

Reviewing files that changed from the base of the PR and between 052168e and 722ee9a.

📒 Files selected for processing (5)
  • v3/UNRELEASED_CHANGELOG.md
  • v3/internal/assetserver/bundledassets/runtime.debug.js
  • v3/internal/assetserver/bundledassets/runtime.js
  • v3/internal/runtime/desktop/@wailsio/runtime/src/index.ts
  • v3/pkg/application/application.go
✅ Files skipped from review due to trivial changes (1)
  • v3/UNRELEASED_CHANGELOG.md
🚧 Files skipped from review as they are similar to previous changes (2)
  • v3/pkg/application/application.go
  • v3/internal/runtime/desktop/@wailsio/runtime/src/index.ts

Walkthrough

Verifies HEAD responses' case-insensitive Content-Type contains "javascript" before injecting optional scripts, and adds an explicit 404 response for /wails/custom.js. Also updates minified export symbol wiring in bundled runtime.

Changes

Cohort / File(s) Summary
Runtime (source)
v3/internal/runtime/desktop/@wailsio/runtime/src/index.ts
loadOptionalScript(url) now requires response.ok AND a case-insensitive Content-Type containing "javascript" before inserting a <script>; retains silent error handling.
Runtime (bundled/minified)
v3/internal/assetserver/bundledassets/runtime.js
Mirrors the loadOptionalScript Content-Type check. Updated minified export wiring: Screens now maps Neje; Dialogs.Info now maps Nnjn; Dialogs.Warning now maps jnNn.
Application router
v3/pkg/application/application.go
Added explicit branch to respond with http.NotFound() for requests to "/wails/custom.js" instead of letting normal asset routing handle it.
Changelog
v3/UNRELEASED_CHANGELOG.md
Added entry documenting the fix: explicit 404 for /wails/custom.js and case-insensitive Content-Type validation in loadOptionalScript.

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
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Suggested labels

Bug, runtime, v3-alpha, Documentation, size:L

Poem

🐰 I sniff the headers, ears held high,
If "javascript" winks, I give a try.
If not, I hop away, no broken view—
A tidy patch, a safer chew.
Happy hops for code anew 🥕✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 1.02% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely summarizes the main change: fixing the 'Unexpected token <' error caused by missing custom.js.
Description check ✅ Passed The description provides a clear summary, root cause analysis, and test plan, though it does not formally check all template boxes or reference the changelog update checklist.
Linked Issues check ✅ Passed The PR addresses all coding requirements from #5068: explicit 404 for /wails/custom.js, Content-Type validation in loadOptionalScript, and preserves server-mode behavior.
Out of Scope Changes check ✅ Passed All changes (Go middleware, TypeScript runtime logic, runtime bundles, and changelog) are directly related to fixing the custom.js issue and contain no out-of-scope modifications.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch v3-alpha-bugfix/issue-5068-custom-js

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 404 handler for /wails/custom.js in 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 the Content-Type indicates 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.

Comment thread v3/internal/runtime/desktop/@wailsio/runtime/src/index.ts
@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented Apr 3, 2026

Deploying wails with  Cloudflare Pages  Cloudflare Pages

Latest commit: 722ee9a
Status:🚫  Build failed.

View logs

leaanthony and others added 2 commits April 15, 2026 09:44
… 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>
@leaanthony leaanthony force-pushed the v3-alpha-bugfix/issue-5068-custom-js branch from 052168e to 722ee9a Compare April 15, 2026 09:44
@leaanthony leaanthony closed this Apr 15, 2026
@leaanthony leaanthony reopened this Apr 15, 2026
@leaanthony leaanthony merged commit 28831b0 into v3-alpha Apr 15, 2026
105 of 107 checks passed
@leaanthony leaanthony deleted the v3-alpha-bugfix/issue-5068-custom-js branch April 15, 2026 10:20
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.

2 participants