Skip to content

Commit 408b787

Browse files
committed
Don't render an error during hydration unless there was also one on the server
1 parent 9bd80ad commit 408b787

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

packages/volto/news/7132.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make sure the server-side rendered content is still shown if there is an error while hydrating a page that did not have a server-side error. @davisagli

packages/volto/src/middleware/api.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,21 +128,25 @@ function sendOnSocket(request) {
128128
* @param {Object} api Api object.
129129
* @returns {Promise} Action promise.
130130
*/
131+
let isHydrating = __CLIENT__ ? true : false;
132+
131133
const apiMiddlewareFactory =
132134
(api) =>
133135
({ dispatch, getState }) =>
134136
(next) =>
135137
(action) => {
136138
const { settings } = config;
137139

138-
const token = getState().userSession.token;
139-
let uploadedFiles = getState().content.uploadedFiles;
140+
const state = getState();
141+
const token = state.userSession.token;
142+
let uploadedFiles = state.content.uploadedFiles;
140143
let isAnonymous = true;
141144
if (token) {
142145
const tokenExpiration = jwtDecode(token).exp;
143146
const currentTime = new Date().getTime() / 1000;
144147
isAnonymous = !token || currentTime > tokenExpiration;
145148
}
149+
const hasExistingError = state.content.get?.error;
146150

147151
if (typeof action === 'function') {
148152
return action(dispatch, getState);
@@ -223,6 +227,7 @@ const apiMiddlewareFactory =
223227
});
224228
actionPromise.then(
225229
(result) => {
230+
isHydrating = false;
226231
if (uploadedFiles !== 0) {
227232
dispatch(updateUploadedFiles(0));
228233
}
@@ -298,6 +303,14 @@ const apiMiddlewareFactory =
298303
}
299304
},
300305
(error) => {
306+
// Make sure an error during hydration
307+
// (for example when serving an archived page)
308+
// doesn't hide the SSR content.
309+
if (isHydrating && !hasExistingError) {
310+
isHydrating = false;
311+
return;
312+
}
313+
301314
// Only SSR can set ECONNREFUSED
302315
if (error.code === 'ECONNREFUSED') {
303316
next({

0 commit comments

Comments
 (0)