Skip to content

Commit a6be2df

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

2 files changed

Lines changed: 57 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
Example:
15+
16+
```sh Terminal
17+
$ FRESH_PUBLIC_FOO=bar deno task dev
18+
```
19+
20+
```tsx
21+
export function MyIsland() {
22+
const value = Deno.env.get("FRESH_PUBLIC_FOO");
23+
return <h1>{value}</h1>;
24+
}
25+
```
26+
27+
This code when bundled will be turned into:
28+
29+
```tsx
30+
export function MyIsland() {
31+
const value = "bar";
32+
return <h1>{value}</h1>;
33+
}
34+
```
35+
36+
This way you can use specific environment variables in the browser.
37+
38+
> [warn]: To make inlining work the code needs to be analyzable by our plugins.
39+
> This means that not all forms of reading an environment variable in Deno are
40+
> supported, even if it's perfectly valid JavaScript code.
41+
>
42+
> ```ts MyIsland.tsx
43+
> // CORRECT
44+
> Deno.env.get("FRESH_PUBLIC_FOO");
45+
> Deno.env.get("FRESH_PUBLIC_FOO");
46+
> process.env.FRESH_PUBLIC_FOO;
47+
>
48+
> // WRONG
49+
> const name = "FRESH_PUBLIC_FOO";
50+
> Deno.env.get(name);
51+
> process.env[name];
52+
>
53+
> // WRONG
54+
> const obj = Deno.env.toObject();
55+
> obj.FRESH_PUBLIC_FOO;
56+
> ```

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)