ipfs/service-worker-gateway

Improve subdomain detection logic

lidel opened this issue · 2 comments

Problem: brittle subdomain mode support detection

Right now we make request to bafkqaaa on subdomain via fetch:

https://github.com/ipfs-shipyard/service-worker-gateway/blob/289aca764da893789508c56d7f9e0caeb5711d90/src/lib/path-or-subdomain.ts#L35-L39

This is too brittle, because it will likely break once people start deployign their own instances without doing all the steps we did:

  • false-positive if server is a regular subdomain gateway (e.g.g rainbow returns HTTP 200) just fine
  • false-negative if subdomains have no CORS safelist or other headers

Solution: switch to img onload

We should have a static /1x1.png in public/ and request that asset specifically.

This solves two problems:

  • confirming subdomain response is SW payload, and nothing else (solves false-positive)
  • image loads not impacted by CORS, onload will succeed even without * CORS (solves false-negative)

This will also resolve the "test this once" todo because it will be cached and return instantly with subsequent requests.

one gotcha with the suggested fix is that this is also called from sw.ts code, and image onload won't work there, so we won't be able to do redirects from sw anymore, which will result in a little more assets rendered than we need.. but we have the redirect already working in client side code so it shouldn't break anything.