Skip to content

feat(service-worker): add support for configuring navigations URLs #23339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from

Conversation

gkalpak
Copy link
Member

@gkalpak gkalpak commented Apr 12, 2018

PR Checklist

PR Type

[x] Feature

What is the current behavior?

The ServiceWorker will redirect navigation requests that don't match any asset or data group to the specified index file. The rules for a request to be classified as a navigation request are as follows:

  1. Its mode must be navigation.
  2. It must accept a text/html response.
  3. Its URL must match certain criteria (see below).

By default, a navigation request can have any URL except for:

  1. URLs containing __.
  2. URLs to files (i.e. containing a file extension in the last path segment).

While thse rules are fine in many cases, sometimes it is desirable to configure different rules for the URLs of navigation requests (e.g. ignore specific URLs and pass them through to the server).

Issue Number: #20404

What is the new behavior?

It is now possible to specify an optional navigationUrls list in ngsw-config.json, which contains URLs or simple globs (currently only recognizing !, * and **). Only requests whose URLs match any of the positive URLs/patterns and none of the negative ones (i.e. URLs/patterns starting with !) will be considered navigation requests (and handled accordingly by the SW).

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Other information

This is an alternative implementation to #23025.
Fixes #20404.

@gkalpak gkalpak added feature Issue that requests a new feature action: review The PR is still awaiting reviews from at least one requested reviewer target: major This PR is targeted for the next major release area: service-worker Issues related to the @angular/service-worker package labels Apr 12, 2018
@gkalpak gkalpak requested a review from alxhub April 12, 2018 15:30
@gkalpak gkalpak added state: WIP and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Apr 12, 2018
@mary-poppins
Copy link

You can preview 369967a at https://pr23339-369967a.ngbuilds.io/.

@mary-poppins
Copy link

You can preview 5e2542c at https://pr23339-5e2542c.ngbuilds.io/.

gkalpak added 2 commits April 13, 2018 02:12
The ServiceWorker will redirect navigation requests that don't match any
`asset` or `data` group to the specified index file. The rules for a
request to be classified as a navigation request are as follows:
1. Its `mode` must be `navigation`.
2. It must accept a `text/html` response.
3. Its URL must match certain criteria (see below).

By default, a navigation request can have any URL except for:
1. URLs containing `__`.
2. URLs to files (i.e. containing a file extension in the last path
   segment).

While these rules are fine in many cases, sometimes it is desirable to
configure different rules for the URLs of navigation requests (e.g.
ignore specific URLs and pass them through to the server).

This commit adds support for specifying an optional `navigationUrls`
list in `ngsw-config.json`, which contains URLs or simple globs
(currently only recognizing `!`, `*` and `**`).
Only requests whose URLs match any of the positive URLs/patterns and
none of the negative ones (i.e. URLs/patterns starting with `!`) will be
considered navigation requests (and handled accordingly by the SW).

(This is an alternative implementation to angular#23025.)

Fixes angular#20404
In [glob patterns][1], the `*` wildcard is supposed to match 0 or more
characters.

For reference:
- This is also how `*` works in other implementations, such as
  `.gitignore` files or Firebase hosting config.
- Some popular JS implementations (e.g. [minimatch][2], [micromatch][3])
  work differently, matching 1 or more character (but not 0).

This commit "fixes" the minimal glob support in
`@angular/service-worker` to allow `*` to also match 0 characters.

[1]: https://en.wikipedia.org/wiki/Glob_%28programming%29
[2]: https://www.npmjs.com/package/minimatch
[3]: https://www.npmjs.com/package/micromatch
@mary-poppins
Copy link

You can preview b5e94eb at https://pr23339-b5e94eb.ngbuilds.io/.

@gkalpak gkalpak added action: review The PR is still awaiting reviews from at least one requested reviewer and removed state: WIP labels Apr 12, 2018
@@ -551,13 +550,14 @@ export class Driver implements Debuggable, UpdateSource {
// Check if there is an assigned client id.
if (this.clientVersionMap.has(clientId)) {
// There is an assignment for this client already.
let hash = this.clientVersionMap.get(clientId) !;
const hash = this.clientVersionMap.get(clientId) !;
let appVersion = this.lookupVersionByHash(hash, 'assignVersion');
Copy link
Member

Choose a reason for hiding this comment

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

This call can throw if the SW is not in an okay state to read this.versions (if this.state is not DriverReadyState.NORMAL. Let's guard this lookup so it only happens if the SW is in a normal state state.

Copy link
Member Author

Choose a reason for hiding this comment

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

Discussed "offline". lookupVersionByHash() was called without checking the state anyway, so this PR does not change anything in that regard.

@IgorMinar IgorMinar added the action: merge The PR is ready for merge by the caretaker label Apr 13, 2018
@ngbot
Copy link

ngbot bot commented Apr 13, 2018

I see that you just added the PR action: merge label, but the following checks are still failing:
    failure forbidden label detected: PR action: review
    pending status "google3" is pending

If you want your PR to be merged, it has to pass all the CI checks.

If you can't get the PR to a green state due to flakes or broken master, please try rebasing to master and/or restarting the CI job. If that fails and you believe that the issue is not due to your change, please contact the caretaker and ask for help.

@IgorMinar IgorMinar removed the action: review The PR is still awaiting reviews from at least one requested reviewer label Apr 13, 2018
@IgorMinar IgorMinar closed this in d1e716b Apr 13, 2018
IgorMinar pushed a commit that referenced this pull request Apr 13, 2018
…23339)

The ServiceWorker will redirect navigation requests that don't match any
`asset` or `data` group to the specified index file. The rules for a
request to be classified as a navigation request are as follows:
1. Its `mode` must be `navigation`.
2. It must accept a `text/html` response.
3. Its URL must match certain criteria (see below).

By default, a navigation request can have any URL except for:
1. URLs containing `__`.
2. URLs to files (i.e. containing a file extension in the last path
   segment).

While these rules are fine in many cases, sometimes it is desirable to
configure different rules for the URLs of navigation requests (e.g.
ignore specific URLs and pass them through to the server).

This commit adds support for specifying an optional `navigationUrls`
list in `ngsw-config.json`, which contains URLs or simple globs
(currently only recognizing `!`, `*` and `**`).
Only requests whose URLs match any of the positive URLs/patterns and
none of the negative ones (i.e. URLs/patterns starting with `!`) will be
considered navigation requests (and handled accordingly by the SW).

(This is an alternative implementation to #23025.)

Fixes #20404

PR Close #23339
IgorMinar pushed a commit that referenced this pull request Apr 13, 2018
In [glob patterns][1], the `*` wildcard is supposed to match 0 or more
characters.

For reference:
- This is also how `*` works in other implementations, such as
  `.gitignore` files or Firebase hosting config.
- Some popular JS implementations (e.g. [minimatch][2], [micromatch][3])
  work differently, matching 1 or more character (but not 0).

This commit "fixes" the minimal glob support in
`@angular/service-worker` to allow `*` to also match 0 characters.

[1]: https://en.wikipedia.org/wiki/Glob_%28programming%29
[2]: https://www.npmjs.com/package/minimatch
[3]: https://www.npmjs.com/package/micromatch

PR Close #23339
@gkalpak gkalpak deleted the feat-sw-nav-urls branch April 15, 2018 11:57
@angular-automatic-lock-bot
Copy link

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 15, 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 feature Issue that requests a new feature target: major This PR is targeted for the next major release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: ngsw navigation path exceptions
5 participants