From e7485f0dac135ed8d6f321209f215eadd2ca1b32 Mon Sep 17 00:00:00 2001 From: Mauro Amico Date: Mon, 28 Aug 2023 15:19:30 +0200 Subject: [PATCH] fix: backport blacklistRoutes (#305) --- .../volto/middleware/blacklistRoutes.js | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/customizations/volto/middleware/blacklistRoutes.js diff --git a/src/customizations/volto/middleware/blacklistRoutes.js b/src/customizations/volto/middleware/blacklistRoutes.js new file mode 100644 index 000000000..a4e1b8137 --- /dev/null +++ b/src/customizations/volto/middleware/blacklistRoutes.js @@ -0,0 +1,48 @@ +/** + * backport https://github.com/plone/volto/pull/4854 + */ + +import config from '@plone/volto/registry'; +import { matchPath } from 'react-router'; + +const blacklistRoutes = ({ dispatch, getState }) => (next) => (action) => { + if (typeof action === 'function') { + return next(action); + } + + switch (action.type) { + case '@@router/LOCATION_CHANGE': + debugger; + let { pathname } = action.payload.location; + const { externalRoutes = [] } = config.settings; + + const route = externalRoutes.find((route) => + matchPath(pathname, route.match), + ); + + let actionToSend = action; + if (pathname.startsWith('/++api++')) { + actionToSend.payload.location.pathname = actionToSend.payload.location.pathname.substring( + 8, + ); + // To handle the `window.location.replace` + pathname = actionToSend.payload.location.pathname; + if (window.history) { + window.history.replaceState(window.history.state, '', pathname); + } + } + + if (!route) { + return next(actionToSend); + } else { + window.location.replace( + route.url ? route.url(actionToSend.payload) : pathname, + ); + } + break; + default: + return next(action); + } +}; + +export default blacklistRoutes; \ No newline at end of file