On this page
article
Nextjs
Client components can be imported into server components, not the other way around!
AppConfig
type AppConfigItem = {
[key: string]: string;
}
function checkMissingEnvs(requiredEnv: AppConfigItem) {
let missingEnvs: string[] = []
for (const key in requiredEnv) {
const value = requiredEnv[key]
if (value === '') {
missingEnvs.push(key)
}
}
if (missingEnvs.length > 0) {
throw new Error(`Missing required envs: ${missingEnvs}`)
}
}
function ServerAppConfig() {
const requiredEnv: AppConfigItem = {}
const optionalEnv: AppConfigItem = {}
checkMissingEnvs(requiredEnv)
return {
...requiredEnv,
...optionalEnv,
};
}
function ClientAppConfig() {
const requiredEnv: AppConfigItem = {
NEXT_PUBLIC_VAR: process.env.NEXT_PUBLIC_VAR || '',
}
const optionalEnv: AppConfigItem = {}
checkMissingEnvs(requiredEnv)
return {
...requiredEnv,
...optionalEnv,
};
}
export {ServerAppConfig, ClientAppConfig}
## AppConfig
Static Exporting
Common error I’ll get when doing static builds with nextjs
Page "/blog/[blog]" is missing "generateStaticParams()" so it cannot be used with "output: export" config.
Make absolutely sure the directory structure (say /app/blog/[blog]/page.tsx) lines up with the getStaticParams. In this case, don’t use id but use blog because of the [blog]. If it was [id] you would use id