Skip to content
Open
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
0cd54e0
fix(server): handle unhandledRejection and catch sync SSR errors to a…
ichim-david Oct 16, 2025
dcb6bfe
fix(UniversalLink): return null for array hrefs and guard external check
ichim-david Oct 16, 2025
6aaab04
fix(server): remove global unhandledRejection and use request-scoped …
ichim-david Oct 16, 2025
db402f5
fix(UniversalLink): guard url before checking for @@display-file to a…
ichim-david Oct 16, 2025
7af4a93
fix(UniversalLink): guard url when checking isInternalURL to avoid er…
ichim-david Oct 17, 2025
a56d7d4
feat(docs): add AGENTS.md with repository guidelines (project overvie…
ichim-david Oct 17, 2025
974ca27
docs(AGENTS): specify Volto 17.20.0+ and Plone 6; expand File Structu…
ichim-david Oct 17, 2025
c6ae82e
fix(print): loading of plotly charts by scrolling to the bottom of th…
ichim-david Oct 20, 2025
c35da3e
fix(print): await all content before printing and use pageDocument sc…
ichim-david Oct 22, 2025
df9d465
update jest addon with improvements made in volto-listing-block
ichim-david Oct 22, 2025
ecff183
test(print): expand setupPrintView tests to cover iframes, images, pl…
ichim-david Oct 22, 2025
cb23dc8
test(user-select-widget): add helpers and expand unit tests
ichim-david Oct 22, 2025
c59da27
move ignoredErrors outside of function to avoid recreation
ichim-david Oct 23, 2025
cddfd3f
refactor: reset PDF printing improvements to develop branch, moving t…
ichim-david Oct 23, 2025
c976d8a
Merge branch 'develop' into contents_crash
avoinea Nov 4, 2025
a51032c
Apply suggestions from code review
ichim-david Nov 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 91 additions & 68 deletions src/customizations/volto/server.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ import {
loadOnServer,
} from '@plone/volto/helpers/AsyncConnect';

// Global handler for unhandled promise rejections during SSR
// This prevents the server from crashing on API errors like 404
process.on('unhandledRejection', (reason, promise) => {
console.error('[SSR] Unhandled Promise Rejection:', {
reason: reason?.message || reason,
status: reason?.status,
});
// Don't crash the server for HTTP errors (401, 404, etc.)
const ignoredStatuses = [301, 302, 401, 404];
if (reason?.status && !ignoredStatuses.includes(reason.status)) {
console.error('[SSR] Full error:', reason);
}
});

let locales = {};
const isCSP = process.env.CSP_HEADER || config.settings.serverConfig.csp;

Expand Down Expand Up @@ -251,67 +265,72 @@ server.get('/*', (req, res) => {
const url = req.originalUrl || req.url;
const location = parseUrl(url);

loadOnServer({ store, location, routes, api })
.then(() => {
const initialLang =
req.universalCookies.get('I18N_LANGUAGE') ||
config.settings.defaultLanguage ||
req.headers['accept-language'];

// The content info is in the store at this point thanks to the asynconnect
// features, then we can force the current language info into the store when
// coming from an SSR request

// TODO: there is a bug here with content that, for any reason, doesn't
// present the language token field, for some reason. In this case, we
// should follow the cookie rather then switching the language
const contentLang = store.getState().content.get?.error
? initialLang
: store.getState().content.data?.language?.token ||
config.settings.defaultLanguage;

if (toBackendLang(initialLang) !== contentLang && url !== '/') {
const newLang = toReactIntlLang(
new locale.Locales(contentLang).best(supported).toString(),
// Wrap in try-catch to handle synchronous errors from superagent
try {
loadOnServer({ store, location, routes, api })
.then(() => {
const initialLang =
req.universalCookies.get('I18N_LANGUAGE') ||
config.settings.defaultLanguage ||
req.headers['accept-language'];

// The content info is in the store at this point thanks to the asynconnect
// features, then we can force the current language info into the store when
// coming from an SSR request

// TODO: there is a bug here with content that, for any reason, doesn't
// present the language token field, for some reason. In this case, we
// should follow the cookie rather then switching the language
const contentLang = store.getState().content.get?.error
? initialLang
: store.getState().content.data?.language?.token ||
config.settings.defaultLanguage;

if (toBackendLang(initialLang) !== contentLang && url !== '/') {
const newLang = toReactIntlLang(
new locale.Locales(contentLang).best(supported).toString(),
);
store.dispatch(changeLanguage(newLang, locales[newLang], req));
}

const context = {};
resetServerContext();
const markup = renderToString(
<ChunkExtractorManager extractor={extractor}>
<CookiesProvider cookies={req.universalCookies}>
<Provider store={store} onError={reactIntlErrorHandler}>
<StaticRouter context={context} location={req.url}>
<ReduxAsyncConnect routes={routes} helpers={api} />
</StaticRouter>
</Provider>
</CookiesProvider>
</ChunkExtractorManager>,
);
store.dispatch(changeLanguage(newLang, locales[newLang], req));
}

const context = {};
resetServerContext();
const markup = renderToString(
<ChunkExtractorManager extractor={extractor}>
<CookiesProvider cookies={req.universalCookies}>
<Provider store={store} onError={reactIntlErrorHandler}>
<StaticRouter context={context} location={req.url}>
<ReduxAsyncConnect routes={routes} helpers={api} />
</StaticRouter>
</Provider>
</CookiesProvider>
</ChunkExtractorManager>,
);

const readCriticalCss =
config.settings.serverConfig.readCriticalCss || defaultReadCriticalCss;

// If we are showing an "old browser" warning,
// make sure it doesn't get cached in a shared cache
const browserdetect = store.getState().browserdetect;
if (config.settings.notSupportedBrowsers.includes(browserdetect?.name)) {
res.set({
'Cache-Control': 'private',
});
}

if (context.url) {
res.redirect(flattenToAppURL(context.url));
} else if (context.error_code) {
res.set({
'Cache-Control': 'no-cache',
});

res.status(context.error_code).send(
`<!doctype html>

const readCriticalCss =
config.settings.serverConfig.readCriticalCss ||
defaultReadCriticalCss;

// If we are showing an "old browser" warning,
// make sure it doesn't get cached in a shared cache
const browserdetect = store.getState().browserdetect;
if (
config.settings.notSupportedBrowsers.includes(browserdetect?.name)
) {
res.set({
'Cache-Control': 'private',
});
}

if (context.url) {
res.redirect(flattenToAppURL(context.url));
} else if (context.error_code) {
res.set({
'Cache-Control': 'no-cache',
});

res.status(context.error_code).send(
`<!doctype html>
${renderToString(
<Html
extractor={extractor}
Expand All @@ -330,10 +349,10 @@ server.get('/*', (req, res) => {
/>,
)}
`,
);
} else {
res.status(200).send(
`<!doctype html>
);
} else {
res.status(200).send(
`<!doctype html>
${renderToString(
<Html
extractor={extractor}
Expand All @@ -348,10 +367,14 @@ server.get('/*', (req, res) => {
/>,
)}
`,
);
}
}, errorHandler)
.catch(errorHandler);
);
}
}, errorHandler)
.catch(errorHandler);
} catch (error) {
// Handle synchronous errors from superagent (e.g., 404 during SSR)
errorHandler(error);
}
});

export const defaultReadCriticalCss = () => {
Expand Down