diff --git a/.changeset/good-loops-make.md b/.changeset/good-loops-make.md new file mode 100644 index 00000000000..d447758136d --- /dev/null +++ b/.changeset/good-loops-make.md @@ -0,0 +1,5 @@ +--- +'@clerk/clerk-js': patch +--- + +Fixes errors in react-native without polyfill for window.location diff --git a/packages/clerk-js/bundlewatch.config.json b/packages/clerk-js/bundlewatch.config.json index 77114b834cf..7e34ffb9d3a 100644 --- a/packages/clerk-js/bundlewatch.config.json +++ b/packages/clerk-js/bundlewatch.config.json @@ -1,7 +1,7 @@ { "files": [ { "path": "./dist/clerk.js", "maxSize": "819KB" }, - { "path": "./dist/clerk.browser.js", "maxSize": "78KB" }, + { "path": "./dist/clerk.browser.js", "maxSize": "79KB" }, { "path": "./dist/clerk.legacy.browser.js", "maxSize": "120KB" }, { "path": "./dist/clerk.headless*.js", "maxSize": "61KB" }, { "path": "./dist/ui-common*.js", "maxSize": "117.1KB" }, diff --git a/packages/clerk-js/src/utils/__tests__/url.spec.ts b/packages/clerk-js/src/utils/__tests__/url.spec.ts index cd0f03d2165..695756cb3b3 100644 --- a/packages/clerk-js/src/utils/__tests__/url.spec.ts +++ b/packages/clerk-js/src/utils/__tests__/url.spec.ts @@ -20,6 +20,7 @@ import { relativeToAbsoluteUrl, requiresUserInput, sanitizeHref, + stripOrigin, trimLeadingSlash, trimTrailingSlash, } from '../url'; @@ -641,3 +642,32 @@ describe('relativeToAbsoluteUrl', () => { expect(relativeToAbsoluteUrl(relative, origin)).toEqual(new URL(expected)); }); }); + +describe('stripOrigin(url)', () => { + it('should strip origin when window.location is available', () => { + const originalLocation = window.location; + Object.defineProperty(window, 'location', { + value: { origin: 'https://example.com' }, + writable: true, + }); + + expect(stripOrigin('https://example.com/test?param=1')).toBe('/test?param=1'); + expect(stripOrigin('/test')).toBe('/test'); + + Object.defineProperty(window, 'location', { value: originalLocation }); + }); + + it('should handle undefined window.location gracefully', () => { + const originalLocation = window.location; + Object.defineProperty(window, 'location', { + value: undefined, + writable: true, + }); + + expect(() => stripOrigin('/test')).not.toThrow(); + expect(stripOrigin('/test')).toBe('/test'); + expect(stripOrigin('https://example.com/test')).toBe('https://example.com/test'); + + Object.defineProperty(window, 'location', { value: originalLocation }); + }); +}); diff --git a/packages/clerk-js/src/utils/url.ts b/packages/clerk-js/src/utils/url.ts index 38949715511..695433efe2b 100644 --- a/packages/clerk-js/src/utils/url.ts +++ b/packages/clerk-js/src/utils/url.ts @@ -174,6 +174,13 @@ 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 + // note: in that case, we can't easily strip the origin, so we return the original string + if (typeof window.location === 'undefined' && typeof url === 'string') { + return url; + } + url = toURL(url); return url.href.replace(url.origin, ''); }