Skip to content

Commit 6ec4177

Browse files
docs: add page for environment variables
1 parent ebd7b22 commit 6ec4177

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
description: |
3+
Error pages can be used to customize the page that is shown when an error occurs in the application.
4+
---
5+
6+
Envrionment variables in Deno are typically read via `Deno.env.get()` or
7+
`process.env.*` calls or via an `.env` file if the `--env-file` flag is used,
8+
see
9+
[how to use Environment Variables in Deno](https://docs.deno.com/runtime/reference/env_variables/).
10+
11+
On top of that Fresh automatically inlines all environment variables whose names
12+
start with `FRESH_PUBLIC_` during bundling of islands.
13+
14+
> [info]: This inlining step occurs when building the app (`deno task build`).
15+
> Environment variables inside islands cannot be read at runtime.
16+
17+
Example:
18+
19+
```sh Terminal
20+
$ FRESH_PUBLIC_FOO=bar deno task dev
21+
```
22+
23+
```tsx
24+
export function MyIsland() {
25+
const value = Deno.env.get("FRESH_PUBLIC_FOO");
26+
return <h1>{value}</h1>;
27+
}
28+
```
29+
30+
This code when bundled will be turned into this:
31+
32+
```tsx
33+
export function MyIsland() {
34+
const value = "bar";
35+
return <h1>{value}</h1>;
36+
}
37+
```
38+
39+
This way you can use specific environment variables in the browser.
40+
41+
> [warn]: To make inlining work the code needs to be analyzable by our plugins.
42+
> This means that not all forms of reading an environment variable in Deno are
43+
> supported, even if it's perfectly valid JavaScript code.
44+
>
45+
> ```ts MyIsland.tsx
46+
> // CORRECT
47+
> Deno.env.get("FRESH_PUBLIC_FOO");
48+
> Deno.env.get("FRESH_PUBLIC_FOO");
49+
> process.env.FRESH_PUBLIC_FOO;
50+
>
51+
> // WRONG
52+
> const name = "FRESH_PUBLIC_FOO";
53+
> Deno.env.get(name);
54+
> process.env[name];
55+
>
56+
> // WRONG
57+
> const obj = Deno.env.toObject();
58+
> obj.FRESH_PUBLIC_FOO;
59+
> ```

docs/toc.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const toc: RawTableOfContents = {
5353
["partials", "Partials", "link:canary"],
5454
["forms", "Forms", "link:canary"],
5555
["define", "Define Helpers", "link:canary"],
56+
["environment-variables", "Environment Variables", "link:canary"],
5657
],
5758
},
5859
deployment: {

0 commit comments

Comments
 (0)