uconfig
v1.0.2
Published
A Next.js/Nuxt.js-like universal runtime configuration
Downloads
231
Readme
uconfig
A Next.js/Nuxt.js-like universal runtime configuration
Server Side
- Set config
import { initConfig } from 'uconfig';
initConfig({
serverRuntimeConfig: {
// Will only be available on the server side
mySecret: 'secret',
secondSecret: process.env.SECOND_SECRET, // Pass through env variables
},
publicRuntimeConfig: {
// Will be available on both server and client
staticFolder: '/static',
},
});
- Use config
import { getConfig } from 'uconfig';
const config = getConfig();
// Will only be available on the server-side
console.log(config.mySecret)
// Will be available on both server-side and client-side
console.log(config.staticFolder)
- Generate HTML
// Make sure config is already set by initConfig
import Koa from 'koa';
import { serializePublicRuntimeConfig } from 'uconfig';
const app = new Koa();
app.use(async ctx => {
const serializedConfig = serializePublicRuntimeConfig();
ctx.body = `
<html>
<head>
<title>My Page</title>
</head>
<body>
<script>${serializedConfig}</script>
<script src="client.js" />
</body>
</html>
`;
});
Client Side
import { getConfig } from 'uconfig';
const config = getConfig();
// Will be available on client-side
console.log(config.staticFolder);
Q&A
How to change default key for serialized config
The default key is __INIT_UCONFIG__
, you can use webpack/rollup DefinePlugin to replace this value.
LICENSE
MIT