Skip to content
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

chore(shared): Browser global accessors [SDK-293] #2041

Closed
wants to merge 2 commits into from
Closed

Conversation

tmilewski
Copy link
Member

Description

Create an accessor for browser globals such that they fail on non-browser environments.

import { browser } from '@clerk/shared'

console.log(browser.window) // throws on non-browser envs
console.log(browser.document) // throws on non-browser envs

I have follow-up work, converting call-sites to this new process, but I ultimately think we should update as-needed and in-line with other work to avoid potentially missed issues per cached modules in Jest.

SDK-293

Checklist

  • npm test runs as expected.
  • npm run build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

Packages affected

  • @clerk/backend
  • @clerk/chrome-extension
  • @clerk/clerk-js
  • @clerk/clerk-expo
  • @clerk/fastify
  • gatsby-plugin-clerk
  • @clerk/localizations
  • @clerk/nextjs
  • @clerk/clerk-react
  • @clerk/remix
  • @clerk/clerk-sdk-node
  • @clerk/shared
  • @clerk/themes
  • @clerk/types
  • build/tooling/chore

@tmilewski tmilewski self-assigned this Nov 3, 2023
@tmilewski tmilewski requested a review from a team as a code owner November 3, 2023 20:58
Copy link

changeset-bot bot commented Nov 3, 2023

⚠️ No Changeset found

Latest commit: 3d2209a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

*/
export const browser = inBrowser()
? rawBrowserGlobals
: new Proxy(rawBrowserGlobals, {
Copy link
Member

Choose a reason for hiding this comment

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

@tmilewski Could you please share some context here? Have we set up any specific test environments / eslint config to catch issues like these without testing with an actual expo app?

Unless I'm missing something, trying to access these in non-browser environments would result in errors even without the Proxy here.

@@ -52,8 +78,8 @@ export function userAgentIsRobot(userAgent: string): boolean {
* @returns {boolean}
*/
export function isValidBrowser(): boolean {
const navigator = window?.navigator;
if (!inBrowser() || !navigator) {
const navigator = browser.window.navigator;
Copy link
Member

Choose a reason for hiding this comment

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

This changes the behavior - instead of returning false we're now throwing. Is this expected?

if (!inBrowser() || !navigator) {
const navigator = browser.window.navigator;

if (!navigator) {
Copy link
Member

Choose a reason for hiding this comment

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

Ditto.
This refactor for these two cases might be fine, however I don't think we can simply replace the usage of the inBrowser() helper with the new proxy-based approach as the two are not equivalent.

Copy link
Member

Choose a reason for hiding this comment

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

Thinking about this a bit more, if this piece of code runs in an expo app, instead of returning false, its going to throw and stop app execution upon accessing browser.window

@SokratisVidros
Copy link
Contributor

It will be great as part of this task to add some linter or build rules for Expo so that building fails if any of these are used in Expo during development.

@tmilewski
Copy link
Member Author

Closing in favor of implementing linting rules.

@tmilewski tmilewski closed this Nov 17, 2023
@tmilewski tmilewski deleted the tom/sdk-293 branch March 29, 2024 13:37
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.

4 participants