Merge pull request #18028 from strapi/fix/backend-url-can-be-empty-string

This commit is contained in:
Alexandre BODIN 2023-09-12 16:05:49 +02:00 committed by GitHub
commit e22b016617
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 15 deletions

View File

@ -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: {

View File

@ -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) {

View File

@ -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;
});
});
});