fix: fallback to window.location.href if backendURL is not defined (#17982)

Co-authored-by: Jamie Howard <48524071+jhoward1994@users.noreply.github.com>
This commit is contained in:
Josh 2023-09-11 10:31:21 +01:00 committed by GitHub
parent ef5f997c57
commit ef41a56cf1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 1 deletions

View File

@ -10,7 +10,7 @@ const appendSearchParamsToUrl = ({ url, params }) => {
return url;
}
const urlObj = new URL(url, window.strapi.backendURL);
const urlObj = new URL(url, window.strapi.backendURL ?? window.location.origin);
Object.entries(params).forEach(([key, value]) => {
if (value !== undefined) {

View File

@ -102,5 +102,18 @@ describe('appendSearchParamsToUrl', () => {
`"https://appending-search-params.com/uploads/image.jpg?param1=example1&param2=example2"`
);
});
test("if there's no window.strapi.backendURL, it uses window.location.origin", () => {
const oldBackendURL = window.strapi.backendURL;
window.strapi.backendURL = undefined;
expect(
appendSearchParamsToUrl({ url, params: { updatedAt: updateTime } })
).toMatchInlineSnapshot(
`"http://localhost:1337/uploads/image.jpg?updatedAt=2023-07-19T03%3A00%3A00.000Z"`
);
window.strapi.backendURL = oldBackendURL;
});
});
});