-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set
X-Date-Temporal-Context
header for easy cache rules (#209)
Set `X-Date-Temporal-Context: [past|present|future]` header for easy cache rules: - Cache `past` things heavily - Cache `present`/`future` things for 5 minutes This accomplishes the goal we set out for: > - We can cache all responses except for the latest UTC day (and anything in the future). ex. `/!aMzLHLvScQCGKDNqCB:gitter.im/date/2022/10/13` > - For the latest day, we could set the cache expire after 5 minutes or so > > *-- [Matrix Public Archive deployment issue](element-hq/sre-internal#2079 And this way we don't have to do any fancy date parsing and comparison from the URL which is probably not even possible Cloudflare cache rules.
- Loading branch information
1 parent
b70439e
commit 9b067f8
Showing
4 changed files
with
127 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
|
||
const { getUtcStartOfDayTs } = require('matrix-public-archive-shared/lib/timestamp-utilities'); | ||
|
||
// `X-Date-Temporal-Context` indicates the temporal context of the content, whether it | ||
// is related to past, present, or future *day*. | ||
// | ||
// This is useful for caching purposes so you can heavily cache past content, but not | ||
// present/future. | ||
function setHeadersForDateTemporalContext({ res, nowTs, comparedToUrlDate: { yyyy, mm, dd } }) { | ||
assert(res); | ||
assert(Number.isInteger(nowTs)); | ||
assert(Number.isInteger(yyyy)); | ||
assert(Number.isInteger(mm)); | ||
assert(Number.isInteger(dd)); | ||
|
||
// We use the start of the UTC day so we can compare apples to apples with a new date | ||
// constructed with yyyy-mm-dd (no time occured since the start of the day) | ||
const startOfTodayTs = getUtcStartOfDayTs(nowTs); | ||
const compareTs = Date.UTC(yyyy, mm, dd); | ||
|
||
let temporalContext = 'present'; | ||
if (compareTs < startOfTodayTs) { | ||
temporalContext = 'past'; | ||
} else if (compareTs > startOfTodayTs) { | ||
temporalContext = 'future'; | ||
} | ||
|
||
res.set('X-Date-Temporal-Context', temporalContext); | ||
} | ||
|
||
module.exports = setHeadersForDateTemporalContext; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters