Skip to content
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

fix(service-worker): fix handling absolute and relative base href #37922

Closed
wants to merge 5 commits into from

Conversation

gkalpak
Copy link
Member

@gkalpak gkalpak commented Jul 4, 2020

Various fixes to allow the Angular ServiceWorker to correctly work when the app has an absolute or relative base href.
See individual commits for details.

Fixes #25055 and #30505.

@gkalpak gkalpak added area: service-worker Issues related to the @angular/service-worker package state: WIP target: patch This PR is targeted for the next patch release type: bug/fix labels Jul 4, 2020
@ngbot ngbot bot modified the milestone: needsTriage Jul 4, 2020
@mary-poppins
Copy link

You can preview 17b9d4b at https://pr37922-17b9d4b.ngbuilds.io/.

@mary-poppins
Copy link

You can preview 63974dd at https://pr37922-63974dd.ngbuilds.io/.

@mary-poppins
Copy link

You can preview 3953c4a at https://pr37922-3953c4a.ngbuilds.io/.

@mary-poppins
Copy link

You can preview 4d3843f at https://pr37922-4d3843f.ngbuilds.io/.

@mary-poppins
Copy link

You can preview e574fbb at https://pr37922-e574fbb.ngbuilds.io/.

@gkalpak gkalpak force-pushed the fix-sw-baseHref branch 2 times, most recently from 58f4704 to 407f8ff Compare July 5, 2020 20:31
@mary-poppins
Copy link

You can preview 407f8ff at https://pr37922-407f8ff.ngbuilds.io/.

…scope

The Angular ServiceWorker can serve requests to a special virtual path,
`ngsw/state`, showing [information about its internal state][1], which
can be useful for debugging.

Previously, this would only work if the ServiceWorker's [scope][2] was
the root directory (`/`). Otherwise, (e.g. when building the app with
`--baseHref=/some/path/`), the ServiceWorker would fail to detect a
request to `/some/path/ngsw/state` as matching `ngsw/state` and would
not serve it with the debugging information.

This commit fixes it by ensuring that the ServiceWorker's scope is taken
into account when detecting a request to `ngsw/state`.

[1]: https://angular.io/guide/service-worker-devops#locating-and-analyzing-debugging-information
[2]: https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/scope

