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

Allow custom user-agent via --extra-headers #8756

Closed
benschwarz opened this issue May 1, 2019 · 6 comments
Closed

Allow custom user-agent via --extra-headers #8756

benschwarz opened this issue May 1, 2019 · 6 comments

Comments

@benschwarz
Copy link
Contributor

Chrome requires custom user agents to be set using Network.setUserAgentOverride rather than accepting a user-agent to be set as additional HTTP headers.

This is confusing to users and a weird quirk of the devtools API that should not be exposed to users of Lighthouse.

My proposal is that we add a check to see if there are any headers set, detect the User-agent header if present and apply it using Network.setUserAgentOverride.

As an example:

  if (extraHeaders) {
    const headerUA = extraHeaders.find(header =>
      header.name.match(new RegExp('User-Agent', 'i'))
    )

    if (headerUA) {
      logger.info(
        '[Config]: Setting custom User-Agent based on test headers',
        headerUA.value
      )
      await Network.setUserAgentOverride({ userAgent: headerUA.value })
    }
  }

💁‍♂️ No longer will people need to set user-agent as a chrome flag.

@wildlyinaccurate
Copy link
Contributor

Would this work for setting a custom UA under mobile emulation as well? AFAIK even with --chrome-flags="--user-agent='...'" the Nexus 5 emulation overrides the UA.

@benschwarz
Copy link
Contributor Author

@wildlyinaccurate I mean, it's all up to implementation… but my pitch would be to make whatever User-agent header is set to be respected.

It should override emulation, for sure. 👍

@patrickhulce
Copy link
Collaborator

It should override emulation, for sure.

I'm not 100% sold on this one. I think in order for us to use the user-agent you should have specified a different or no device emulation. Otherwise you can find yourself in unexpected and inconsistent device setups like a desktop user agent but emulating a nexus 5x...

WDYT about warning as a first step?

@patrickhulce
Copy link
Collaborator

We'll cover this as part of #10910

@paulirish
Copy link
Member

@benschwarz my plan is to expose a emulatedUserAgent prop to the config. Does that work for you instead of using --extra-headers ?

@benschwarz
Copy link
Contributor Author

@paulirish, that sounds helpful — we're still using setUserAgentOverride from devtools proto (and blocking LH on certain commands).

For brevity, those blocked methods are: Emulation.setVisibleSize, Emulation.setDeviceMetricsOverride, Network.setUserAgentOverride and Emulation.setTouchEmulationEnabled

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants