npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@begrowth/custom-krabs

v0.1.10

Published

Multitenant middleware for Next.js

Downloads

6

Readme

Build Status e2e tests codecov NPM Downloads npm Commitizen friendly

Krabs is an enterprise-ready Express.js/Fastify middleware for serving thousands of different websites from a single Next.js instance.

Installation

Krabs is available on npm and can be installed as follows:

For Express.js (see on npm)

yarn add @begrowth/custom-krabs

# or

npm install --save @begrowth/custom-krabs

For Fastify (see on npm)

yarn add fastify-krabs

# or

npm insall --save fastify-krabs

Things to know

  • Krabs forces you to use a custom server. Therefore, deployments to Vercel are not supported.
  • _app and _document pages are common to every website.

Getting Started

You can watch a video introduction on YouTube:

Examples

Let's say that we want to support two different websites with just one Next.js instance, and serve them using just one Express.js server. Write the following configuration inside a .krabs.js or .krabs.config.js file inside of the root of your project:

module.exports = {
  tenants: [
    {
      name: 'website-1',
      domains: [
        {
          development: /dev\.[a-z]*\.local\.website-1\.com/, // Regex supported!
          staging: 'stage.website-1.com',
          production: 'website-1.com',
        },
      ],
    },
    {
      name: 'website-2',
      domains: [
        {
          development: 'local.website-2.com',
          staging: 'stage.website-2.com',
          production: /[\w|\d|-|_]+\.website-2.com/, // Regex supported!
        },
      ],
    },
  ],
};

Create an index.js file and fill it with the following content:

const express = require('express');
const next = require('next');
const krabs = require('krabs').default;
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });

async function main() {
  try {
    await app.prepare();

    const handle = app.getRequestHandler();
    const server = express();

    server
      .get('*', (req, res) => krabs(req, res, handle, app))
      .listen(3000, () => console.log('server ready'));
  } catch (err) {
    console.log(err.stack);
  }
}

main();

Inside our .krabs.js file, we configured two tenants with two different name properties: website-1 and website-2. So now let's create two new folders inside of the Next.js' default pages/ directory:

pages/
  - _app.js
  - website-1
  - website-2

Feel free to add any page you want inside both of these folders, as they will be treated as they were the default Next.js' pages/ folder. Let's add the following content to pages/website-1/about.js:

function About() {
  return <div> About website 1 </div>;
}

export default About;

and the following code to pages/website-2/about.js:

function About() {
  return <div> This is website 2 </div>;
}

export default About;

Map local.website-1.com and local.website-2.com in your hosts file, then boot the server by typing:

node index.js

going to http://dev.pizza.local.website-1.com/about and http://local.website-2.com/about, you will see the components above rendered by the same Next.js instance!

Let's say that we want to support two different websites with just one Next.js instance, and serve them using just one Express.js server. Write the following configuration inside a .krabs.js or .krabs.config.js file inside of the root of your project:

module.exports = {
  tenants: [
    {
      name: 'website-1',
      domains: [
        {
          development: /dev\.[a-z]*\.local\.website-1\.com/, // Regex supported!
          staging: 'stage.website-1.com',
          production: 'website-1.com',
        },
      ],
    },
    {
      name: 'website-2',
      domains: [
        {
          development: 'local.website-2.com',
          staging: 'stage.website-2.com',
          production: /[\w|\d|-|_]+\.website-2.com/, // Regex supported!
        },
      ],
    },
  ],
};

Create an index.js file and fill it with the following content:

const fastify = require('fastify')({ trustProxy: true });
const next = require('next');
const krabs = require('../dist/fastify-krabs').default;

const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });

async function main() {
  try {
    await app.prepare();
    const handle = app.getRequestHandler();

    fastify
      .get('*', (request, reply) => krabs(request, reply, handle, app))
      .listen(3000, () => console.log('server ready'));
  } catch (err) {
    console.log(err.stack);
  }
}

main();

Inside our .krabs.js file, we configured two tenants with two different name properties: website-1 and website-2. So now let's create two new folders inside of the Next.js' default pages/ directory:

pages/
  - _app.js
  - website-1
  - website-2

Feel free to add any page you want inside both of these folders, as they will be treated as they were the default Next.js' pages/ folder. Let's add the following content to pages/website-1/about.js:

function About() {
  return <div> About website 1 </div>;
}

export default About;

and the following code to pages/website-2/about.js:

function About() {
  return <div> This is website 2 </div>;
}

export default About;

Map local.website-1.com and local.website-2.com in your hosts file, then boot the server by typing:

node index.js

going to http://dev.pizza.local.website-1.com/about and http://local.website-2.com/about, you will see the components above rendered by the same Next.js instance!

Documentation

You can find the full documentation (with real code examples) here!

License

Krabs is free as in freedom and licensed under the MIT license.