Skip to content

Commit

Permalink
fix: set a fallback where we define window.strapi.backendURL
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaellis committed Sep 12, 2023
1 parent 0bd5f29 commit 4d92b11
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 15 deletions.
8 changes: 7 additions & 1 deletion packages/core/admin/admin/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import plugins from './plugins';
import appReducers from './reducers';

window.strapi = {
backendURL: process.env.STRAPI_ADMIN_BACKEND_URL,
/**
* This ENV variable is passed from the strapi instance, by default no url is set
* in the config and therefore the instance returns you an empty string so URLs are relative.
*
* To ensure that the backendURL is always set, we use the window.location.origin as a fallback.
*/
backendURL: process.env.STRAPI_ADMIN_BACKEND_URL || window.location.origin,
isEE: false,
telemetryDisabled: process.env.STRAPI_TELEMETRY_DISABLED ?? false,
features: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const appendSearchParamsToUrl = ({ url, params }) => {
return url;
}

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

Object.entries(params).forEach(([key, value]) => {
if (value !== undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,5 @@ 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;
});
});
});

1 comment on commit 4d92b11

@strapi-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit has been mentioned on Strapi Community Forum. There might be relevant details there:

https://forum.strapi.io/t/media-library-url-error/31861/8

Please sign in to comment.