1.0.1 • Published 5 years ago

@moxy/next-runtime-env v1.0.1

Weekly downloads
15
License
MIT
Repository
github
Last release
5 years ago

next-runtime-env

NPM version Downloads Build Status Coverage Status Dependency status Dev Dependency status

NextJS plugin to pass environment variables to NextJS's runtime configuration.

By default, NextJS does not make available process.env to the client side. The available solution for this problem is to set a runtime config in next.config.js to pass values which will be available in either server and client or just server.

The way to do it is by passing values to the publicRuntimeConfig and serverRuntimeConfig properties of the NextJS' config.

This plugin does that automatically for all environment variables starting with a certain prefix.

Installation

$ npm i --save-dev @moxy/next-runtime-env

Usage

// next.config.js
const withRuntimeEnv = require('@moxy/next-runtime-env');

module.exports = withRuntimeEnv({ ...options })({ ...nextConfig });

Multiple configurations can be combined together with function composition. For example:

// next.config.js
const withCSS = require('@zeit/next-css');
const withRuntimeEnv = require('@moxy/next-runtime-env');

module.exports = withCSS(
    withRuntimeEnv({
        removePrefixes: true,
    })({
        cssModules: true, // this options will be passed to withCSS plugin through nextConfig
    }),
);

API

OptionDescriptionTypeDefault
publicPrefixPrefix of variables to lookup and then pass to publicRuntimeConfigStringPUBLIC_
serverPrefixPrefix of variables to lookup and then pass to serverRuntimeConfigStringSERVER_
removePrefixesOption to remove prefix when passing variables to runtime configBooleanfalse

Tests

Any parameter passed to the test command, is passed down to Jest.

$ npm t
$ npm t -- --coverage # to generate coverage report
$ npm t -- --watch # during development

License

Released under the MIT License.