Skip to content

🐛 Fix Unexpected token '<' console spam on Netlify#4332

Merged
clubanderson merged 1 commit intomainfrom
fix/msw-api-fallback
Apr 2, 2026
Merged

🐛 Fix Unexpected token '<' console spam on Netlify#4332
clubanderson merged 1 commit intomainfrom
fix/msw-api-fallback

Conversation

@clubanderson
Copy link
Copy Markdown
Collaborator

Summary

On console.kubestellar.io, unhandled /api/* requests fall through MSW's bypass mode to Netlify's SPA catch-all → returns index.html as 200 OK → .json() throws SyntaxError: Unexpected token '<' → floods the console with errors every minute.

Fix

Replace MSW's onUnhandledRequest: 'bypass' with a custom handler that silently drops unhandled /api/* requests. They fail as network errors (which hooks already handle gracefully) instead of returning misleading HTML 200s.

Non-API requests (fonts, images, external scripts) still pass through normally.

Test plan

  • Build passes with all 5 post-build checks green
  • Visit console.kubestellar.io — no more Unexpected token '<' errors in console

On Netlify (no Go backend), unhandled API requests fall through MSW's
'bypass' mode to Netlify's SPA catch-all, which returns index.html as
200 OK. Every hook calling .json() on this HTML response throws
SyntaxError: Unexpected token '<' — flooding the console with errors.

Fix: Replace MSW's onUnhandledRequest 'bypass' with a custom handler
that silently drops unhandled /api/* requests (letting them fail as
network errors, which hooks already handle) while still bypassing
non-API requests (fonts, images, external scripts).

Signed-off-by: Andrew Anderson <andy@clubanderson.com>
@clubanderson
Copy link
Copy Markdown
Collaborator Author

/lgtm
/approve

Copilot AI review requested due to automatic review settings April 2, 2026 21:09
@kubestellar-prow kubestellar-prow bot added the dco-signoff: yes Indicates the PR's author has signed the DCO. label Apr 2, 2026
@kubestellar-prow
Copy link
Copy Markdown
Contributor

@clubanderson: you cannot LGTM your own PR.

Details

In response to this:

/lgtm
/approve

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@kubestellar-prow
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: clubanderson

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@netlify
Copy link
Copy Markdown

netlify bot commented Apr 2, 2026

Deploy Preview for kubestellarconsole ready!

Name Link
🔨 Latest commit 5a7525f
🔍 Latest deploy log https://app.netlify.com/projects/kubestellarconsole/deploys/69cedb298962490008b4fe96
😎 Deploy Preview https://deploy-preview-4332.console-deploy-preview.kubestellar.io
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@kubestellar-prow kubestellar-prow bot added approved Indicates a PR has been approved by an approver from all required OWNERS files. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Apr 2, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 2, 2026

👋 Hey @clubanderson — thanks for opening this PR!

🤖 This project is developed exclusively using AI coding assistants.

Please do not attempt to code anything for this project manually.
All contributions should be authored using an AI coding tool such as:

This ensures consistency in code style, architecture patterns, test coverage,
and commit quality across the entire codebase.


This is an automated message.

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 aims to stop repeated SyntaxError: Unexpected token '<' console errors on Netlify deployments by changing how MSW handles unhandled requests, specifically preventing unhandled /api/* calls from falling through to Netlify’s SPA catch-all and returning index.html.

Changes:

  • Replaces onUnhandledRequest: 'bypass' with a custom onUnhandledRequest callback.
  • Attempts to suppress unhandled /api/* requests while continuing to passthrough non-API requests.

Comment on lines +15 to +24
onUnhandledRequest(request, print) {
const url = new URL(request.url)
// API calls that MSW doesn't handle should NOT fall through to Netlify's
// SPA catch-all (which returns index.html as 200 OK). That causes
// `SyntaxError: Unexpected token '<'` when code tries `.json()`.
// Silently ignore unhandled /api/* requests — they'll fail with a network
// error which hooks already handle, instead of a misleading HTML 200.
if (url.pathname.startsWith('/api/')) {
return
}
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

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

The early return for /api/* only skips calling print.*(); it doesn’t actually provide a response or force a network error. As a result, unhandled /api/* requests may still be performed against the origin and can still receive Netlify’s index.html 200 OK (so the .json() Unexpected token '<' issue may persist). If the intent is to make unhandled /api/* fail as a network error, add an explicit MSW catch-all handler for /api/* that returns a network error (or a non-HTML error status) instead of relying on onUnhandledRequest.

Copilot uses AI. Check for mistakes.
Comment on lines +25 to +26
// Non-API requests (fonts, images, external scripts) pass through normally
print.warning()
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

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

This changes behavior for non-API unhandled requests: previously onUnhandledRequest: 'bypass' would pass through silently, but now print.warning() will log a warning for every unhandled non-API request. If “pass through normally” is meant to be quiet, consider not calling print.warning() for these (or otherwise restoring silent bypass behavior).

Copilot uses AI. Check for mistakes.
Comment on lines 13 to +16
export async function startMocking(): Promise<void> {
await worker.start({
onUnhandledRequest: 'bypass',
onUnhandledRequest(request, print) {
const url = new URL(request.url)
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

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

This logic only runs when MSW is actually started. The PR description targets console.kubestellar.io, but MSW startup is gated elsewhere (in main.tsx) and currently only checks VITE_DEMO_MODE / netlify.app. Unless MSW is enabled on console.kubestellar.io too, this change won’t affect the reported environment; either widen the gate to include that hostname or adjust the PR description/test plan accordingly.

Copilot uses AI. Check for mistakes.
@clubanderson
Copy link
Copy Markdown
Collaborator Author

🔄 Auto-Applying Copilot Code Review

Copilot code review found 0 code suggestion(s) and 3 general comment(s).

Also address these general comments:

  • web/src/mocks/browser.ts (line 24): The early return for /api/* only skips calling print.*(); it doesn’t actually provide a response or force a networ
  • web/src/mocks/browser.ts (line 26): This changes behavior for non-API unhandled requests: previously onUnhandledRequest: 'bypass' would pass through silen
  • web/src/mocks/browser.ts (line 16): This logic only runs when MSW is actually started. The PR description targets console.kubestellar.io, but MSW startup

Push all fixes in a single commit. Run cd web && npm run build && npm run lint before committing.


Auto-generated by copilot-review-apply workflow.

@clubanderson clubanderson merged commit 35f6670 into main Apr 2, 2026
28 of 33 checks passed
@kubestellar-prow kubestellar-prow bot deleted the fix/msw-api-fallback branch April 2, 2026 21:16
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 2, 2026

Thank you for your contribution! Your PR has been merged.

Check out what's new:

Stay connected: Slack #kubestellar-dev | Multi-Cluster Survey

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. dco-signoff: yes Indicates the PR's author has signed the DCO. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants