Skip to content

Instantly share code, notes, and snippets.

@martinratinaud
Last active September 12, 2023 06:18
Show Gist options
  • Save martinratinaud/4e36b84e621037d836e6f11868e87a98 to your computer and use it in GitHub Desktop.
Save martinratinaud/4e36b84e621037d836e6f11868e87a98 to your computer and use it in GitHub Desktop.
usePublicRuntimeConfig hook for NextJs
declare module 'next/config' {
type ConfigTypes = () => {
publicRuntimeConfig: {
staticFolder: string;
version: string;
};
serverRuntimeConfig: {
mySecret: string;
};
};
declare const getConfig: ConfigTypes;
export default getConfig;
}
const { version } = require('./package.json');
const commit = process.env.VERCEL_GIT_COMMIT_SHA ? process.env.VERCEL_GIT_COMMIT_SHA.substring(0, 6) : undefined;
/** @type {import('next').NextConfig} */
const nextConfig = {
publicRuntimeConfig: {
staticFolder: process.env.STATIC_FOLDER,
version: commit || version,
},
serverRuntimeConfig: {
mySecret: process.env.SECRET;
};
};
module.exports = nextConfig;
import getConfig from 'next/config';
const config = getConfig();
const usePublicRuntimeConfig = () => config.publicRuntimeConfig;
export default usePublicRuntimeConfig;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment