Skip to content
This repository was archived by the owner on Jul 10, 2025. It is now read-only.

Commit 7627d71

Browse files
authored
Merge pull request #11 from technosophos/feat/cache-control
Added CACHE_CONTROL env vars to set cache control headers
2 parents 39eebc0 + a639c0e commit 7627d71

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ volumes = {"/" = "/path/to/fileserver"}
9090

9191
The above configures Wagi to map the path `/static/...` to the `fileserver.gr.wasm` module. Then it serves all of the files in this project.
9292

93+
### Environment Variables
94+
95+
The following environment variables can be passed via Wagi's `-e` flag:
96+
97+
- `CACHE_CONTROL`: The string value of a cache-control header. If not specified, this will set cache-control to `no-cache`. Google recommends setting this value to `CACHE_CONTROL="max-age=31536000"` (cache for up to 1 year).
98+
- Type-specific cache controls can be set using the following env vars (all of which default to `CACHE_CONTROL` if not set):
99+
- `CSS_CACHE_CONTROL`
100+
- `FONT_CACHE_CONTROL`
101+
- `IMAGE_CACHE_CONTROL`
102+
- `JS_CACHE_CONTROL`
103+
104+
93105
### Testing the Static Fileserver with `curl`
94106

95107
This step is the same whether you use Bindle or a `modules.toml`.

fileserver.gr

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,43 @@ let rec pipe = (in, out) => {
4545
}
4646
}
4747

48+
// Determine cache control values.
49+
let cache_duration = (env, mtype) => {
50+
let default_cache_control = match (Map.get("CACHE_CONTROL", env)) {
51+
Some(val) => val,
52+
None => "no-cache",
53+
}
54+
55+
if (String.indexOf("image/", mtype) == Some(0)) {
56+
match (Map.get("IMAGE_CACHE_CONTROL", env)) {
57+
Some(val) => val,
58+
None => default_cache_control,
59+
}
60+
} else if (String.indexOf("font/", mtype) == Some(0)) {
61+
match (Map.get("FONT_CACHE_CONTROL", env)) {
62+
Some(val) => val,
63+
None => default_cache_control,
64+
}
65+
} else if (String.indexOf("text/css", mtype) == Some(0)) {
66+
match (Map.get("CSS_CACHE_CONTROL", env)) {
67+
Some(val) => val,
68+
None => default_cache_control,
69+
}
70+
} else if (String.indexOf("text/javascript", mtype) == Some(0)) {
71+
match (Map.get("JS_CACHE_CONTROL", env)) {
72+
Some(val) => val,
73+
None => default_cache_control,
74+
}
75+
} else {
76+
default_cache_control
77+
}
78+
}
79+
80+
let headers = (env, path) => {
81+
let mtype = Mediatype.guess(path);
82+
"Content-Type: " ++ mtype ++ "\nCache-Control: " ++ cache_duration(env, mtype) ++ "\n\n"
83+
}
84+
4885
let serve = (abs_path, env) => {
4986
// If PATH_PREFIX is set, then the path prefix is prepended onto the incoming path.
5087
// This allows you to map to a directory that does not match the directory name in the URL.
@@ -70,7 +107,7 @@ let serve = (abs_path, env) => {
70107
"Unexpected error when writing Content-Type",
71108
File.fdWrite(
72109
File.stdout,
73-
"Content-Type: " ++ Mediatype.guess(path) ++ "\n\n",
110+
headers(env, path),
74111
),
75112
)
76113

@@ -84,6 +121,8 @@ let serve = (abs_path, env) => {
84121
}
85122
}
86123

124+
125+
87126
let guestpath = env => {
88127
// Backward compat for an older version of Wagi that had PATH_INFO wrong.
89128
// X_RELATIVE_PATH was removed before Wagi 0.4

0 commit comments

Comments
 (0)