Skip to content

Ability to pass options to service worker's fetch request(s). #38907

Closed

Description

@ghost

Versions

Angular CLI: 1.7.3
Node: 8.11.1
OS: darwin x64
Angular: 5.2.9
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router, service-worker

@angular/cdk: 5.2.4
@angular/cli: 1.7.3
@angular/flex-layout: 5.0.0-beta.14
@angular/material: 5.2.4
@angular-devkit/build-optimizer: 0.3.2
@angular-devkit/core: 0.3.2
@angular-devkit/schematics: 0.3.2
@ngtools/json-schema: 1.2.0
@ngtools/webpack: 1.10.2
@schematics/angular: 0.3.2
@schematics/package-update: 0.3.2
typescript: 2.6.2
webpack: 3.11.0

Observed behavior

Our app is located behind a security gateway and is using a service worker. The service worker was integrated following this guide. Because of the fact that any request is passed through the security gateway we had to provide additional options to the construction of any Request object, namely credentials and mode, when a service worker is requesting its manifest from the server. Otherwise manifest fetch fails with CORS error:

No 'Access-Control-Allow-Origin' header is present on the requested resource.

Desired behavior

If I could use the ngsw-config.json file, or some other means of configuration, to set some global options which in return are applied to any fetch request. Like so:

{

  "index": "index.html",
  "request-options": {
    "credentials" : "same-origin",
    "mode": "cors"
  },
  "assetGroups": [
    {
      "name": "app",
      "installMode": "prefetch",
      "resources": {
        "files": [
          "/assets/images/favicon.png",
          "/index.html"
        ],
        "versionedFiles": [
          "/*.bundle.css",
          "/*.bundle.js",
          "/*.chunk.js"
        ]
      }
    },
    {
      "name": "assets",
      "installMode": "lazy",
      "updateMode": "prefetch",
      "resources": {
        "files": [
          "/assets/**"
        ],
        "urls": [
          "https://fonts.googleapis.com/**"
        ]
      }
    }
  ]
}

Activity

  1. mina-skunk commented on Aug 1, 2018

    @mina-skunk

    I experienced this twice, strangely it is resolved by longing out and back in to my OS (stopping all chrome processes) with out publishing or any changes to the service worker config.

  2. bjornharvold commented on Nov 9, 2018

    @bjornharvold

    Same issue. Service worker tries to fetch server data without CORS headers: No 'Access-Control-Allow-Origin' header is present on the requested resource. What's the workaround right now?

  3. Jack-Sparrow commented on Jan 30, 2019

    @Jack-Sparrow

    Got same issue when fetch assets from CDN when use service worker, I had to change CDN config and add 'Access-Control-Allow-Origin';
    And I got another issue: we use custom user-agent on webview, and run angular app in webview, but service worker send request with the original user-agent but not the custom one, cause cannot pass some backend auth, maybe I can change service worker config to add 'User-Agent' in request header?

  4. ademidun commented on Aug 10, 2019

    @ademidun

    Any updates on this please?

  5. antoinebrault commented on Aug 29, 2019

    @antoinebrault

    There was a PR opened and closed. #24510

  6. Trent-Matthias commented on Sep 25, 2019

    @Trent-Matthias

    I'm having issues with a service worker behind ping and I'm assuming this is my issue as well, has there been any updates?

    Edit: I fixed my issue by adding the cross-origin attribute here in the index.html:

    <link
          rel="manifest"
          href="manifest.webmanifest"
          crossorigin="use-credentials"
    />
    
  7. leongrin commented on Sep 19, 2020

    @leongrin

    Looking for an answer for this issue too.

  8. added this to the needsTriage milestone on Sep 19, 2020
  9. gkalpak commented on Sep 20, 2020

    @gkalpak
    Member

    Sorry for the late response 😞
    I was just made aware of this issue and I am having some difficulties understanding the underlying problems. For example:

    I do get that it might be a problem that the SW is dropping the request headers, as mentioned by @Jack-Sparrow in #38907 (comment). This is being tracked in #24227.

    Based on the above (and the fact that the issue was opened ~2.5 years ago - a lot of SW issues have been fixed since, both in browsers and in Angular), I am going to close the issue.

    If the problem you are facing is related to the SW's removing request headers, let's continue the discussion in #24227.
    If the problem is different, please open a new issue (providing a clear description of the usecase and if possible a minimal reproduction).

    Thank you all!

  10. angular-automatic-lock-bot commented on Oct 21, 2020

    @angular-automatic-lock-bot

    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.

  11. locked and limited conversation to collaborators on Oct 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: service-workerIssues related to the @angular/service-worker package

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions