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

Best practices for feature detection of DOM API #137

Closed
alice opened this issue Sep 12, 2019 · 11 comments
Closed

Best practices for feature detection of DOM API #137

alice opened this issue Sep 12, 2019 · 11 comments
Assignees
Milestone

Comments

@alice
Copy link
Contributor

alice commented Sep 12, 2019

One example: WebXR have a feature detection method, supportsSession(), which returns a promise which rejects if the session type is not supported.

Should this return a Promise<boolean>, on the principle that feature detection shouldn't throw?

Also, what other best practices are there for feature detection?

cc @torgo

@annevk
Copy link
Member

annevk commented Sep 12, 2019

https://www.w3.org/2001/tag/doc/promises-guide/#rejections-should-be-exceptional applies here I think. In particular, this does not seem exceptional.

@torgo
Copy link
Member

torgo commented Mar 31, 2020

We have some text in section 2.3 (New Features should be Detectable) so if we have more to say on this topic it should probably be added here.

@kenchris
Copy link
Contributor

I think there is some overlap between feature detection and fingerprinting (and private browsing mode).

Of the top of my head I have heard different recommendations at different times.

Examples:

  • don't expose the objects etc when feature is not available (like no underlying hardware gyroscope for instance)
  • always expose (due to avoiding fingerprinting) but let object throw (NotAvailableError or similar) when instantiated - this way you can see whether the browser support the feature or not, but not whether the hardware does

All of this leads to different design and confuses users and spec authors. I am not sure what would be the right answer but it deserves some thought and it also requires some research on what kind of different solutions are in use today.

@anssik @slightlyoff @reillyeon @beaufortfrancois

@torgo torgo added this to the 2020-05-04-week milestone Mar 31, 2020
@torgo
Copy link
Member

torgo commented Mar 31, 2020

Suggest having a dedicated breakout session on this during the non-design-review week next month.

@kenchris kenchris changed the title Best practices for feature detection? Best practices for feature detection in JavaScript May 5, 2020
@kenchris
Copy link
Contributor

kenchris commented May 5, 2020

Things to keep in mind

  • no diff between regular mode and privacy preserving mode (incognito)
  • keeping fingerprinting to the minimum (people can know browser from UA string and features available)
  • this involves permissions

Current different ways (non-exaustive):

  • Presence of a namespace
if ('bluetooth' in navigator)
  • Presence of an object
if ('NFCReader' in window)
  • properties on objects, eg. Media Capture
const capabilities = videoTrack.getCapabilities();

if ("pan" in capabilities) { ... }

@torgo
Copy link
Member

torgo commented May 5, 2020

@triblondon do you have opinions here?

@tomayac
Copy link
Contributor

tomayac commented May 5, 2020

Not strictly feature detection, but maybe still interesting: the Shape Detection API has a BarcodeDetector.getSupportedFormats() method, since presence of the BarcodeDetector alone may not be enough if the objective is scanning “weird” barcode formats that not all platforms may support.

@scunliffe
Copy link

I wonder if there is a solution that enables developers to detect features, but also restricts/reduces fingerprinting?

e.g. browsers are smart enough now to differentiate between a user-driven-event like a mouse click vs. a simulated .click() event applied to a link/button to determine if triggered code like a window.open() call should be allowed or not. This was done to stop spammy websites from launching popup ads, but still allow "desired" popups in web applications.

As such, for many of these features... I wonder if a Just In Time (JIT) model might work where by a check to detect a feature would only be allowed if the code following it attempts to use it.

if('NFCReader' in window){ // check is allowed
  doNFCStuff();
}

if('NFCReader' in window){ // check is denied/blocked
  addToFingerprintingData();
}

Not foolproof by any means, but mixed with browser permissions by domain etc. might cover most cases.

@dbaron
Copy link
Member

dbaron commented May 28, 2020

always expose (due to avoiding fingerprinting) but let object throw (NotAvailableError or similar) when instantiated - this way you can see whether the browser support the feature or not, but not whether the hardware does

It seems like this doesn't help much with fingerprinting, since it's still exposing the information about whether the device is available or not (unless the latter response is after a permission prompt).

It's also worth noting that "implementation doesn't support X" and "implementation supports X but there aren't any devices available" are different results that pages might want to handle in different ways.

@dbaron
Copy link
Member

dbaron commented May 28, 2020

... so what I'm saying is that detection of the support for a feature in the implementation should generally be separate from detection of whether a device to do something with that feature is available. (And the latter might be after a request for user permission.)

@atanassov atanassov changed the title Best practices for feature detection in JavaScript Best practices for feature detection of DOM API May 28, 2020
dbaron added a commit to dbaron/design-principles that referenced this issue May 28, 2020
…ection.

While discussing w3ctag#137 with @kenchris and @atanassov I realized that the
existing advice in this section could be misinterpreted.  This clarifies
that advice.  (It's not a fix for that issue, though, but it is
related.)
dbaron added a commit to dbaron/design-principles that referenced this issue May 28, 2020
…ection.

While discussing w3ctag#137 with @kenchris and @atanassov I realized that the
existing advice in this section could be misinterpreted.  This clarifies
that advice.  (It's not a fix for that issue, though, but it is
related.)
cynthia pushed a commit that referenced this issue May 29, 2020
…ection (#201)

* Add paragraph about device detection to clarify advice on feature detection.

While discussing #137 with @kenchris and @atanassov I realized that the
existing advice in this section could be misinterpreted.  This clarifies
that advice.  (It's not a fix for that issue, though, but it is
related.)

* Link to section on device enumeration.
@plinss plinss modified the milestones: 2020-08-10-week, 2020-09-07 Sep 5, 2020
@torgo
Copy link
Member

torgo commented Sep 22, 2020

Discussed and agreed to close at our f2f.

@torgo torgo closed this as completed Sep 22, 2020
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

9 participants