Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

Fix browser cache headers from always being set #37

Merged
merged 6 commits into from
Sep 30, 2019
Merged

Conversation

exvuma
Copy link
Contributor

@exvuma exvuma commented Sep 27, 2019

For cache HITs we always returned the edge cache TTLs to the client, which in turn means the browser would be caching as long as the edge does (100 days)

curl -X 'GET' -v https://sites.workers-tooling.cf/workers/ 2>&1 | grep cache
< cf-cache-status: HIT
< cache-control: public, max-age=8640000

Also, I set the edge cache to a safer, more realistic value (no edge would likely not get close to caching an asset for 100 days)

fixes #38

browserTTL: 0,
edgeTTL: 100 * 60 * 60 * 24, // 100 days
browserTTL: null,
edgeTTL: 2 * 60 * 60 * 24, // 2 days
Copy link
Contributor

Choose a reason for hiding this comment

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

lets keep this at 100?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it's weird IMO because it would never stay on cache for that long and puts us and developers at risks for situations like this.

src/index.js Outdated Show resolved Hide resolved
Copy link
Contributor

@EverlastingBugstopper EverlastingBugstopper left a comment

Choose a reason for hiding this comment

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

cache-control: max-age=undefined

Hey so this doesn't quite work, I'm fiddling around with it

I think if you change the if (options.browserTTL === null) to if if (!options.browserTTL) it seems to work as expected :) should add some tests for this. I am a little confused as to why exactly options.browserTTL was undefined in the first place... since you explicitly set it to null

@exvuma
Copy link
Contributor Author

exvuma commented Sep 30, 2019

cache-control: max-age=undefined

Hey so this doesn't quite work, I'm fiddling around with it

I think if you change the if (options.browserTTL === null) to if if (!options.browserTTL) it seems to work as expected :) should add some tests for this. I am a little confused as to why exactly options.browserTTL was undefined in the first place... since you explicitly set it to null

Fixed

src/index.js Outdated
@@ -121,6 +121,13 @@ const getAssetFromKV = async (event, options) => {
if (response) {
let headers = new Headers(response.headers)
headers.set('CF-Cache-Status', 'HIT')
if (options.cacheControl.browserTTL === 0 || options.cacheControl.browserTTL) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should separate this boolean check to its own function that returns a boolean, and it should also return false if options.cacheControl.browserTTL is negative

Copy link
Contributor Author

@exvuma exvuma Sep 30, 2019

Choose a reason for hiding this comment

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

so like:

const toSetBrowserTTL = cacheControl => cacheControl.browserTTL === 0 || cacheControl.browserTTL

? is purpose for readability ?

Copy link
Contributor

Choose a reason for hiding this comment

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

readability and also so we don't repeat ourselves, this code is used twice - generally that should be a separate function imo.

const isValidBrowserTTL = browserTTL => {
  let ttl = parseInt(browserTTL, 10)
  if (typeof ttl !== "number" || ttl < 0) {
    return false
  }
  return true
}

Copy link
Contributor

@EverlastingBugstopper EverlastingBugstopper left a comment

Choose a reason for hiding this comment

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

looks great!

@exvuma exvuma merged commit 90a83a5 into master Sep 30, 2019
@exvuma exvuma deleted the victoria/cache branch September 30, 2019 17:41
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Browser Cache issue
3 participants