Skip to content

Commit

Permalink
fix: backport blacklistRoutes (#305)
Browse files Browse the repository at this point in the history
  • Loading branch information
mamico authored Aug 28, 2023
1 parent 1e47842 commit e7485f0
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/customizations/volto/middleware/blacklistRoutes.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit e7485f0

Please sign in to comment.