diff --git a/news/4854.bugfix b/news/4854.bugfix new file mode 100644 index 0000000000..c083e89766 --- /dev/null +++ b/news/4854.bugfix @@ -0,0 +1 @@ +Return a redirect response from Volto server-side rendering if the API request was redirected. @JeffersonBledsoe @mamico diff --git a/src/components/theme/View/View.jsx b/src/components/theme/View/View.jsx index e836e452ab..34905fc38d 100644 --- a/src/components/theme/View/View.jsx +++ b/src/components/theme/View/View.jsx @@ -205,8 +205,10 @@ class View extends Component { */ render() { const { views } = config; - if (this.props.error && this.props.error.code === 301) { - const redirect = flattenToAppURL(this.props.error.url).split('?')[0]; + if ([301, 302].includes(this.props.error?.code)) { + const redirect = flattenToAppURL(this.props.error.url) + .split('?')[0] + .replace('/++api++', ''); return ; } else if (this.props.error && !this.props.connectionRefused) { let FoundView; diff --git a/src/components/theme/View/View.test.jsx b/src/components/theme/View/View.test.jsx index 66d02014c6..338dc192b4 100644 --- a/src/components/theme/View/View.test.jsx +++ b/src/components/theme/View/View.test.jsx @@ -22,6 +22,7 @@ beforeAll(() => { }); config.settings.publicURL = 'https://plone.org'; }); +global.__SERVER__ = true; // eslint-disable-line no-underscore-dangle const mockStore = configureStore(); diff --git a/src/helpers/Api/Api.js b/src/helpers/Api/Api.js index c91a667ea4..57419e116a 100644 --- a/src/helpers/Api/Api.js +++ b/src/helpers/Api/Api.js @@ -80,6 +80,10 @@ class Api { Object.keys(headers).forEach((key) => request.set(key, headers[key])); + if (__SERVER__ && checkUrl && ['get', 'head'].includes(method)) { + request.redirects(0); + } + if (data) { request.send(data); } @@ -104,6 +108,14 @@ class Api { url: request.xhr.responseURL, }); } + + if ([301, 302].includes(err?.status)) { + return reject({ + code: err.status, + url: err.response.headers.location, + }); + } + return err ? reject(err) : resolve(response.body || response.text); }); }); diff --git a/src/helpers/Api/Api.plone.rest.test.js b/src/helpers/Api/Api.plone.rest.test.js index 80e18682d1..71383caecd 100644 --- a/src/helpers/Api/Api.plone.rest.test.js +++ b/src/helpers/Api/Api.plone.rest.test.js @@ -18,6 +18,7 @@ beforeAll(() => { const api = new Api(); const { settings } = config; +global.__SERVER__ = true; // eslint-disable-line no-underscore-dangle test('get request', () => {}); diff --git a/src/helpers/Api/Api.test.js b/src/helpers/Api/Api.test.js index c877f1724f..bb7f072230 100644 --- a/src/helpers/Api/Api.test.js +++ b/src/helpers/Api/Api.test.js @@ -24,6 +24,7 @@ beforeAll(() => { const api = new Api(); const { settings } = config; +global.__SERVER__ = true; // eslint-disable-line no-underscore-dangle test('get request', () => {});