Skip to content

Commit 4d92b11

Browse files
committed
fix: set a fallback where we define window.strapi.backendURL
1 parent 0bd5f29 commit 4d92b11

File tree

3 files changed

+8
-15
lines changed

3 files changed

+8
-15
lines changed

packages/core/admin/admin/src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ import plugins from './plugins';
88
import appReducers from './reducers';
99

1010
window.strapi = {
11-
backendURL: process.env.STRAPI_ADMIN_BACKEND_URL,
11+
/**
12+
* This ENV variable is passed from the strapi instance, by default no url is set
13+
* in the config and therefore the instance returns you an empty string so URLs are relative.
14+
*
15+
* To ensure that the backendURL is always set, we use the window.location.origin as a fallback.
16+
*/
17+
backendURL: process.env.STRAPI_ADMIN_BACKEND_URL || window.location.origin,
1218
isEE: false,
1319
telemetryDisabled: process.env.STRAPI_TELEMETRY_DISABLED ?? false,
1420
features: {

packages/core/upload/admin/src/utils/appendSearchParamsToUrl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const appendSearchParamsToUrl = ({ url, params }) => {
1010
return url;
1111
}
1212

13-
const urlObj = new URL(url, window.strapi.backendURL ?? window.location.origin);
13+
const urlObj = new URL(url, window.strapi.backendURL);
1414

1515
Object.entries(params).forEach(([key, value]) => {
1616
if (value !== undefined) {

packages/core/upload/admin/src/utils/tests/appendSearchParamsToUrl.test.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,5 @@ describe('appendSearchParamsToUrl', () => {
102102
`"https://appending-search-params.com/uploads/image.jpg?param1=example1&param2=example2"`
103103
);
104104
});
105-
106-
test("if there's no window.strapi.backendURL, it uses window.location.origin", () => {
107-
const oldBackendURL = window.strapi.backendURL;
108-
window.strapi.backendURL = undefined;
109-
110-
expect(
111-
appendSearchParamsToUrl({ url, params: { updatedAt: updateTime } })
112-
).toMatchInlineSnapshot(
113-
`"http://localhost:1337/uploads/image.jpg?updatedAt=2023-07-19T03%3A00%3A00.000Z"`
114-
);
115-
116-
window.strapi.backendURL = oldBackendURL;
117-
});
118105
});
119106
});

0 commit comments

Comments
 (0)