Skip to content

Commit 5a28d32

Browse files
Add better workaraound
1 parent 7fc6e28 commit 5a28d32

File tree

2 files changed

+8
-57
lines changed

2 files changed

+8
-57
lines changed

packages/core/src/fetch/__tests__/lib.test.ts

+1-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, test, expect } from 'vitest';
22

3-
import { mergeRecords, splitUrl } from '../lib';
3+
import { mergeRecords } from '../lib';
44

55
describe('mergeRecords', () => {
66
test('empty to empty object', () => {
@@ -41,21 +41,3 @@ describe('mergeRecords', () => {
4141
});
4242
});
4343
});
44-
45-
describe('Safari 14.0 bug', () => {
46-
test('splitUrl', () => {
47-
const url = 'https://example.com/api?foo=bar';
48-
49-
const split = splitUrl(url);
50-
51-
expect(split).toEqual({
52-
base: 'https://example.com',
53-
path: '/api?foo=bar',
54-
});
55-
56-
const url1 = new URL(url);
57-
const url2 = new URL(split.path, split.base);
58-
59-
expect(url1.href).toBe(url2.href);
60-
});
61-
});

packages/core/src/fetch/lib.ts

+7-38
Original file line numberDiff line numberDiff line change
@@ -93,30 +93,15 @@ export function formatUrl(
9393
urlString = `${url}?${queryString}`;
9494
}
9595

96+
/**
97+
* Workararound for Safari 14.0
98+
* @see https://github.com/igorkamyshev/farfetched/issues/528
99+
*/
100+
const urlArgs = [urlString, urlBase].filter(Boolean) as [string, string];
101+
96102
try {
97-
return new URL(urlString, urlBase);
103+
return new URL(...urlArgs);
98104
} catch (e) {
99-
if (!urlBase) {
100-
try {
101-
/**
102-
* Fallback branch for Safari 14.0
103-
* @see https://github.com/igorkamyshev/farfetched/issues/528
104-
*
105-
* If url is full path, but we're in Safari 14.0, we will have a TypeError for new URL(urlString, undefined)
106-
*
107-
* So we have to manually split url into base and path parts first
108-
*/
109-
const { base, path } = splitUrl(urlString);
110-
111-
return new URL(path, base);
112-
} catch (_e) {
113-
throw configurationError({
114-
reason: 'Invalid URL',
115-
validationErrors: [`"${urlString}" is not valid URL`],
116-
});
117-
}
118-
}
119-
120105
throw configurationError({
121106
reason: 'Invalid URL',
122107
validationErrors: [`"${urlString}" is not valid URL`],
@@ -150,19 +135,3 @@ function clearValue(
150135

151136
return value ?? null;
152137
}
153-
154-
/**
155-
* @see https://github.com/igorkamyshev/farfetched/issues/528
156-
*/
157-
export function splitUrl(urlString: string): { base: string; path: string } {
158-
const urlPattern = /^(https?:\/\/[^\/]+)(\/.*)?$/;
159-
const match = urlString.match(urlPattern);
160-
161-
if (!match) {
162-
throw new Error(`Invalid URL: ${urlString}`);
163-
}
164-
165-
const base = match[1];
166-
const path = match[2] || '';
167-
return { base, path };
168-
}

0 commit comments

Comments
 (0)