-
Notifications
You must be signed in to change notification settings - Fork 396
fix(clerk-js): Handle removing origin in non-browser environments #6737
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
Changes from 1 commit
686f2f0
1f622c0
49f8a60
ff7748e
a78ffd7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -174,6 +174,12 @@ export function toURL(url: string | URL): URL { | |||||||||||||||||||||||||||||||||||||||||||
* @returns {string} Returns the URL href without the origin | ||||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||||
export function stripOrigin(url: URL | string): string { | ||||||||||||||||||||||||||||||||||||||||||||
// In non-browser environments `window.location.origin` might not be available | ||||||||||||||||||||||||||||||||||||||||||||
// if not polyfilled, so we can't construct a URL object with the `url` string | ||||||||||||||||||||||||||||||||||||||||||||
if (typeof window.location === 'undefined' && typeof url === 'string') { | ||||||||||||||||||||||||||||||||||||||||||||
return url; | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❓ should we instead handle this in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That was my initial thought, but I think
Happy to change this if you think moving it to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bratsos I don't think this covers the case of passing a fully qualified URL as a string, e.g.
In this case, I would still expect it to strip the origin. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case, if The thing is that An even better solution would probably be to try and detect if the required APIs are available, and work as expected, or detect the runtime and decide what to do based on that, but I think that's an overkill as in most cases you're either in a browser/polyfilled* environment (so *: If you polyfilled the environment in a weird way this can also throw us off. WDYT? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense! Maybe we can adjust the JSDoc comment here to make it explicit that origin won't be stripped in certain non-browser environments. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure: ff7748e |
||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
url = toURL(url); | ||||||||||||||||||||||||||||||||||||||||||||
return url.href.replace(url.origin, ''); | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Fix: guard must not access Current check Apply: export function stripOrigin(url: URL | string): string {
- // In non-browser environments `window.location.origin` might not be available
- // if not polyfilled, so we can't construct a URL object with the `url` string
- if (typeof window.location === 'undefined' && typeof url === 'string') {
- return url;
- }
-
- url = toURL(url);
- return url.href.replace(url.origin, '');
+ // In non-browser environments `window` or `window.location` might not exist.
+ const hasWindowOrigin =
+ typeof window !== 'undefined' && !!window.location && typeof window.location.origin === 'string';
+ if (!hasWindowOrigin) {
+ // Without a reliable current origin, return input unchanged.
+ return typeof url === 'string' ? url : url.href;
+ }
+ const u = typeof url === 'string' ? new URL(url, window.location.origin) : url;
+ return u.href.replace(u.origin, '');
} 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry if Im missing something obvious, but would this be safer?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is intentional as we're expecting that at least a
window
global object will be somehow declared. If it's not I prefer to get an error here, plus we'll have many other places that would break as well. WDYT?Edit: Happy to make this more defensive though if we think it's valuable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
window
is defined in react-native 😬See my comment here https://github.com/clerk/javascript/pull/4095/files#r1742755342