fix(service-worker): Cache-Control: no-cache on assets breaks service worker#25408
fix(service-worker): Cache-Control: no-cache on assets breaks service worker#25408alan-agius4 wants to merge 1 commit into
Cache-Control: no-cache on assets breaks service worker#25408Conversation
There was a problem hiding this comment.
Note: At the moment current it will fail when having
const cacheAge = [["no-cache"]].filter(v => v[0] === 'max-age').map(v => v[1])[0] // this is undefined
cacheAge.length // This is an errorThere was a problem hiding this comment.
I am trying to see how to test it to be honest :) any help?
There was a problem hiding this comment.
@alan-agius4 I am not familiar with the service worker code at all, sorry. At first glance, looks like we can pull out quite a bit to a separate pure function to easily test:
export function parseCacheControlIntoMaxAge(cacheControl: string) {
const cacheDirectives =
cacheControl
// Directives are comma-separated within the Cache-Control header value.
.split(',')
// Make sure each directive doesn't have extraneous whitespace.
.map(v => v.trim())
// Some directives have values (like maxage and s-maxage)
.map(v => v.split('='));
// Find the max-age directive, if one exists.
const maxAge = cacheDirectives.find(v => v[0].toLowerCase() === 'max-age');
return maxAge ? maxAge[1] : undefined;
}Let's see what @gkalpak thinks 👍
There was a problem hiding this comment.
I managed to test it. Thanks anyways.
Also for me personally, I don't like to export a function just to be able to unit test it.
There was a problem hiding this comment.
I'm okay with exporting it just to test it. Exporting it from a private module within the package is different than exporting it as public API.
There was a problem hiding this comment.
Yep you are absolutely right.
|
@alxhub, can you have a look at this please? |
length of undefinedCache-Control: no-cache on assets breaks service worker
|
Note: I just changed the commit msg and referenced an issue. Can we please add the relative Thanks. |
There was a problem hiding this comment.
There is a maxAge variable declared few lines below. Consider giving them different names to avoid confusion.
…ce worker At the moment `cacheAge` can we undefined when having `Cache-Control` set to `no-cache` due the mapping method in `needToRevalidate` Closes #25442
|
caretaker note: no g3 impact |
| // Find the max-age directive, if one exists. | ||
| const cacheAge = cacheDirectives.filter(v => v[0] === 'max-age').map(v => v[1])[0]; | ||
| const maxAgeDirective = cacheDirectives.find(v => v[0] === 'max-age'); | ||
| const cacheAge = maxAgeDirective ? maxAgeDirective[1] : undefined; |
|
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
At the moment
cacheAgecan be undefined and when it is so, it causescannot readlengthofundefinedwhen havingCache-Controlset tono-cachedue the mapping method inneedToRevalidate`Issue Number: N/A
What is the new behavior?
No error
Does this PR introduce a breaking change?
Other information
Closes #25442