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

Add timestamp to layouts for cachebusting #254

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

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

3 changes: 3 additions & 0 deletions src/cobalt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,15 @@ pub fn build(config: &Config) -> Result<()> {
.collect();

trace!("Generating other documents");
let timestamp = Value::Str(UTC::now().timestamp().to_string());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used Value::Str since Value::Num only accepts f32 - the timestamp is i64 so casting to f32 trims the number to the point where it's not unique.

liquid-rust/#88 will require us to have integer values. So in the future we can change this.

for mut doc in documents {
trace!("Generating {}", doc.path);

if config.dump.contains(&Dump::Liquid) {
create_liquid_dump(dest, &doc.path, &doc.content, &doc.attributes)?;
}
doc.attributes
.insert("timestamp".to_owned(), timestamp.clone());

let mut context = doc.get_render_context(&posts_data);
let doc_html = try!(doc.render(&mut context, source, &layouts, &mut layouts_cache));
Expand Down
13 changes: 13 additions & 0 deletions tests/fixtures/timestamp/_layouts/default.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<link rel="stylesheet" href="/css/main.css?v={{ timestamp }}">
</head>
<body>
<h1>{{ path }}</h1>

{{ content }}
</body>
</html>

10 changes: 10 additions & 0 deletions tests/fixtures/timestamp/_layouts/posts.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>My blog - {{ title }}</title>
</head>
<body>
{{ content }}
</body>
</html>

7 changes: 7 additions & 0 deletions tests/fixtures/timestamp/index.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extends: default.liquid
---
This is my Index page!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If your goal is to test the timestamp feature, is there a reason to have posts, or having the timestamp in the file you extend rather than having it in your actual index.liquid?

Boiling down tests to cover only the feature in question helps to make the tests more stable and reduces churn when something unrelated changes that causes either minor tweaks (like syntect making slight layout changes) or major ones (us breaking compatibility as we work towards 1.0).


{% for post in posts %}
<a href="{{post.path}}">{{ post.title }}</a>
{% endfor %}
10 changes: 10 additions & 0 deletions tests/fixtures/timestamp/posts/2014-08-24-my-first-blogpost.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
extends: posts.liquid

title: My first Blogpost
date: 24/08/2014 at 15:36
---
# {{ title }}

Hey there this is my first blogpost and this is super awesome.

My Blog is lorem ipsum like, yes it is..
33 changes: 33 additions & 0 deletions tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ extern crate difference;
extern crate cobalt;
extern crate tempdir;
extern crate walkdir;
extern crate chrono;
extern crate regex;

use std::path::Path;
use std::fs::{self, File};
Expand All @@ -11,6 +13,8 @@ use tempdir::TempDir;
use walkdir::WalkDir;
use std::error::Error;
use cobalt::Config;
use chrono::UTC;
use regex::Regex;

fn run_test(name: &str) -> Result<(), cobalt::Error> {
let target = format!("tests/target/{}/", name);
Expand Down Expand Up @@ -226,3 +230,32 @@ pub fn empty_frontmatter() {
pub fn querystrings() {
run_test("querystrings").expect("Build error");
}

#[test]
pub fn timestamp() {
// timestamp adds current ms time so need to build the target folder at run time
let mut config = Config::from_file("tests/fixtures/timestamp/.cobalt.yml").unwrap_or_default();
config.source = "tests/fixtures/timestamp/".to_string();
config.dest = "tests/target/timestamp/".to_string();

fs::create_dir_all(&config.dest).is_ok();

let timestamp = UTC::now().timestamp().to_string();
let result = cobalt::build(&config);

if result.is_ok() {
// check timestamp is in the target layout
let mut content = String::new();
let _file = File::open("tests/target/timestamp/index.html")
.unwrap()
.read_to_string(&mut content);
let re = Regex::new(&timestamp.as_str()).unwrap();
assert!(re.is_match(&content.as_str()));

// run standard test to make sure it doesn't fall over
run_test("timestamp").expect("Build error");

// delete file with timestamp otherwise next test build will fail
let _deleted = fs::remove_file("tests/target/timestamp/index.html");
}
}
13 changes: 13 additions & 0 deletions tests/target/timestamp/posts/2014-08-24-my-first-blogpost.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<title>My blog - My first Blogpost</title>
</head>
<body>
<h1>My first Blogpost</h1>
<p>Hey there this is my first blogpost and this is super awesome.</p>
<p>My Blog is lorem ipsum like, yes it is..</p>

</body>
</html>