Fixes angular#30505
This is in preparation of enabling the ServiceWorker to handle
relative paths in `ngsw.json` (as discussed in angular#25055), which will
require normalizing URLs in other parts of the ServiceWorker.
…l ones

This commit makes the mock implementations used is ServiceWorker tests
behave more similar to the actual ones.
@mary-poppins
Copy link

You can preview 6eb027c at https://pr37922-6eb027c.ngbuilds.io/.

@gkalpak gkalpak marked this pull request as ready for review July 6, 2020 16:26
In some cases, it is useful to use a relative base href in the app (e.g.
when an app has to be accessible on different URLs, such as on an
intranet and the internet - see angular#25055 for a related discussion).

Previously, the Angular ServiceWorker was not able to handle relative
base hrefs (for example when building the with `--base-href=./`).

This commit fixes this by normalizing all URLs from the ServiceWorker
configuration wrt the ServiceWorker's scope.

Fixes angular#25055
Some ServiceWorker operations and methods require normalized URLs.
Previously, the generic `string` type was used.

This commit introduces a new `NormalizedUrl` type, a special kind of
`string`, to make this requirement explicit and use the type system to
enforce it.
@mary-poppins
Copy link

You can preview d59b181 at https://pr37922-d59b181.ngbuilds.io/.

fs.list().forEach(path => {
const file = fs.lookup(path)!;
this.resources.set(path, new MockResponse(file.contents, {headers: file.headers}));
withRootDirectory(newRootDir: string): MockServerStateBuilder {
Copy link
Member

Choose a reason for hiding this comment

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

So clean, I love it.

@gkalpak gkalpak added action: merge The PR is ready for merge by the caretaker and removed state: WIP labels Jul 7, 2020
@pullapprove pullapprove bot requested a review from AndrewKushnir July 7, 2020 19:57
@atscott atscott closed this in 2156bee Jul 9, 2020
atscott pushed a commit that referenced this pull request Jul 9, 2020
…37922)

This is in preparation of enabling the ServiceWorker to handle
relative paths in `ngsw.json` (as discussed in #25055), which will
require normalizing URLs in other parts of the ServiceWorker.

PR Close #37922
atscott pushed a commit that referenced this pull request Jul 9, 2020
…l ones (#37922)

This commit makes the mock implementations used is ServiceWorker tests
behave more similar to the actual ones.

PR Close #37922
atscott pushed a commit that referenced this pull request Jul 9, 2020
In some cases, it is useful to use a relative base href in the app (e.g.
when an app has to be accessible on different URLs, such as on an
intranet and the internet - see #25055 for a related discussion).

Previously, the Angular ServiceWorker was not able to handle relative
base hrefs (for example when building the with `--base-href=./`).

This commit fixes this by normalizing all URLs from the ServiceWorker
configuration wrt the ServiceWorker's scope.

Fixes #25055

PR Close #37922
atscott pushed a commit that referenced this pull request Jul 9, 2020
Some ServiceWorker operations and methods require normalized URLs.
Previously, the generic `string` type was used.

This commit introduces a new `NormalizedUrl` type, a special kind of
`string`, to make this requirement explicit and use the type system to
enforce it.

PR Close #37922
atscott pushed a commit that referenced this pull request Jul 9, 2020
…scope (#37922)

The Angular ServiceWorker can serve requests to a special virtual path,
`ngsw/state`, showing [information about its internal state][1], which
can be useful for debugging.

Previously, this would only work if the ServiceWorker's [scope][2] was
the root directory (`/`). Otherwise, (e.g. when building the app with
`--baseHref=/some/path/`), the ServiceWorker would fail to detect a
request to `/some/path/ngsw/state` as matching `ngsw/state` and would
not serve it with the debugging information.

This commit fixes it by ensuring that the ServiceWorker's scope is taken
into account when detecting a request to `ngsw/state`.

[1]: https://angular.io/guide/service-worker-devops#locating-and-analyzing-debugging-information
[2]: https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/scope

Fixes #30505

PR Close #37922
atscott pushed a commit that referenced this pull request Jul 9, 2020
…37922)

This is in preparation of enabling the ServiceWorker to handle
relative paths in `ngsw.json` (as discussed in #25055), which will
require normalizing URLs in other parts of the ServiceWorker.

PR Close #37922
atscott pushed a commit that referenced this pull request Jul 9, 2020
…l ones (#37922)

This commit makes the mock implementations used is ServiceWorker tests
behave more similar to the actual ones.

PR Close #37922
atscott pushed a commit that referenced this pull request Jul 9, 2020
In some cases, it is useful to use a relative base href in the app (e.g.
when an app has to be accessible on different URLs, such as on an
intranet and the internet - see #25055 for a related discussion).

Previously, the Angular ServiceWorker was not able to handle relative
base hrefs (for example when building the with `--base-href=./`).

This commit fixes this by normalizing all URLs from the ServiceWorker
configuration wrt the ServiceWorker's scope.

Fixes #25055

PR Close #37922
atscott pushed a commit that referenced this pull request Jul 9, 2020
Some ServiceWorker operations and methods require normalized URLs.
Previously, the generic `string` type was used.

This commit introduces a new `NormalizedUrl` type, a special kind of
`string`, to make this requirement explicit and use the type system to
enforce it.

PR Close #37922
@gkalpak gkalpak deleted the fix-sw-baseHref branch July 9, 2020 16:59
@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 Aug 9, 2020
profanis pushed a commit to profanis/angular that referenced this pull request Sep 5, 2020
…scope (angular#37922)

The Angular ServiceWorker can serve requests to a special virtual path,
`ngsw/state`, showing [information about its internal state][1], which
can be useful for debugging.

Previously, this would only work if the ServiceWorker's [scope][2] was
the root directory (`/`). Otherwise, (e.g. when building the app with
`--baseHref=/some/path/`), the ServiceWorker would fail to detect a
request to `/some/path/ngsw/state` as matching `ngsw/state` and would
not serve it with the debugging information.

This commit fixes it by ensuring that the ServiceWorker's scope is taken
into account when detecting a request to `ngsw/state`.

[1]: https://angular.io/guide/service-worker-devops#locating-and-analyzing-debugging-information
[2]: https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/scope

Fixes angular#30505

PR Close angular#37922
profanis pushed a commit to profanis/angular that referenced this pull request Sep 5, 2020
…ngular#37922)

This is in preparation of enabling the ServiceWorker to handle
relative paths in `ngsw.json` (as discussed in angular#25055), which will
require normalizing URLs in other parts of the ServiceWorker.

PR Close angular#37922
profanis pushed a commit to profanis/angular that referenced this pull request Sep 5, 2020
…l ones (angular#37922)

This commit makes the mock implementations used is ServiceWorker tests
behave more similar to the actual ones.

PR Close angular#37922
profanis pushed a commit to profanis/angular that referenced this pull request Sep 5, 2020
In some cases, it is useful to use a relative base href in the app (e.g.
when an app has to be accessible on different URLs, such as on an
intranet and the internet - see angular#25055 for a related discussion).

Previously, the Angular ServiceWorker was not able to handle relative
base hrefs (for example when building the with `--base-href=./`).

This commit fixes this by normalizing all URLs from the ServiceWorker
configuration wrt the ServiceWorker's scope.

Fixes angular#25055

PR Close angular#37922
profanis pushed a commit to profanis/angular that referenced this pull request Sep 5, 2020
…ar#37922)

Some ServiceWorker operations and methods require normalized URLs.
Previously, the generic `string` type was used.

This commit introduces a new `NormalizedUrl` type, a special kind of
`string`, to make this requirement explicit and use the type system to
enforce it.

PR Close angular#37922
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 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.

Service Worker Http Error 504 When Build Using --base-href=./ (Dynamic Base Href)
5 participants