krabs icon indicating copy to clipboard operation
krabs copied to clipboard

'Common pages' functionality

Open TheLeePriest opened this issue 2 years ago • 3 comments

Hi there!

I am currently working on a project that uses Next.js and I want to make use of Krabs for a multi-tenant approach. One thing I wanted to do was to have what I call 'common pages' between all the tenants. For example creating a resources page in the default Next.js pages directory would then allow that page to be used by all tenants.

I have a fork of the repo and believe that I have been able to achieve this by passing an extra options parameter in to the default krabs() function call. The options parameter is an object that currently contains a commonPages boolean, this could be extended going forward. The update to the utils/routes/resolve.ts file is as follows:

const fs = require('fs')
const path = require('path')

export function resolveRoutes(tenantName: string, pathname: string, commonPages: boolean | undefined): string {
  const pageExistsInTenant = fs.existsSync(path.join(process.cwd(), 'pages', tenantName, `${pathname}.js`));

  if(commonPages && !pageExistsInTenant && pathname !== '/') {
    return `${pathname}`
  }

  return pathname === '/' ? `/${tenantName}` : `/${tenantName}${pathname}`;
}

export default resolveRoutes;

As you can see the modification to the function is minor but offers that little bit more flexibility.

I would love to hear any and all of your feedback on what you think of potentially adding this functionality to Krabs.

TheLeePriest avatar Nov 08 '21 18:11 TheLeePriest

If you use https://github.com/ds300/patch-package you can just use this patch I created https://gist.github.com/cj/a9c967c517c37aad09e41aa2e9779c25

cj avatar Nov 13 '21 01:11 cj

Hi @cj, thank you again for showing how to solve this problem! Would you mind opening a PR for this? 🙂

micheleriva avatar Nov 13 '21 17:11 micheleriva

Is this thread active?

mihir-karbelkar-wiz avatar May 09 '23 11:05 mihir-karbelkar-wiz