Skip to content

Commit b291c98

Browse files
authored
fix(clerk-js): Handle removing origin in non-browser environments (#6737)
1 parent 23948dc commit b291c98

4 files changed

Lines changed: 43 additions & 1 deletion

File tree

.changeset/good-loops-make.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/clerk-js': patch
3+
---
4+
5+
Fixes errors in react-native without polyfill for window.location

packages/clerk-js/bundlewatch.config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"files": [
33
{ "path": "./dist/clerk.js", "maxSize": "819KB" },
4-
{ "path": "./dist/clerk.browser.js", "maxSize": "78KB" },
4+
{ "path": "./dist/clerk.browser.js", "maxSize": "79KB" },
55
{ "path": "./dist/clerk.legacy.browser.js", "maxSize": "120KB" },
66
{ "path": "./dist/clerk.headless*.js", "maxSize": "61KB" },
77
{ "path": "./dist/ui-common*.js", "maxSize": "117.1KB" },

packages/clerk-js/src/utils/__tests__/url.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
relativeToAbsoluteUrl,
2121
requiresUserInput,
2222
sanitizeHref,
23+
stripOrigin,
2324
trimLeadingSlash,
2425
trimTrailingSlash,
2526
} from '../url';
@@ -641,3 +642,32 @@ describe('relativeToAbsoluteUrl', () => {
641642
expect(relativeToAbsoluteUrl(relative, origin)).toEqual(new URL(expected));
642643
});
643644
});
645+
646+
describe('stripOrigin(url)', () => {
647+
it('should strip origin when window.location is available', () => {
648+
const originalLocation = window.location;
649+
Object.defineProperty(window, 'location', {
650+
value: { origin: 'https://example.com' },
651+
writable: true,
652+
});
653+
654+
expect(stripOrigin('https://example.com/test?param=1')).toBe('/test?param=1');
655+
expect(stripOrigin('/test')).toBe('/test');
656+
657+
Object.defineProperty(window, 'location', { value: originalLocation });
658+
});
659+
660+
it('should handle undefined window.location gracefully', () => {
661+
const originalLocation = window.location;
662+
Object.defineProperty(window, 'location', {
663+
value: undefined,
664+
writable: true,
665+
});
666+
667+
expect(() => stripOrigin('/test')).not.toThrow();
668+
expect(stripOrigin('/test')).toBe('/test');
669+
expect(stripOrigin('https://example.com/test')).toBe('https://example.com/test');
670+
671+
Object.defineProperty(window, 'location', { value: originalLocation });
672+
});
673+
});

packages/clerk-js/src/utils/url.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,13 @@ export function toURL(url: string | URL): URL {
174174
* @returns {string} Returns the URL href without the origin
175175
*/
176176
export function stripOrigin(url: URL | string): string {
177+
// In non-browser environments `window.location.origin` might not be available
178+
// if not polyfilled, so we can't construct a URL object with the `url` string
179+
// note: in that case, we can't easily strip the origin, so we return the original string
180+
if (typeof window.location === 'undefined' && typeof url === 'string') {
181+
return url;
182+
}
183+
177184
url = toURL(url);
178185
return url.href.replace(url.origin, '');
179186
}

0 commit comments

Comments
 (0)