Skip to content

fix(browser): guard DOM tracking listeners during SSR - #2208

Merged
Mercy811 merged 16 commits into
amplitude:mainfrom
sshnox:fix/942-guard-autocapture-browser-listeners
Dec 1, 2026
Merged

Mercy811 merged 16 commits into
amplitude:mainfrom
sshnox:fix/942-guard-autocapture-browser-listeners

Conversation

@sshnox

@sshnox sshnox commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #942.

The file-download and form-interaction tracking plugins assume a global returned by getGlobalScope() implements browser event listeners, and read document.readyState during setup. In SSR and server-like environments these assumptions can throw during SDK initialization. The page-view plugin similarly assumes a browser history and addEventListener.

  • Skip DOM-specific tracking setup when document is unavailable.
  • Guard registration of the window load listeners when the global does not implement addEventListener.
  • Guard page-view navigation tracking setup and teardown in non-browser global scopes.
  • Add focused regression tests for globals lacking addEventListener in file-download and form-interaction plugins.

Validation

  • Compared branch against current upstream main: only the five intended source and test files differ.
  • Full Jest/typecheck execution pending CI (not run in this connected GitHub environment).

No changes to browser tracking behavior when DOM APIs are present.


Note

Low Risk
Defensive environment checks with no intended behavior change when browser APIs exist; broad test coverage for SSR paths.

Overview
Adds SSR and server-like environment guards so Amplitude browser tracking plugins no longer throw during SDK init when document, location, history, or addEventListener are missing.

File download and form interaction plugins now return early from setup when document is undefined, and only register the window load listener when addEventListener is actually a function.

The page view plugin introduces an isServerEnvironment flag: setup bails out (no initial page view, no history/popstate hooks), execute passes events through without attribution-to-page-view conversion, and teardown only calls removeEventListener when it exists.

Regression tests simulate missing document, undefined global scope, globals without addEventListener or history, and related edge cases across all three plugins.

Reviewed by Cursor Bugbot for commit e3d7538. Configure here.

@sshnox
sshnox requested a review from a team as a code owner November 26, 2026 12:24

@daniel-graham-amplitude daniel-graham-amplitude left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good. Thanks for the contribution!

@Mercy811

Copy link
Copy Markdown
Contributor

bugbot run

@Mercy811
Mercy811 marked this pull request as draft November 30, 2026 17:45

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

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

please fix the bugbot issue

@sshnox

sshnox commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

please fix the bugbot issue

Thanks for flagging this. I've addressed the SSR issue in the latest commits by preventing initial page-view tracking when browser APIs are unavailable, and added a regression test to cover it.

Could you please take another look when you get a chance? I also noticed that five CI workflows are awaiting maintainer approval.

@Mercy811
Mercy811 marked this pull request as ready for review November 30, 2026 18:42
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-30T18:48:00.120138Z c12c835 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@Mercy811

Copy link
Copy Markdown
Contributor

bugbot run

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c12c835631

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

@Mercy811

Copy link
Copy Markdown
Contributor

overall lgtm! @sshnox could you fix the failing test and add more test for 100% test coverage?

Notes for myself: we cannot have a shared helper function because different plugins requires different APIs. Page view plugin requires history and addEventListener while file download only needs addEventListener.

@Mercy811

Mercy811 commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

bugbot run

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit e3d7538. Configure here.

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

lgtm!

@Mercy811
Mercy811 enabled auto-merge (squash) December 1, 2026 20:14
@Mercy811
Mercy811 merged commit f0db8e4 into amplitude:main Dec 1, 2026
21 checks passed
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.

unhandledRejection: TypeError: globalScope.addEventListener is not a function

3 participants