fridays / next-routes

Universal dynamic routes for Next.js
MIT License
2.47k stars 230 forks source link

Allow routes.add to pass additional properties to Route #294

Closed elisechant closed 1 year ago

elisechant commented 5 years ago

I want to be able to add additional parameters to the route configuration objects so that I can access the route scope at Page level. I want to do this so that route configuration can encapsulate things such as title handling, redirect conditions and various others.

Where I can now supply (only):

Routes.add({ name: 'home', pattern: '/', page: 'home });

I want to be able to supply additional parameters, such as title and myFn:

Routes.add({ name: 'home', pattern: '/', page: 'home, title: 'Home Page' , myFn: () => {}});

I could then access the scoped route at getInitialProps Something like:

import Routes from 'server/routes';

static async getInitialProps({Component, ctx}) {
    let pageProps = {};

    const route = Routes.routes.find(route => {
        return route.pattern === ctx.pathname;
    });

        // use route

The current Route signature looks like this:


[ Route {
    name: 'home',
    pattern: '/',
    page: '/',
    keys: [],
    regex: /^\/(?:\/)?$/i,
    keyNames: [],
    toPath: [Function] },

Perhaps it could be achieved with a HoC or by extending the library.

If you think that this would be beneficial, I would be happy to create the PR