Skip to content

New browser lifecycle API: `modifyRoutes`

Choose a tag to compare

@KyleAMathews KyleAMathews released this 07 Feb 00:39
· 21057 commits to master since this release

@scottyeck and @stevensurgnier have been working on a way to add client-side redirects and remove trailing slashes from routes and towards that aim added a new browser lifecycle API modifyRoutes in #657

Some examples they came up with.

Add redirects:

exports.modifyRoutes = routes => {
    const redirects = [
        {
            path: '/cat',
            onEnter: (nextState, replace) => replace('/dog?utm_campaign=cat')
        }
    ];
    const childRoutesLength = routes.childRoutes.length;
    const childRoutesButLast = routes.childRoutes.slice(0, childRoutesLength - 1);
    const childRoutesLast = routes.childRoutes[childRoutesLength - 1];
    routes.childRoutes = childRoutesButLast.concat(redirects).concat([childRoutesLast]);
    return routes;
};

Remove trailing slashes:

exports.modifyRoutes = routes => {
    routes.childRoutes.forEach(route => route.path = route.path.replace(/\/$/, ''));
    return routes;
};