Skip to content

Commit

Permalink
fix: env doesn't include ULTRA_MODE (#270)
Browse files Browse the repository at this point in the history
* fix: ultra mode is public

* fix: missing key prop

* wip
  • Loading branch information
deckchairlabs authored Jul 24, 2023
1 parent c0d83e1 commit b2f212a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/context/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import EnvContext from "../../hooks/env-context.js";
import useServerInsertedHTML from "../../hooks/use-server-inserted-html.js";

export function EnvProvider({ children }: { children: JSX.Element }) {
const env = Object.entries(Deno.env.toObject()).filter(([key]) =>
key.startsWith("ULTRA_PUBLIC_")
const publicEnv = Object.entries(Deno.env.toObject()).filter(([key]) =>
key.startsWith("ULTRA_PUBLIC_") || key === "ULTRA_MODE"
);

const value = new Map<string, string>(env);
const value = new Map<string, string>(publicEnv);

useServerInsertedHTML(() => {
const entries = Array.from(value.entries());
Expand Down
4 changes: 3 additions & 1 deletion lib/context/serverInsertedHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export const getServerInsertedHTML = (): Promise<string> => {
h(
Fragment,
null,
Array.from(serverInsertedHTMLCallbacks).map((callback) => callback()),
Array.from(serverInsertedHTMLCallbacks).map((callback, index) =>
h(Fragment, { key: `server-insert-cb-${index}` }, callback())
),
),
));
};
46 changes: 46 additions & 0 deletions test/unit/use-env.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import useEnv from "../../hooks/use-env.js";
import { renderToStream } from "../../lib/render.ts";

Deno.test("useEnv hook", async () => {
let value;

Deno.env.set("ULTRA_MODE", "foo");

const App = () => {
value = useEnv("ULTRA_MODE");
return (
<html>
<head>
<title>useEnv</title>
</head>
<body>
<div>{value}</div>
</body>
</html>
);
};

const stream = await renderToStream(
<App />,
undefined,
{
baseUrl: "/",
importMap: { imports: {} },
assetManifest: new Map(),
},
);

const response = new Response(stream);
const text = await response.text();

assertEquals(
text.includes("foo"),
true,
);

assertEquals(
text.includes("globalThis.__ULTRA_ENV"),
true,
);
});

0 comments on commit b2f212a

Please sign in to comment.