Files
sign/packages/lib/universal/runtime-env/server.tsx
Mythie 1cd60e1abb feat: runtime env
Support runtime environment variables using server components.

This will mean docker images can change env vars for runtime as required.
2023-11-12 13:10:30 +11:00

30 lines
815 B
TypeScript

'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>
);
};