Improve page speeds

* Defer loading of highlight css
* Cache /static for up to 16 days
This commit is contained in:
Chris Josten 2020-06-25 10:51:26 +02:00
parent ad1724b47f
commit 503d2d1ad0
2 changed files with 195 additions and 182 deletions

View file

@ -39,9 +39,9 @@ enum OutputType {
MARKDOWN MARKDOWN
} }
const string MIME_MARKDOWN = "text/markdown"; immutable string MIME_MARKDOWN = "text/markdown";
immutable Duration CACHE_TIME = days(16);
/** /**
* Get's the document type for the given slug based on extension * Get's the document type for the given slug based on extension
@ -64,6 +64,17 @@ private OutputType getOutputType(ref string slug) {
} }
} }
void addCachingHeader(bool publicCache = true)(ref HTTPServerResponse res) {
string header = "";
static if (publicCache) {
header ~= "public";
} else {
header ~= "private";
}
header ~= ", max-age=" ~ to!string(CACHE_TIME.total!"seconds");
res.headers["Cache-Control"] = header;
}
/** /**
* Template method for fetching a single page for a subclass of page. * Template method for fetching a single page for a subclass of page.
* Params: * Params:
@ -80,7 +91,6 @@ void getSingle(T, string templ)(ref T[string] array, HTTPServerRequest req, HTTP
enforceHTTP(slug in array, HTTPStatus.notFound, "Page not found"); enforceHTTP(slug in array, HTTPStatus.notFound, "Page not found");
T content = array[slug]; T content = array[slug];
res.headers["Cache-Control"] = "public";
switch(outputType) with (OutputType) { switch(outputType) with (OutputType) {
case MARKDOWN: case MARKDOWN:
res.writeBody(content.contentSource, MIME_MARKDOWN); res.writeBody(content.contentSource, MIME_MARKDOWN);
@ -103,7 +113,7 @@ void articleGetSingle(HTTPServerRequest req, HTTPServerResponse res) {
* Generates response for /posts and /palen * Generates response for /posts and /palen
*/ */
void articleGetOverview(HTTPServerRequest req, HTTPServerResponse res) { void articleGetOverview(HTTPServerRequest req, HTTPServerResponse res) {
res.headers["Cache-Control"] = "public"; addCachingHeader(res);
render!("pages/article-list.dt", articleList)(res); render!("pages/article-list.dt", articleList)(res);
} }
@ -111,7 +121,7 @@ void articleGetOverview(HTTPServerRequest req, HTTPServerResponse res) {
* Generates response for /projects and /projecten * Generates response for /projects and /projecten
*/ */
void projectGetOverview(HTTPServerRequest req, HTTPServerResponse res) { void projectGetOverview(HTTPServerRequest req, HTTPServerResponse res) {
res.headers["Cache-Control"] = "public"; addCachingHeader(res);
render!("pages/project-list.dt", projectList)(res); render!("pages/project-list.dt", projectList)(res);
} }
@ -119,6 +129,7 @@ void projectGetOverview(HTTPServerRequest req, HTTPServerResponse res) {
* Generate response for a page * Generate response for a page
*/ */
void pageGet(HTTPServerRequest req, HTTPServerResponse res) { void pageGet(HTTPServerRequest req, HTTPServerResponse res) {
addCachingHeader(res);
// If no slug is supplied, it will be adjusted to "index" // If no slug is supplied, it will be adjusted to "index"
if (("slug" in req.params) is null) { if (("slug" in req.params) is null) {
req.params.addField("slug", "index"); req.params.addField("slug", "index");
@ -152,6 +163,8 @@ void main() {
debug { debug {
settings.accessLogToConsole = true; settings.accessLogToConsole = true;
} }
HTTPFileServerSettings fSettings = new HTTPFileServerSettings;
fSettings.maxAge = days(16);
URLRouter router = new URLRouter; URLRouter router = new URLRouter;
router.get("/posts/:slug", &articleGetSingle); router.get("/posts/:slug", &articleGetSingle);
@ -161,7 +174,7 @@ void main() {
router.get("/projects/", &projectGetOverview); router.get("/projects/", &projectGetOverview);
router.get("/projects/:slug", &projectGet); router.get("/projects/:slug", &projectGet);
router.get("/projecten/:slug", &projectGet); router.get("/projecten/:slug", &projectGet);
router.get("/static/*", serveStaticFiles("./public/")); router.get("/static/*", serveStaticFiles("./public/", fSettings));
router.get("/:slug", &pageGet); router.get("/:slug", &pageGet);
router.get("/", &pageGet); router.get("/", &pageGet);

View file

@ -4,7 +4,7 @@ html(prefix="og: http://ogp.me/ns#")
head head
meta(name="viewport", content="width=device-width; initial-scale=1") meta(name="viewport", content="width=device-width; initial-scale=1")
link(rel="stylesheet", href="/static/style/base.css") link(rel="stylesheet", href="/static/style/base.css")
link(rel="stylesheet", href="/static/style/highlight.css") link(rel="stylesheet", href="/static/style/highlight.css", defer)
link(rel="alternate stylesheet", href="/static/style/old.css", title="1999") link(rel="alternate stylesheet", href="/static/style/old.css", title="1999")
link(rel="shortcut icon", href="/static/img/logo.png") link(rel="shortcut icon", href="/static/img/logo.png")
link(rel="apple-touch-icon", href="/static/img/logo.png") link(rel="apple-touch-icon", href="/static/img/logo.png")