Skip to content

Commit 1ec01b2

Browse files
committedMar 1, 2023
Browser-cache HTML files for one minute
1 parent 4ef01bb commit 1ec01b2

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed
 

‎loadHnComments.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { html } from "./utils/misc.ts"
1+
import { html, ONE_DAY, ONE_HOUR, ONE_MINUTE, ONE_MONTH, ONE_YEAR } from "./utils/misc.ts"
22

33
type PostComments = {
44
postId: string,
@@ -131,9 +131,3 @@ function ago(date: Date) {
131131
}
132132

133133
const s = (num: number) => num === 1 ? '' : 's'
134-
135-
const ONE_MINUTE = 60 * 1000
136-
const ONE_HOUR = 60 * ONE_MINUTE
137-
const ONE_DAY = 24 * ONE_HOUR
138-
const ONE_MONTH = 30 * ONE_DAY
139-
const ONE_YEAR = 365 * ONE_DAY

‎staticLoader.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import index from './render/index.html.ts'
1111
import feed from './render/feed.xml.ts'
1212
import blogPost from './render/blog-post.html.ts'
1313
import CleanCSS from "./deps/clean-css.ts";
14+
import { ONE_HOUR, ONE_MINUTE } from './utils/misc.ts';
1415

1516
const SIMPLE_PAGES = {
1617
'404': fourOhFour,
@@ -20,6 +21,9 @@ const SIMPLE_PAGES = {
2021
'feed.xml': feed
2122
} as const
2223

24+
const ONE_MINUTE_S = ONE_MINUTE / 1000
25+
const ONE_HOUR_S = ONE_HOUR / 1000
26+
2327
export const createFileMap = async () => {
2428
const fileMap = new Map<string, { content: Uint8Array, headers: HeadersInit }>()
2529
const encoder = new TextEncoder()
@@ -47,7 +51,7 @@ export const createFileMap = async () => {
4751
content: allCSSArray,
4852
headers: {
4953
'Content-Type': CONTENT_TYPES.css,
50-
'Cache-Control': `max-age=${60 * 60}`
54+
'Cache-Control': `max-age=${ONE_HOUR_S}`
5155
}
5256
})
5357
}
@@ -62,7 +66,7 @@ export const createFileMap = async () => {
6266
content,
6367
headers: {
6468
'Content-Type': CONTENT_TYPES[fileExtension],
65-
'Cache-Control': `max-age=${60 * 60}`
69+
'Cache-Control': `max-age=${ONE_HOUR_S}`
6670
}
6771
})
6872
}
@@ -80,7 +84,8 @@ export const createFileMap = async () => {
8084
fileMap.set(`/${pageName}`, {
8185
content: encoder.encode(render({ allTags, posts })),
8286
headers: {
83-
'Content-Type': CONTENT_TYPES.html
87+
'Content-Type': CONTENT_TYPES.html,
88+
'Cache-Control': `max-age=${ONE_MINUTE_S}`
8489
}
8590
})
8691
}
@@ -89,7 +94,8 @@ export const createFileMap = async () => {
8994
fileMap.set('/', {
9095
content: encoder.encode(index({ allTags, posts })),
9196
headers: {
92-
'Content-Type': CONTENT_TYPES.html
97+
'Content-Type': CONTENT_TYPES.html,
98+
'Cache-Control': `max-age=${ONE_MINUTE_S}`
9399
}
94100
})
95101

@@ -98,7 +104,8 @@ export const createFileMap = async () => {
98104
const file = {
99105
content: encoder.encode(index({ allTags, posts, tag })),
100106
headers: {
101-
'Content-Type': CONTENT_TYPES.html
107+
'Content-Type': CONTENT_TYPES.html,
108+
'Cache-Control': `max-age=${ONE_MINUTE_S}`
102109
}
103110
}
104111

@@ -111,7 +118,8 @@ export const createFileMap = async () => {
111118
const file = {
112119
content: encoder.encode(blogPost({ post })),
113120
headers: {
114-
'Content-Type': CONTENT_TYPES.html
121+
'Content-Type': CONTENT_TYPES.html,
122+
'Cache-Control': `max-age=${ONE_MINUTE_S}`
115123
}
116124
}
117125

‎utils/misc.ts

+6
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,9 @@ export const capitalize = (str: string) =>
3030
str === ''
3131
? ''
3232
: str[0].toUpperCase() + str.substring(1)
33+
34+
export const ONE_MINUTE = 60 * 1000
35+
export const ONE_HOUR = 60 * ONE_MINUTE
36+
export const ONE_DAY = 24 * ONE_HOUR
37+
export const ONE_MONTH = 30 * ONE_DAY
38+
export const ONE_YEAR = 365 * ONE_DAY

0 commit comments

Comments
 (0)