feat: runtime env
Support runtime environment variables using server components. This will mean docker images can change env vars for runtime as required.
This commit is contained in:
29
packages/lib/universal/runtime-env/server.tsx
Normal file
29
packages/lib/universal/runtime-env/server.tsx
Normal file
@@ -0,0 +1,29 @@
|
||||
'use server';
|
||||
|
||||
import React from 'react';
|
||||
|
||||
import { RuntimeEnvClientProvider } from './client';
|
||||
import { PublicEnv } from './types';
|
||||
|
||||
export type RuntimeEnvProviderProps = {
|
||||
children: React.ReactNode;
|
||||
};
|
||||
|
||||
export const RuntimeEnvProvider = ({ children }: RuntimeEnvProviderProps) => {
|
||||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
||||
const publicEnv = Object.entries(process.env)
|
||||
.filter(([key]) => key.startsWith('NEXT_PUBLIC_'))
|
||||
.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {}) as PublicEnv;
|
||||
|
||||
return (
|
||||
<RuntimeEnvClientProvider value={publicEnv}>
|
||||
{children}
|
||||
|
||||
<script
|
||||
dangerouslySetInnerHTML={{
|
||||
__html: `window.__unstable_runtimeEnv = ${JSON.stringify(publicEnv)}`,
|
||||
}}
|
||||
/>
|
||||
</RuntimeEnvClientProvider>
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user