Skip to content

Commit 292bd85

Browse files
authored
Purge all domains on Fastly (#6835)
When invalidating the cache on Fastly using the "single URL" endpoint, the content is only purged for that specific URL. This can cause inconsistencies, since we have two domains associated with each Fastly service (namely `static.*` and `fastly-static.*`). The invalidation logix has been refactor to purge a path for both domains.
1 parent 6a18663 commit 292bd85

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/worker/fastly.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ impl Fastly {
2323
/// Invalidate a path on Fastly
2424
///
2525
/// This method takes a path and invalidates the cached content on Fastly. The path must not
26-
/// contain a wildcard, since the Fastly API does not support wildcard invalidations.
26+
/// contain a wildcard, since the Fastly API does not support wildcard invalidations. Paths are
27+
/// invalidated for both domains that are associated with the Fastly service.
2728
///
2829
/// Requests are authenticated using a token that is sent in a header. The token is passed to
2930
/// the application as an environment variable.
@@ -38,11 +39,21 @@ impl Fastly {
3839
));
3940
}
4041

42+
let domains = [
43+
&self.static_domain_name,
44+
&format!("fastly-{}", self.static_domain_name),
45+
];
4146
let path = path.trim_start_matches('/');
42-
let url = format!(
43-
"https://api.fastly.com/purge/{}/{}",
44-
self.static_domain_name, path
45-
);
47+
48+
for domain in domains.iter() {
49+
let url = format!("https://api.fastly.com/purge/{}/{}", domain, path);
50+
self.purge_url(client, &url)?;
51+
}
52+
53+
Ok(())
54+
}
55+
56+
fn purge_url(&self, client: &Client, url: &str) -> anyhow::Result<()> {
4657
trace!(?url);
4758

4859
let api_token = self.api_token.expose_secret();
@@ -54,7 +65,7 @@ impl Fastly {
5465

5566
debug!("sending invalidation request to Fastly");
5667
let response = client
57-
.post(&url)
68+
.post(url)
5869
.headers(headers)
5970
.send()
6071
.context("failed to send invalidation request to Fastly")?;
@@ -76,7 +87,7 @@ impl Fastly {
7687
"invalidation request to Fastly failed"
7788
);
7889

79-
Err(error).with_context(|| format!("failed to invalidate {path} on Fastly"))
90+
Err(error).with_context(|| format!("failed to purge {url}"))
8091
}
8192
}
8293
}

0 commit comments

Comments
 (0)