Force Service Worker to only return cached responses when specific page is open

194
April 26, 2018, at 11:53 AM

I have built a portal which provides access to several features, including trouble ticket functionality.

The client has asked me to make trouble ticket functionality available offline. They want to be able to "check out" specific existing tickets while online, which are then accessible (view/edit) while the user's device is out-of-range of any internet connection. Also, they want the ability to create new tickets while offline. Then, when the connection is available, they will check in the changed/newly created tickets.

I have been tinkering with Service Workers and reviewing some good documentation on them, and I feel I have a basic understanding of how to cache the data.

However, since I only want to make the Ticketing portion of the portal available offline, I don't want the service worker caching or returning cached data when any other page of the portal is being accessed. All pages are in the same directory, so the service worker, once loaded, would by default intercept all requests from all pages in the portal.

How can I set up the service worker to only respond with cached data when the Tickets page is open?

Do I have to manually check the window.location value when fetch events occur? For example,

if (window.location == 'https://www.myurl.com/tickets')
{
    // try to get the request from network. If successful, cache the result.
    // If not successful, try returning the request from the cache.
}
else
{
    // only try the network, and don't cache the result.
}

There are many supporting files that need to be loaded for the page (i.e. css files, js files, etc.) so it's not enough to simply check the request.url for the page name. Will 'window.location' be accessible in the service worker event, and is this a reasonable way to accomplish this?

Rent Charter Buses Company
READ ALSO
Is there any library/npm package/monitoring tool to monitor live updates/insert/update happening on Cassandra DB?

Is there any library/npm package/monitoring tool to monitor live updates/insert/update happening on Cassandra DB?

I want to monitor live updates (insert/update operations) happening on Cassandra DB so that I can use socketio to push real time updates to front end

142
How to enter text in CK Editor?

How to enter text in CK Editor?

I'm having this weird issue and can't seem to figure it outBasically I only want to allow user to enter up to 40 characters in CK Editor, and if user enters more than 40 characters simply stop them

226
Storing return value in variable isn't giving me the same thing as calling the function by name

Storing return value in variable isn't giving me the same thing as calling the function by name

I'm working on a memory game using only vanilla javascript and the function below is supposed to store all open cards in an Nodelist, that I want to turn into an array laterWhen I call the function, using selected(); I get all of the cards I clicked on, but when I use the variable...

162
Need Javascript or PHP Regex to Convert Relative URLs in a String to Absolute URLs [on hold]

Need Javascript or PHP Regex to Convert Relative URLs in a String to Absolute URLs [on hold]

I have a string php variable $html which has many URLs some absolute, some notThe string variable $html in php contains the html below

242