Skip to content

fix(core): prevent server-side usage of browser APIs #7571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 10 commits into
base: build/v2
Choose a base branch
from

Conversation

JerryWu1234
Copy link
Contributor

@JerryWu1234 JerryWu1234 commented May 9, 2025

What is it?

  • Feature / enhancement
  • Bug
  • Docs / tests / types / typos
  • Infra

Description

Checklist

  • My code follows the developer guidelines of this project
  • I performed a self-review of my own code
  • I added a changeset with pnpm change
  • I made corresponding changes to the Qwik docs
  • I added new tests to cover the fix / functionality

Add error handling for browser API usage during server-side rendering. The changes include:
- Adding a new error code `QError.notUsingBrowserAPiInserver` to handle cases where browser APIs are used on the server.
- Updating the `use-task.spec.tsx` test to catch and verify the new error.
- Modifying `qrl-class.ts` to throw the error when browser APIs are accessed during server-side rendering.

This ensures that developers are aware of and avoid using browser-specific APIs in server-side contexts, preventing runtime errors.
Copy link

changeset-bot bot commented May 9, 2025

🦋 Changeset detected

Latest commit: ff28ef1

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

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

Update the changeset to correctly reflect the version bump as a patch instead of a major version change. This ensures accurate versioning and release notes.
try {
await render(<Cmp />, { debug });
} catch (e: unknown) {
expect((e as Error).message.includes('Code(Q51)')).toBeTruthy;
Copy link
Member

Choose a reason for hiding this comment

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

I think this needs updating? Also test with a different error, check that it does not add the message but still throws

When an error occurs during SSR due to using browser APIs, provide a clearer explanation to help developers understand the issue and how to fix it. This enhances debugging experience and reduces confusion.
Copy link
Contributor

github-actions bot commented May 12, 2025

built with Refined Cloudflare Pages Action

⚡ Cloudflare Pages Deployment

Name Status Preview Last Commit
qwik-docs ✅ Ready (View Log) Visit Preview ff28ef1

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.

2 participants