Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: perform server-side rendering only when user-agent is a bot #1744

Merged
merged 16 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ indicatif = "0.17.1"
snafu = "0.8.0"
validator = { version = "0.16", features = ["derive"] }
zxcvbn = "2"
isbot = "0.1.3"

[workspace.dependencies.uuid]
version = "1.6.1"
Expand Down
1 change: 1 addition & 0 deletions fastn-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ uuid.workspace = true
validator = { workspace = true, optional = true }
zxcvbn = { workspace = true, optional = true }
zip.workspace = true
isbot.workspace = true

[dev-dependencies]
fbt-lib.workspace = true
Expand Down
8 changes: 7 additions & 1 deletion fastn-core/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,17 @@ impl Request {

pub fn content_type(&self) -> Option<mime_guess::Mime> {
self.headers
.get("content-type")
.get(actix_web::http::header::CONTENT_TYPE)
.and_then(|v| v.to_str().ok())
.and_then(|v| v.parse().ok())
}

pub fn user_agent(&self) -> Option<String> {
self.headers
.get(actix_web::http::header::USER_AGENT)
.and_then(|v| v.to_str().map(|v| v.to_string()).ok())
}

harshdoesdev marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(feature = "auth")]
pub fn ud(&self) -> Option<fastn_core::UserData> {
let session_data = match self.cookie(fastn_core::auth::SESSION_COOKIE_NAME) {
Expand Down
21 changes: 17 additions & 4 deletions fastn-core/src/package/package_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,10 @@ pub(crate) async fn read_ftd_2023(
download_assets: bool,
only_js: bool,
) -> fastn_core::Result<FTDResult> {
let is_bot = match config.request.user_agent() {
Some(user_agent) => KNOWN_BOTS.is_bot(&user_agent),
None => true,
};
harshdoesdev marked this conversation as resolved.
Show resolved Hide resolved
let package_name = config.config.package.name.to_string();
let c = &config.config.clone();

Expand Down Expand Up @@ -621,10 +625,14 @@ pub(crate) async fn read_ftd_2023(
format!("{js_ftd_script}\n{js_document_script}").as_str(),
)
} else {
let ssr_body = fastn_js::ssr_with_js_string(
&package_name,
format!("{js_ftd_script}\n{js_document_script}").as_str(),
);
let ssr_body = if !is_bot {
EMPTY_HTML_BODY.to_string()
} else {
fastn_js::ssr_with_js_string(
&package_name,
format!("{js_ftd_script}\n{js_document_script}").as_str(),
)
};
harshdoesdev marked this conversation as resolved.
Show resolved Hide resolved

fastn_core::utils::replace_markers_2023(
js_document_script.as_str(),
Expand Down Expand Up @@ -656,3 +664,8 @@ pub(crate) async fn process_ftd(

Ok(response)
}

static KNOWN_BOTS: once_cell::sync::Lazy<isbot::Bots> =
once_cell::sync::Lazy::new(isbot::Bots::default);

const EMPTY_HTML_BODY: &str = "<body></body><style id=\"styles\"></style>";
2 changes: 1 addition & 1 deletion ftd/src/executor/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn p(s: &str, t: &str, fix: bool, file_location: &std::path::PathBuf) {
let mut executor =
ftd::executor::ExecuteDoc::from_interpreter(doc).unwrap_or_else(|e| panic!("{:?}", e));
for thing in ftd::interpreter::default::get_default_bag().keys() {
executor.bag.remove(thing);
executor.bag.swap_remove(thing);
}
let expected_json = serde_json::to_string_pretty(&executor).unwrap();
if fix {
Expand Down
2 changes: 1 addition & 1 deletion ftd/src/interpreter/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub fn interpret_helper(
fn p(s: &str, t: &str, fix: bool, file_location: &std::path::PathBuf) {
let mut i = interpret_helper("foo", s).unwrap_or_else(|e| panic!("{:?}", e));
for thing in ftd::interpreter::default::get_default_bag().keys() {
i.data.remove(thing);
i.data.swap_remove(thing);
}
let expected_json = serde_json::to_string_pretty(&i).unwrap();
if fix {
Expand Down
2 changes: 1 addition & 1 deletion ftd/src/node/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn p(s: &str, t: &str, fix: bool, file_location: &std::path::PathBuf) {
ftd::executor::ExecuteDoc::from_interpreter(doc).unwrap_or_else(|e| panic!("{:?}", e));
let mut node = ftd::node::NodeData::from_rt(executor);
for thing in ftd::interpreter::default::get_default_bag().keys() {
node.bag.remove(thing);
node.bag.swap_remove(thing);
}
let expected_json = serde_json::to_string_pretty(&node).unwrap();
if fix {
Expand Down
Loading