zackargyle / service-workers

A collection of utilities for creating/testing/experimenting with service workers.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cache Requests URLs are not full URLs

gauntface opened this issue · comments

I just noticed that in testing in a browser a Request.url value is a complete URL (i.e. includes origin).

In testing with the mocks, the URLs don't include this information. Would be good to have some fake notion of ensuring Request URLs are parsed as new URL(inputUrl, location).toString().

What do you think?

This seems like a smart change. The closer we get to the actual environment the better for tests. I will try to look into this next week if I get some time.

Changing location to be an instance of URL seems like it would get us pretty far. Node 7+ supports the full WHATWG URL API in its built-in url module: https://nodejs.org/api/url.html#url_the_whatwg_url_api

It looks like the WorkerLocation object is effectively a read-only subset of URL: https://developer.mozilla.org/en-US/docs/Web/API/WorkerLocation (Maybe not exactly, but close enough for testing purposes?)

I think it would be worth writing our own URL mock, since assuming Node 7+ would be rather limiting. We can use url.parse in the mock which will simplify it and is supported from very low versions of Node. I'll take a look at this tomorrow!

I've had good luck using https://www.npmjs.com/package/dom-urls when I needed something similar in older versions of node for sw-precache.

This should be fixed now. Will bump the version after addressing a few other issues.