Skip to content

Prevent setTimeout from firing if we can't match devtools user agent - #13349

Merged
jerelmiller merged 3 commits into
mainfrom
jerel/devtools-trigger
Jul 23, 2026
Merged

Prevent setTimeout from firing if we can't match devtools user agent#13349
jerelmiller merged 3 commits into
mainfrom
jerel/devtools-trigger

Conversation

@jerelmiller

@jerelmiller jerelmiller commented Jul 23, 2026

Copy link
Copy Markdown
Member

Fixes #13344

The connectToDevtools method inside ApolloClient runs a setTimeout then checks the window.navigator.userAgent to see if it matches a supported platform before logging the message. As #13344 points out, this can be problematic in test environments since that setTimeout can linger. Even worse is when that setTimeout outlives the virtual DOM construction so window doesn't exist anymore which could cause crashes.

This fix checks window.navigator.userAgent outside the setTimeout and prevents it from scheduling if we're in a non Chrome/Firefox environment.

Summary by CodeRabbit

  • Bug Fixes
    • Improved Apollo DevTools extension prompts by displaying them only in supported browser contexts.
    • Prevented unnecessary extension suggestions in embedded frames and unsupported protocols.

@jerelmiller
jerelmiller requested a review from phryneas July 23, 2026 16:14
@apollo-librarian

apollo-librarian Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

✅ Docs preview has no changes

The preview was not built because there were no changes.

Build ID: 789eedbf85d2779c21efa047
Build Logs: View logs


✅ AI Style Review — No Changes Detected

No MDX files were changed in this pull request.

Review Log: View detailed log

This review is AI-generated. Please use common sense when accepting these suggestions, as they may not always be accurate or appropriate for your specific context.

@changeset-bot

changeset-bot Bot commented Jul 23, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 588ab4b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@apollo/client Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro Plus

Run ID: 616c129f-516e-462b-bb86-5f1f8b821942

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

ApolloClient now computes the browser user agent and devtools extension URL before scheduling the delayed suggestion. The suggestion log is gated by top-level browsing context, supported protocols, and URL availability.

Changes

Devtools suggestion control flow

Layer / File(s) Summary
Guard delayed devtools suggestion
src/core/ApolloClient.ts
Precomputes the user agent and extension URL, then restricts the delayed invariant.log call to supported top-level browser contexts with a resolved extension URL.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: phryneas

Poem

A rabbit checks the browser’s door,
No timer hops where windows soar.
A fitting URL marks the way,
The devtools hint waits for its day.
“Safe travels!” says the bun.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The PR guards the devtools timer, but it does not restore MockedProvider's connectToDevTools false default required by #13344. Update MockedProvider to default devtools/connectToDevTools to false, and keep the timer guard as a secondary safeguard.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title matches the main change: gating the devtools suggestion timeout by user-agent support.
Out of Scope Changes check ✅ Passed The only changes are in ApolloClient's devtools path and align with the stated test-environment fix.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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.

@pkg-pr-new

pkg-pr-new Bot commented Jul 23, 2026

Copy link
Copy Markdown
npm i https://pkg.pr.new/@apollo/client@13349

commit: 588ab4b

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/core/ApolloClient.ts (1)

1172-1199: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add regression coverage for the scheduling gate.

Test an unsupported/jsdom user agent to verify that no timeout is registered, then cover Chrome and Firefox user agents to verify the delayed suggestion still logs correctly when the hook is absent.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/core/ApolloClient.ts` around lines 1172 - 1199, Add regression tests
around the scheduling gate in the Apollo client devtools suggestion logic: use
an unsupported or jsdom user agent to assert no timeout is registered, then test
Chrome and Firefox user agents with the devtools hook absent to assert the
delayed suggestion logs correctly. Reuse the existing timer and logging test
utilities, and cover both supported browser branches without changing the
implementation.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/core/ApolloClient.ts`:
- Around line 1173-1195: Harden the ApolloClient devtools detection flow around
the user-agent and protocol checks: guard navigator, document, location, and
window/top/self access before dereferencing them, and ensure the delayed
setTimeout callback safely handles window teardown before reading the devtools
hook. Preserve the existing Chrome/Firefox URL selection and devtools warning
behavior when all browser members are available.

---

Nitpick comments:
In `@src/core/ApolloClient.ts`:
- Around line 1172-1199: Add regression tests around the scheduling gate in the
Apollo client devtools suggestion logic: use an unsupported or jsdom user agent
to assert no timeout is registered, then test Chrome and Firefox user agents
with the devtools hook absent to assert the delayed suggestion logs correctly.
Reuse the existing timer and logging test utilities, and cover both supported
browser branches without changing the implementation.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro Plus

Run ID: e24b4c32-f71d-4c0b-ab1f-b9a28d7e4801

📥 Commits

Reviewing files that changed from the base of the PR and between 4bb6df7 and 86c6e0c.

📒 Files selected for processing (1)
  • src/core/ApolloClient.ts

Comment thread src/core/ApolloClient.ts Outdated
@jerelmiller
jerelmiller merged commit 501a33b into main Jul 23, 2026
50 checks passed
@jerelmiller
jerelmiller deleted the jerel/devtools-trigger branch July 23, 2026 17:27
@github-actions github-actions Bot mentioned this pull request Jul 23, 2026
jerelmiller pushed a commit that referenced this pull request Jul 23, 2026
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## @apollo/client@4.2.8

### Patch Changes

- [#13349](#13349)
[`501a33b`](501a33b)
Thanks [@jerelmiller](https://github.com/jerelmiller)! - Prevent the
`setTimeout` in `connectToDevtools` that shows the devtools suggestion
from firing when the user agent does not match Chrome or Firefox. This
check was previously done inside the `setTimeout` which meant the timer
was scheduled for environments where we'd never show the message
anyways. For test environments, this could cause flaky tests when that
`setTimeout` outlived the tests and ran after any virtual DOM was torn
down and removed.

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant