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

Use img elements for testing resource existence rather than fetch #78

Merged
merged 1 commit into from
Jul 21, 2020

Conversation

whymarrh
Copy link
Contributor

Closes #77

The previous implementation of resourceExists was using the same-origin mode for fetch but that breaks when a site uses another domain for their icons—static.example.com, assets.example.com, or images.example.com, for example.

There isn't a fetch mode that works here:[1]

  • same-origin doesn't work for the reasons described
  • cors there's no guarantee that the 2nd origin in question is correctly configured for CORS—a quick check on two sites confirmed this
  • navigate isn't applicable here
  • no-cors hides the status and statusText so we can't use those

With that, this PR loads the resources as images and tests whether that works. This will essentially be a no-cors request but with the status exposed through onload+onerror, which will suffice for our use case.

@whymarrh whymarrh force-pushed the fix-icon-exists-checks-but-better branch from 4cef2bf to f353851 Compare July 21, 2020 19:59
@whymarrh
Copy link
Contributor Author

This is the approach from #75, I think this is better than #77

@whymarrh whymarrh marked this pull request as ready for review July 21, 2020 19:59
@whymarrh whymarrh requested a review from a team as a code owner July 21, 2020 19:59
Copy link
Member

@rekmarks rekmarks left a comment

Choose a reason for hiding this comment

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

I think this makes a lot of sense!

To follow up on questions in #77, I'm not positive why the same-origin restriction was added in the first place, likely out of an (over?)abundance of caution.

The whole point of resourceExists is to only send an href to the extension that actually points to something we can use. We have a similar fallback mechanism in the extension for when these links inevitably go stale, but I don't see any harm in doing some basic validation in the page so as to avoid sending complete garbage to the extension/mobile.

All of this said, let's rename resourceExists to imageExists in light of its new, more specific use?

@whymarrh whymarrh force-pushed the fix-icon-exists-checks-but-better branch from f353851 to 5c09a72 Compare July 21, 2020 20:10
The previous implementation of `resourceExists` was using the `same-origin`
mode for fetch but that breaks when a site uses another domain for their icons—
`static.example.com` or `images.example.com`, for example.

There isn't a fetch mode that works here: [1]

- `same-origin`: doesn't work for the reasons described
- `cors`: there's no guarantee that the 2nd origin in question is correctly
configured for CORS—a quick check on two sites confirmed this
- `navigate`: isn't applicable here
- `no-cors` hides the `status` and `statusText` so we can't use those

With that, this loads the resources as images and tests whether that works.
This will essentially be a `no-cors` request but with the status exposed
through `onload`+`onerror`, which will suffice for our use case.

  [1]:https://fetch.spec.whatwg.org/#concept-request-mode
@whymarrh whymarrh force-pushed the fix-icon-exists-checks-but-better branch from 5c09a72 to 00f2a39 Compare July 21, 2020 20:12
Copy link
Member

@rekmarks rekmarks left a comment

Choose a reason for hiding this comment

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

:guy_fieri_chef_kiss:

@whymarrh whymarrh merged commit 9558704 into master Jul 21, 2020
@whymarrh whymarrh deleted the fix-icon-exists-checks-but-better branch July 21, 2020 20:15
@whymarrh whymarrh mentioned this pull request Jul 21, 2020
@rekmarks rekmarks mentioned this pull request Jul 21, 2020
rekmarks added a commit that referenced this pull request Jul 21, 2020
* 6.1.0

* #72, #73, #76, #78
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants