Skip to content

Commit 92aa897

Browse files
committed
improve sqlpage binary size by compressing assets
1 parent 7b66c90 commit 92aa897

File tree

5 files changed

+72
-4
lines changed

5 files changed

+72
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
This allows pages to load faster, and to get a better score on google's performance audits, potentially improving your position in search results.
77
- This also makes it possible to host a SQLPage website on an intranet without access to the internet.
88
- Fixes https://github.com/lovasoa/SQLpage/issues/37
9+
- store compressed frontend assets in the SQLPage binary:
10+
- smaller SQLPage binary
11+
- Faster page loads, less work on the server
912

1013
## 0.9.2 (2023-08-01)
1114

Cargo.lock

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,5 @@ rand = "0.8.5"
4747

4848
[build-dependencies]
4949
awc = { version = "3", features = ["rustls"] }
50-
actix-rt = "2.8"
50+
actix-rt = "2.8"
51+
libflate = "2"

build.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ use std::collections::hash_map::DefaultHasher;
33
use std::fs::File;
44
use std::hash::Hasher;
55
use std::io::Read;
6-
use std::io::{BufRead, BufReader, BufWriter, Write};
6+
use std::io::{BufRead, BufReader, Write};
77
use std::path::{Path, PathBuf};
8+
use libflate::gzip;
89

910
#[actix_rt::main]
1011
async fn main() {
@@ -48,7 +49,7 @@ async fn download_deps(path_in: &str, path_out: PathBuf) {
4849

4950
async fn process_input_file(path_out: &Path, original: File) {
5051
let client = awc::Client::default();
51-
let mut outfile = BufWriter::new(File::create(path_out).unwrap());
52+
let mut outfile = gzip::Encoder::new(File::create(path_out).unwrap()).unwrap();
5253
for l in BufReader::new(original).lines() {
5354
let line = l.unwrap();
5455
if line.starts_with("/* !include https://") {
@@ -70,6 +71,7 @@ async fn process_input_file(path_out: &Path, original: File) {
7071
writeln!(outfile, "{}", line).unwrap();
7172
}
7273
}
74+
outfile.finish().as_result().expect("Unable to write compressed frontend asset");
7375
}
7476

7577
// Given a filename, creates a new unique filename based on the file contents

src/webserver/static_content.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::utils::static_filename;
22
use actix_web::{
3-
http::header::{CacheControl, CacheDirective, ETag, EntityTag, Header, IfNoneMatch},
3+
http::header::{CacheControl, CacheDirective, ETag, EntityTag, Header, IfNoneMatch, ContentEncoding},
44
web, HttpRequest, HttpResponse, Resource,
55
};
66

@@ -20,6 +20,7 @@ macro_rules! static_file_endpoint {
2020
CacheDirective::Extension("immutable".to_owned(), None),
2121
]))
2222
.insert_header(ETag(file_etag))
23+
.insert_header(ContentEncoding::Gzip)
2324
.body(
2425
&include_bytes!(concat!(env!("OUT_DIR"), "/", $filestem, ".", $extension))[..],
2526
)

0 commit comments

Comments
 (0)