Skip to content

Commit

Permalink
Serve favicon as PNG to Safari and SVG others (ordinals#1302)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Jan 20, 2023
1 parent 2184b1c commit bf83ee6
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 23 deletions.
39 changes: 38 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ members = [".", "test-bitcoincore-rpc"]

[dependencies]
anyhow = { version = "1.0.56", features = ["backtrace"] }
axum = "0.6.1"
axum = { version = "0.6.1", features = ["headers"] }
axum-server = "0.4.0"
bech32 = "0.9.1"
bip39 = "1.0.1"
Expand Down
45 changes: 28 additions & 17 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ use {
axum::{
body,
extract::{Extension, Path, Query},
headers::UserAgent,
http::{header, HeaderMap, HeaderValue, StatusCode, Uri},
response::{IntoResponse, Redirect, Response},
routing::get,
Router,
Router, TypedHeader,
},
axum_server::Handle,
rust_embed::RustEmbed,
Expand Down Expand Up @@ -136,8 +137,7 @@ impl Server {
.route("/clock", get(Self::clock))
.route("/content/:inscription_id", get(Self::content))
.route("/faq", get(Self::faq))
.route("/favicon.ico", get(Self::favicon_ico))
.route("/favicon.svg", get(Self::favicon_svg))
.route("/favicon.ico", get(Self::favicon))
.route("/feed.xml", get(Self::feed))
.route("/input/:block/:transaction/:input", get(Self::input))
.route("/inscription/:inscription_id", get(Self::inscription))
Expand Down Expand Up @@ -555,21 +555,32 @@ impl Server {
}
}

async fn favicon_ico() -> ServerResult<Response> {
Self::static_asset(Path("/favicon.png".to_string())).await
}

async fn favicon_svg() -> ServerResult<Response> {
Ok(
(
[(
header::CONTENT_SECURITY_POLICY,
HeaderValue::from_static("default-src 'unsafe-inline'"),
)],
Self::static_asset(Path("/favicon.svg".to_string())).await?,
async fn favicon(user_agent: Option<TypedHeader<UserAgent>>) -> ServerResult<Response> {
if user_agent
.map(|user_agent| {
user_agent.as_str().contains("Safari/")
&& !user_agent.as_str().contains("Chrome/")
&& !user_agent.as_str().contains("Chromium/")
})
.unwrap_or_default()
{
Ok(
Self::static_asset(Path("/favicon.png".to_string()))
.await
.into_response(),
)
.into_response(),
)
} else {
Ok(
(
[(
header::CONTENT_SECURITY_POLICY,
HeaderValue::from_static("default-src 'unsafe-inline'"),
)],
Self::static_asset(Path("/favicon.svg".to_string())).await?,
)
.into_response(),
)
}
}

async fn feed(
Expand Down
2 changes: 0 additions & 2 deletions src/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ mod tests {
<meta name=viewport content='width=device-width,initial-scale=1.0'>
<title>Foo</title>
<link rel=alternate href=/feed.xml type=application/rss\+xml title='Inscription RSS Feed'>
<link rel=icon type=image/svg\+xml href=/favicon.svg>
<link rel=icon type=image/png href=/favicon.ico>
<link rel=stylesheet href=/static/index.css>
<link rel=stylesheet href=/static/modern-normalize.css>
<script src=/static/index.js defer></script>
Expand Down
2 changes: 0 additions & 2 deletions templates/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
%% }
<title>{{ self.content.title() }}</title>
<link rel=alternate href=/feed.xml type=application/rss+xml title='Inscription RSS Feed'>
<link rel=icon type=image/svg+xml href=/favicon.svg>
<link rel=icon type=image/png href=/favicon.ico>
<link rel=stylesheet href=/static/index.css>
<link rel=stylesheet href=/static/modern-normalize.css>
<script src=/static/index.js defer></script>
Expand Down

0 comments on commit bf83ee6

Please sign in to comment.