Skip to content

fix(core): When an error occurs during SSR due to using the browser APIs, show an explanation #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

Open
wants to merge 20 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

I add if (isDev) {if (isServer) {...} else {...}} and in there I check if the error mentions window or document for the server

usage of Node.js's process object must be inside an if (isServer) statement.
We could throw a TypeScript error when a user uses process without wrapping it in an if (isServer) statement.
How do you think so

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: 764d3b2

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

Ensure the test correctly verifies error messages thrown within useTask$ by adding a new component and refining assertions
Modify the test to wrap components with ErrorProvider and update the expected error message. This ensures consistent error handling and improves test reliability.
…uard in useTask$

The new rule `scope-use-task` ensures that Node.js APIs (e.g., `process.env`) are only used within `useTask$` when guarded by a server check (e.g., `isServer`). This prevents unsafe usage of Node.js APIs in client-side code. The rule also checks for indirect API usage via function calls and provides appropriate error messages to guide developers in adding the necessary guards.
@JerryWu1234 JerryWu1234 marked this pull request as ready for review May 14, 2025 09:23
@JerryWu1234 JerryWu1234 requested a review from a team as a code owner May 14, 2025 09:23
Refactor test files to include additional scenarios for unsafe API usage in Qwik components. This ensures comprehensive coverage for detecting unsafe API calls in various contexts.
@JerryWu1234 JerryWu1234 self-assigned this May 16, 2025
@JerryWu1234 JerryWu1234 moved this from Backlog to In progress in Qwik Development May 16, 2025
@JerryWu1234 JerryWu1234 moved this from In progress to Waiting For Review in Qwik Development May 16, 2025
@JerryWu1234 JerryWu1234 changed the title fix(core): prevent server-side usage of browser APIs fix(core): When an error occurs during SSR due to using the browser APIs, show an explanation May 16, 2025
@JerryWu1234 JerryWu1234 moved this from Waiting For Review to In progress in Qwik Development May 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In progress
Development

Successfully merging this pull request may close these issues.

2 participants