Last active
September 12, 2023 06:18
-
-
Save martinratinaud/4e36b84e621037d836e6f11868e87a98 to your computer and use it in GitHub Desktop.
usePublicRuntimeConfig hook for NextJs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare module 'next/config' { | |
type ConfigTypes = () => { | |
publicRuntimeConfig: { | |
staticFolder: string; | |
version: string; | |
}; | |
serverRuntimeConfig: { | |
mySecret: string; | |
}; | |
}; | |
declare const getConfig: ConfigTypes; | |
export default getConfig; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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