Skip to content

fix(service-worker): Cache-Control: no-cache on assets breaks service worker#25408

Closed
alan-agius4 wants to merge 1 commit into
angular:masterfrom
alan-agius4:fix_sw_assets
Closed

fix(service-worker): Cache-Control: no-cache on assets breaks service worker#25408
alan-agius4 wants to merge 1 commit into
angular:masterfrom
alan-agius4:fix_sw_assets

Conversation

@alan-agius4

@alan-agius4 alan-agius4 commented Aug 9, 2018

Copy link
Copy Markdown
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[x] Bugfix
[ ] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[ ] angular.io application / infrastructure changes
[ ] Other... Please describe:

What is the current behavior?

At the moment cacheAge can be undefined and when it is so, it causes cannot read lengthofundefinedwhen 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?

[ ] Yes
[x] No

Other information

Closes #25442

@alan-agius4 alan-agius4 Aug 9, 2018

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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 error

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could this be added as test 👍

@alan-agius4 alan-agius4 Aug 9, 2018

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I am trying to see how to test it to be honest :) any help?

@JoostK JoostK Aug 9, 2018

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@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 👍

@alan-agius4 alan-agius4 Aug 9, 2018

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yep you are absolutely right.

@alan-agius4

Copy link
Copy Markdown
Contributor Author

@alxhub, can you have a look at this please?

@alxhub alxhub left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

TYVM

@alan-agius4 alan-agius4 changed the title fix(service-worker): cannot read length of undefined fix(service-worker): Cache-Control: no-cache on assets breaks service worker Aug 12, 2018
@alan-agius4

Copy link
Copy Markdown
Contributor Author

Note: I just changed the commit msg and referenced an issue.

Can we please add the relative labels to get this merged?

Thanks.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There is a maxAge variable declared few lines below. Consider giving them different names to avoid confusion.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@gkalpak, I updated it.

…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
@gkalpak gkalpak added type: bug/fix action: merge The PR is ready for merge by the caretaker target: patch This PR is targeted for the next patch release area: service-worker Issues related to the @angular/service-worker package merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note labels Aug 14, 2018
@IgorMinar

Copy link
Copy Markdown
Contributor

caretaker note: no g3 impact

benlesh pushed a commit that referenced this pull request Aug 14, 2018
…ce worker (#25408)

At the moment `cacheAge` can we undefined when having `Cache-Control` set to `no-cache` due the mapping method in `needToRevalidate`

Closes #25442

PR Close #25408
@benlesh benlesh closed this in 01ec5fd Aug 14, 2018
@alan-agius4 alan-agius4 deleted the fix_sw_assets branch August 15, 2018 06:32
// 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;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

maxAgeDirective[1] ?

@angular-automatic-lock-bot

Copy link
Copy Markdown

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot Bot locked and limited conversation to collaborators Sep 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker area: service-worker Issues related to the @angular/service-worker package cla: yes merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note target: patch This PR is targeted for the next patch release type: bug/fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cache-Control no-cache on assets breaking service worker

7 participants