4.8.2 • Published 5 months ago

nextjs-server-modules v4.8.2

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

NextJS Server Modules

A NextJS Server Module is a NextJS project that has been packaged to be easily used as an npm module. You might use a server module to...

  • Package a "fake" or "mock" server for easy use in test suites
  • Allow another server to easily "call into" your NextJS server
  • Improve the organization of a large project by organizing NextJS servers into server sub-components

The nextjs-server-module cli will help you create a custom nextjs runtime to run your projects with, so they can be run without nextjs at runtime.

Usage

First, install with npm install nextjs-server-modules

You can now add a build script or just run nsm build

The build process will output a .nsm/index.ts file which can be used to create your server or invoke requests against it

import myNextJSModule from "./.nsm"

const server = await myNextJSModule({ port: 3030 })

// your server is running on localhost:3030!

server.close()

Middlewares

You can provide middlewares to your server like so:

import myNextJSModule from "./.nsm"

const myMiddleware = (next) => (req, res) => {
  req.token = req.headers.get("authorization").split("Bearer ")?.[1]
  return next(req, res)
}

const server = await myNextJSModule({
  port: 3030,
  middlewares: [myMiddleware],
})

// your server is running on localhost:3030!

server.close()

Common Scripts

Many times you'll want to add nsm when you start testing API endpoints on a NextJS project, it makes a lot of sense to use nextless and skip-build which runs a very fast build.

{
  "scripts": {
    "build:test": "nsm build --nextless --skip-build --only-api-files"
  }
}

Internal: How it Works

Not all NextJS features are currently supported. Particularly:

  • SSR
  • getStaticProps

We build the api endpoints and export static pages with next build. We then analyze .next/server/pages/api to get all the api routes to server.

We then construct a main export file that knows how to parse next.config.js and route to the correct files, which we've statically analyzed and are included in the generated dist/index.js file.

Caveats

Vercel's Edge Runtime is supported only when running in a Node.js environment. In other words, a bundled nsm project cannot be run in the Edge Runtime.

nsm's implementation of the Edge Runtime does not support:

FAQ

Why can't Next.js bundle into an npm module?

You can bundle nextjs into npm modules, but some static analysis isn't available since nextjs uses the directory structure to determine what to load in at runtime.

You'll also have problems running a next server within a Vercel endpoint, because some vercel optimizations get rid of some webpack/next modules as an optimization.

Why is my module bigger?

NSM base64 encodes static files so that they're bundleable, it's very inefficient.

4.8.1

5 months ago

4.8.0

6 months ago

4.8.2

5 months ago

4.7.0

8 months ago

4.4.0

9 months ago

4.6.1

8 months ago

4.6.0

8 months ago

4.2.1

11 months ago

4.2.0

11 months ago

4.5.0

9 months ago

4.3.0

9 months ago

2.4.0

1 year ago

3.0.2

1 year ago

3.0.1

1 year ago

3.0.0

1 year ago

4.0.0

1 year ago

2.3.0

1 year ago

2.5.0

1 year ago

2.3.2

1 year ago

2.3.1

1 year ago

2.5.1

1 year ago

4.1.0

1 year ago

4.1.1

1 year ago

2.2.0

1 year ago

2.1.1

1 year ago

2.0.2

1 year ago

2.1.0

1 year ago

2.0.1

2 years ago

2.0.0

2 years ago

1.7.0

2 years ago

1.6.1

2 years ago

1.6.0

2 years ago

1.5.16

2 years ago

1.5.15

2 years ago

1.5.18

2 years ago

1.5.17

2 years ago

1.5.18-seve.2

2 years ago

1.5.18-seve.1

2 years ago

1.5.17-seve.0

2 years ago

1.5.18-seve.0

2 years ago

1.5.14

2 years ago

1.5.13

2 years ago

1.5.12

2 years ago

1.5.11

2 years ago

1.5.10

2 years ago

1.5.9

2 years ago

1.5.8

2 years ago

1.5.7

2 years ago

1.5.6

2 years ago

1.5.5

2 years ago

1.5.4

2 years ago

1.5.3

2 years ago

1.5.2

2 years ago

1.5.1

2 years ago

1.5.0

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.3.2

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago