Bobby Powers
Bobby Powers

Reputation: 3013

Identify request's page origin in ServiceWorker

In a ServiceWorker's onfetch handler I want to be able to identify what page a Request is coming from. For example, if I have 2 pages that both request /resource.js, and this request is passed to the service worker (the onfetch handler is called with a FetchEvent), how do I tell if it is one instance of a page requesting /resource.js twice, or 2 different pages requesting /resource.js?

Is this possible?

Upvotes: 1

Views: 270

Answers (2)

Jeff Posnick
Jeff Posnick

Reputation: 56064

It's not currently possible. The original poster seems to have already found and commented on https://github.com/slightlyoff/ServiceWorker/issues/723, but for the record, that's where it's currently being discussed in the service worker specification.

Once there's some agreement on the specification, you should expect browsers that support service workers to add in the functionality over time.

Upvotes: 2

pirxpilot
pirxpilot

Reputation: 1647

You might be able to use event.request.referrer to identify the page from which request originates. See: https://developer.mozilla.org/en-US/docs/Web/API/Request/referrer

Upvotes: 0

Related Questions