From 0fbeea4ec9e21dfca80a61d1c2e73055caf4a381 Mon Sep 17 00:00:00 2001 From: Rob Gietema Date: Fri, 3 May 2024 15:44:15 +0200 Subject: [PATCH] Fix 301 and 302 redirects. (#5999) --- packages/volto/news/5999.bugfix | 1 + packages/volto/src/components/theme/View/View.jsx | 5 +++++ packages/volto/src/helpers/Api/Api.js | 2 +- .../volto/src/helpers/Api/Api.plone.rest.test.js | 14 ++++++++------ packages/volto/src/helpers/Api/Api.test.js | 14 ++++++++------ 5 files changed, 23 insertions(+), 13 deletions(-) create mode 100644 packages/volto/news/5999.bugfix diff --git a/packages/volto/news/5999.bugfix b/packages/volto/news/5999.bugfix new file mode 100644 index 0000000000..949e8b88f7 --- /dev/null +++ b/packages/volto/news/5999.bugfix @@ -0,0 +1 @@ +Fix 301 and 302 redirects. @robgietema \ No newline at end of file diff --git a/packages/volto/src/components/theme/View/View.jsx b/packages/volto/src/components/theme/View/View.jsx index 418897e5f0..f841cdcbb7 100644 --- a/packages/volto/src/components/theme/View/View.jsx +++ b/packages/volto/src/components/theme/View/View.jsx @@ -209,6 +209,11 @@ class View extends Component { if (this.props.error && this.props.error.code === 301) { const redirect = flattenToAppURL(this.props.error.url).split('?')[0]; return ; + } else if (this.props.error && this.props.error.status === 302) { + const redirect = flattenToAppURL( + this.props.error.response.header.location, + ).split('?')[0]; + return ; } else if (this.props.error && !this.props.connectionRefused) { let FoundView; if (this.props.error.status === undefined) { diff --git a/packages/volto/src/helpers/Api/Api.js b/packages/volto/src/helpers/Api/Api.js index c91a667ea4..4a5c81d546 100644 --- a/packages/volto/src/helpers/Api/Api.js +++ b/packages/volto/src/helpers/Api/Api.js @@ -54,7 +54,7 @@ class Api { ) => { let request; let promise = new Promise((resolve, reject) => { - request = superagent[method](formatUrl(path)); + request = superagent[method](formatUrl(path)).redirects(0); if (params) { request.query(params); diff --git a/packages/volto/src/helpers/Api/Api.plone.rest.test.js b/packages/volto/src/helpers/Api/Api.plone.rest.test.js index 80e18682d1..81fa1104b2 100644 --- a/packages/volto/src/helpers/Api/Api.plone.rest.test.js +++ b/packages/volto/src/helpers/Api/Api.plone.rest.test.js @@ -3,12 +3,14 @@ import Api from './Api'; jest.mock('superagent', () => ({ get: jest.fn((url) => ({ - url, - query: jest.fn(), - set: jest.fn(), - type: jest.fn(), - send: jest.fn(), - end: jest.fn(), + redirects: jest.fn(() => ({ + url, + query: jest.fn(), + set: jest.fn(), + type: jest.fn(), + send: jest.fn(), + end: jest.fn(), + })), })), })); diff --git a/packages/volto/src/helpers/Api/Api.test.js b/packages/volto/src/helpers/Api/Api.test.js index c877f1724f..2cd2bcd6bb 100644 --- a/packages/volto/src/helpers/Api/Api.test.js +++ b/packages/volto/src/helpers/Api/Api.test.js @@ -9,12 +9,14 @@ import Api from './Api'; jest.mock('superagent', () => ({ get: jest.fn((url) => ({ - url, - query: jest.fn(), - set: jest.fn(), - type: jest.fn(), - send: jest.fn(), - end: jest.fn(), + redirects: jest.fn(() => ({ + url, + query: jest.fn(), + set: jest.fn(), + type: jest.fn(), + send: jest.fn(), + end: jest.fn(), + })), })), }));