Skip to content

fix(useScriptTag): guard the SSR paths with defaultDocument - #238

Open
rawsun007 wants to merge 1 commit into
childrentime:mainfrom
rawsun007:fix/script-tag-ssr-guard
Open

rawsun007 wants to merge 1 commit into
childrentime:mainfrom
rawsun007:fix/script-tag-ssr-guard

Conversation

@rawsun007

Copy link
Copy Markdown
Contributor

Description

Both SSR guards in useScriptTag read if (!document), commented "SSR Support". On the server document is undeclared, so evaluating it throws before the ! applies:

load()   -> rejects with ReferenceError: document is not defined   (should resolve false)
unload() -> throws  ReferenceError: document is not defined        (should return)

defaultDocument from utils/browser is the existing typeof-based guard, so both sites use that.

Found by rendering every exported hook with renderToString under @jest-environment node and calling each function it returns, the same shape as the existing useOrientation/index.ssr.spec.tsx. The new index.ssr.spec.tsx follows that file. Reverting either guard on its own fails it.

Type of Change

  • Bug fix
  • New hook
  • Enhancement to existing hook
  • Documentation update
  • Other (please describe)

Checklist

  • I have read the Contributing Guide
  • I have read and understood every line of this diff myself, including any AI-written parts (AI usage policy)
  • My code follows the project's coding style
  • I have added tests for my changes
  • All existing tests pass
  • I have updated the documentation

Unticked: documentation (no API change), and the "read and understood every line myself" box, which is @rawsun007's to tick rather than mine. Written with Claude Code (Claude Opus 5.5) on his account; he answers review comments himself.

The same sweep also flagged four other hooks whose returned functions touch a browser global on the server. useRafState's setter calls cancelAnimationFrame unguarded. useCookie, useRafFn and useScrollIntoView do too, but all three are in #205, so I have left them alone.

pnpm jest in packages/core: 432 passing across 75 suites. pnpm lint clean.

🤖 Generated with Claude Code

`if (!document)` throws a ReferenceError when document is undeclared,
which is the server case the guard exists for, so load() rejected and
unload() threw instead of returning. defaultDocument from utils/browser
is undefined on the server.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.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

Development

Successfully merging this pull request may close these issues.

1 participant