Skip to content

Network requests made from service worker context don't have electron app name and version in the User Agent header #19600

Closed

Description

@sreedhar7

Preflight Checklist

  • I have read the Contributing Guidelines for this project.
  • I agree to follow the Code of Conduct that this project adheres to.
  • I have searched the issue tracker for an issue that matches the one I want to file, without success.

Issue Details

  • Electron Version:
    • 3.1.13
  • Operating System:
    • Windows 10.0.18632
  • Last Known Working Electron version:
    • Not sure if it ever worked

Expected Behavior

The user agent string for network requests made from Electron contain the Electron app name and version. For example:

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) electron-quick-start/1.0.0 Chrome/66.0.3359.181 Electron/3.1.13 Safari/537.36

The user agent should contain the "electron-quick-start/1.0.0" string irrespective of whether the requests are made from main window or service worker context. Many modern applications have a service worker which makes/intercepts most network requests to improve performance and make the app work offline. We should have the user agent with Electron app name from both contexts.

Actual Behavior

But network requests made from service worker context don't append the Electron app name and version to the user agent string. It does not contain the "electron-quick-start/1.0.0". For example:

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Electron/3.1.13 Safari/537.36

To Reproduce

Repo which outputs user agent string from both main window and service worker context by using navigator.userAgent: https://github.com/sreedhar7/electron-quick-start/tree/service-worker-user-agent-bug

Instructions to setup and run:

# Clone this repository
git clone https://github.com/sreedhar7/electron-quick-start.git
# Go into the repository
cd electron-quick-start
# Checkout service-worker-user-agent-bug branch
git checkout service-worker-user-agent-bug
# Install dependencies
npm install
# Run the app
npm start

Screenshots

Screenshot with user agent from service worker not containing the Electron app name and version:

image

Additional Information

Activity

  1. MarshallOfSound commented on Aug 2, 2019

    @MarshallOfSound
    Member

    Service workers run in a different renderer host under the hood. If you want to adjust the user agent you should use app.userAgentFallback

  2. sreedhar7 commented on Aug 2, 2019

    @sreedhar7
    Author

    We are not customizing the user agent here and want to use the default user agent string. If the user agent has been modified in Electron to include the Electron app name and version for requests made from the main thread in renderer process, I would expect that the requests made from service worker thread also to have the same user agent. There are a lot of scenarios where user agent header is used on the server side to detect versions of app/electron/browser and serve different content. Having a different user agent by default for requests made from service worker and main threads seems like a bug to me.

  3. MarshallOfSound commented on Aug 2, 2019

    @MarshallOfSound
    Member

    Oh I didn't check your electron version

    (a) 3.1.13 is EOL and you should upgrade
    (b) this is fixed in the most recent versions of Electron 👍

  4. sreedhar7 commented on Aug 2, 2019

    @sreedhar7
    Author

    Thanks for the quick response. I verified that this issue does not exist in 4.x.x and above. In general, is there any documentation around how service workers are implemented under the hood in Electron. We are seeing other issues like increased crash rate in Mac OS X etc. and would like to learn more.

  5. sreedhar7 commented on Aug 2, 2019

    @sreedhar7
    Author

    Additional note: Looks like customizing the user agent using app.userAgentFallback and other options does not work in service worker context based on this issue #16196

  6. MarshallOfSound commented on Aug 2, 2019

    @MarshallOfSound
    Member

    @sreedhar7 That issue is wrong 😄 app.userAgentFallback is a very new feature and those folks probably weren't on a new enough version of electron.

    I know for sure that app.userAgentFallback works for service workers 👍

  7. MarshallOfSound commented on Aug 2, 2019

    @MarshallOfSound
    Member

    is there any documentation around how service workers are implemented under the hood in Electron

    Service workers in Electron aren't "implemented" really at all, that all comes from Chromium upstream, we don't do anything special to support it, they Just Work ™️

  8. bughit commented on Aug 7, 2019

    @bughit
    Contributor

    @sreedhar7 That issue is wrong smile app.userAgentFallback is a very new feature and those folks probably weren't on a new enough version of electron.

    I know for sure that app.userAgentFallback works for service workers +1

    @MarshallOfSound

    • There is no way to specify the user agent for service workers in 4.x and 5.x?
    • If so, could userAgentFallback be back-ported?
  9. MarshallOfSound commented on Aug 7, 2019

    @MarshallOfSound
    Member

    There is no way to specify the user agent for service workers in 4.x and 5.x?

    4.2.x supports app.userAgentFallback. 5.x does not. We can't backport to 5.x without a semver minor bump and we have no intention to do that at this time.

    4.x and 6.x are both stable releases so you should be able to use one of them (preferably 6)

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions