Skip to content

Commit

Permalink
Server-side redirects for plone.app.redirector aliases (#4854)
Browse files Browse the repository at this point in the history
Co-authored-by: Mauro Amico <[email protected]>
Co-authored-by: David Glick <[email protected]>
  • Loading branch information
3 people authored May 20, 2024
1 parent 93c30b2 commit 96cc4b1
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/volto/news/4834.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Return a redirect response from Volto server-side rendering if the API request was redirected. @JeffersonBledsoe @mamico
6 changes: 4 additions & 2 deletions packages/volto/src/components/theme/View/View.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,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 <Redirect to={`${redirect}${this.props.location.search}`} />;
} else if (this.props.error && !this.props.connectionRefused) {
let FoundView;
Expand Down
1 change: 1 addition & 0 deletions packages/volto/src/components/theme/View/View.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ beforeAll(() => {
});
config.settings.publicURL = 'https://plone.org';
});
global.__SERVER__ = true; // eslint-disable-line no-underscore-dangle

const mockStore = configureStore();

Expand Down
12 changes: 12 additions & 0 deletions packages/volto/src/helpers/Api/Api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
});
});
Expand Down
1 change: 1 addition & 0 deletions packages/volto/src/helpers/Api/Api.plone.rest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ beforeAll(() => {

const api = new Api();
const { settings } = config;
global.__SERVER__ = true; // eslint-disable-line no-underscore-dangle

test('get request', () => {});

Expand Down
1 change: 1 addition & 0 deletions packages/volto/src/helpers/Api/Api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ beforeAll(() => {

const api = new Api();
const { settings } = config;
global.__SERVER__ = true; // eslint-disable-line no-underscore-dangle

test('get request', () => {});

Expand Down

0 comments on commit 96cc4b1

Please sign in to comment.