Skip to content

🐛 Fix active-users HTML parse errors on Netlify#4325

Merged
clubanderson merged 1 commit intomainfrom
fix/active-users-html-error
Apr 2, 2026
Merged

🐛 Fix active-users HTML parse errors on Netlify#4325
clubanderson merged 1 commit intomainfrom
fix/active-users-html-error

Conversation

@clubanderson
Copy link
Copy Markdown
Collaborator

Summary

On console.kubestellar.io (Netlify), /api/active-users falls through to the SPA catch-all redirect during MSW startup, returning index.html as a 200 response. The hook tries to parse this as JSON → SyntaxError: Unexpected token '<' every minute in the console.

Fix

Added isJsonResponse() guard that checks content-type header before attempting res.json(). Non-JSON responses (HTML fallback) are treated as errors without parse attempt.

Test plan

  • Build passes with all post-build checks green
  • Verify no more Unexpected token '<' errors on console.kubestellar.io

On Netlify (no Go backend), /api/active-users falls through to the SPA
catch-all redirect returning index.html. MSW should intercept these, but
there's a timing gap during MSW startup where fetches fire before the
service worker registers.

The fetch code then tries to parse HTML as JSON, causing repeated
`SyntaxError: Unexpected token '<'` console errors every minute.

Fix: Check response content-type before attempting JSON parse. If the
response is not application/json (e.g., text/html from the SPA
fallback), treat it as an error without attempting to parse.

Signed-off-by: Andrew Anderson <andy@clubanderson.com>
Copilot AI review requested due to automatic review settings April 2, 2026 20:45
@kubestellar-prow kubestellar-prow bot added the dco-signoff: yes Indicates the PR's author has signed the DCO. label Apr 2, 2026
@clubanderson
Copy link
Copy Markdown
Collaborator Author

/lgtm
/approve

@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.

@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.

@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

@kubestellar-prow kubestellar-prow bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. approved Indicates a PR has been approved by an approver from all required OWNERS files. labels Apr 2, 2026
@netlify
Copy link
Copy Markdown

netlify bot commented Apr 2, 2026

Deploy Preview for kubestellarconsole ready!

Name Link
🔨 Latest commit f33acb6
🔍 Latest deploy log https://app.netlify.com/projects/kubestellarconsole/deploys/69ced56a6bbdd700075d31ae
😎 Deploy Preview https://deploy-preview-4325.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.

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

Fixes recurring SyntaxError: Unexpected token '<' console noise on Netlify (console.kubestellar.io) by preventing the /api/active-users hook from attempting response.json() when the response is actually HTML (SPA catch-all index.html).

Changes:

  • Add an isJsonResponse() content-type guard in useActiveUsers to skip JSON parsing on non-JSON responses.
  • Update existing unit tests to include Content-Type: application/json on mocked successful responses.
  • Add a unit test case for the HTML fallback scenario.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
web/src/hooks/useActiveUsers.ts Adds a content-type guard to avoid calling resp.json() on HTML fallback responses.
web/src/hooks/__tests__/useActiveUsers.test.ts Updates fetch mocks to set JSON content-type and adds an HTML-fallback test case.

Comment on lines +199 to +213
// --- Handles HTML fallback response (Netlify SPA catch-all) ---
it('handles HTML response without SyntaxError (Netlify SPA fallback)', async () => {
vi.mocked(fetch).mockResolvedValue(
new Response('<!doctype html><html><body>SPA</body></html>', {
status: 200,
headers: { 'Content-Type': 'text/html' },
})
)

const { result } = renderHook(() => useActiveUsers())
await act(async () => { await vi.advanceTimersByTimeAsync(100) })

// Should not crash or throw SyntaxError — gracefully treated as error
expect(typeof result.current.activeUsers).toBe('number')
})
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 new HTML-fallback test doesn’t actually validate the behavior introduced by this PR. It would have passed even before the isJsonResponse() guard because resp.json().catch(() => null) already prevents the SyntaxError from failing the test. Consider asserting that json() is not called for a text/html response (e.g., return a minimal Response-like mock with headers.get() and a json spy) so the test fails if JSON parsing is attempted again.

Copilot generated this review using guidance from repository custom instructions.
@clubanderson
Copy link
Copy Markdown
Collaborator Author

🔄 Auto-Applying Copilot Code Review

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

Also address these general comments:

  • web/src/hooks/__tests__/useActiveUsers.test.ts (line 213): The new HTML-fallback test doesn’t actually validate the behavior introduced by this PR. It would have passed even befor

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 fcf26c5 into main Apr 2, 2026
28 of 33 checks passed
@kubestellar-prow kubestellar-prow bot deleted the fix/active-users-html-error branch April 2, 2026 20:51
@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/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants