Skip to content

Commit a3292c0

Browse files
committed
Replace noindex tag with X-Robots-Tag header
Expired files could get indexed by search engines because the 404 page doesn't have the noindex meta tag. This puts it on the entire route so even the 404 page tells Google to not index it.
1 parent 76039f3 commit a3292c0

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

bobashare-web/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ thiserror = "2.0.0"
3737
tokio = { version = "1.23.1", features = ["full"] }
3838
tokio-util = { version = "0.7.4", features = ["io"] }
3939
tower = "0.5.0"
40-
tower-http = { version = "0.6.0", features = ["trace", "request-id", "fs", "util"] }
40+
tower-http = { version = "0.6.0", features = ["trace", "request-id", "fs", "util", "set-header"] }
4141
tracing = "0.1.36"
4242
tracing-subscriber = { version = "0.3.15", features = ["env-filter"] }
4343
url = "2.3.1"

bobashare-web/src/views/mod.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ use axum::{
1010
Router,
1111
};
1212
use chrono::TimeDelta;
13+
use http::header::{HeaderName, HeaderValue};
1314
use hyper::StatusCode;
15+
use tower_http::set_header::SetResponseHeaderLayer;
1416
use tracing::{event, Level};
1517
use url::Url;
1618

@@ -148,10 +150,20 @@ pub(crate) fn render_template<T: askama::Template>(tmpl: T) -> Result<Response,
148150
}
149151

150152
pub fn router() -> Router<&'static AppState> {
153+
let x_robots_tag_no_index = SetResponseHeaderLayer::overriding(
154+
HeaderName::from_static("x-robots-tag"),
155+
HeaderValue::from_static("noindex"),
156+
);
151157
Router::new()
152158
.route("/", get(upload::upload))
153159
.route("/paste/", get(upload::paste))
154160
.route("/about/", get(about::about))
155-
.route("/{id}", get(display::display))
156-
.route("/raw/{id}", get(display::raw))
161+
.route(
162+
"/{id}",
163+
get(display::display).layer(x_robots_tag_no_index.clone()),
164+
)
165+
.route(
166+
"/raw/{id}",
167+
get(display::raw).layer(x_robots_tag_no_index.clone()),
168+
)
157169
}

bobashare-web/templates/display.html.jinja

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
<meta name="twitter:title" content='{{ filename_truncated }} ({{ size|humansize }})'>
1414
{% endblock %}
1515
{% block head %}
16-
<meta name="robots" content="noindex">
17-
1816
{% match contents %}
1917
{% when DisplayType::Image %}
2018
<meta property="og:image" content='{{ raw_url }}'>

0 commit comments

Comments
 (0)