Home > CMS Tutorial > WordPress > Growing Service Workers: 5 Important Tips You Can't Miss

Growing Service Workers: 5 Important Tips You Can't Miss

PHPz
Release: 2023-09-09 19:21:09
Original
675 people have browsed it

The browser's Service Worker API allows web designers to offer visitors something they never had before: access to a website or web application even when they are offline, whether for a short or long period of time.

p>

Whether you want to make sure your visitors can still read your website while walking through a train tunnel, or you want to create an app that doesn't require an internet connection, Service Workers has the perfect solution.

As great as Service Workers are, there are some obstacles that may slow down your progress when you first start coding them - unless you realize in advance what those obstacles are. This tutorial will provide you with five basic tips for service worker development, which we hope will help you avoid these problems and save you from the associated troubleshooting headaches.

before the start

If you’re new to Service Workers, check out our beginner’s course Simple Service Workers for Offline Websites and Jeremy Keith’s book Going Offline, both available on Envato Elements.

1. Place your Service Worker script into the root directory

培养 Service Worker:不容错过的 5 个重要技巧

The earliest problems you may encounter when writing your first service worker, you could probably do what you've always done and put your script Go into a subdirectory named js or scripts. However, for service personnel, this mundane operation can cause problems.

The reason is that, by default, the scope of a Service Worker is defined by its location. what does that mean? This means that if you place a script in the /js directory, its scope is now limited to that /js directory. Therefore, it can only handle page requests from www.yoursite.com/js/ and completely ignore other requests, such as those from www e.g. .yoursite.com or www.yoursite.com/news/.

This limited scope in turn means that you won't be able to provide offline fallback for large parts of your site. In order for your service worker to handle any request from any part of your site, its scope must be all-inclusive.

Note: You can actually override the default scope of a Service Worker when registering, e.g.

navigator.serviceWorker.register('/sw.js', {
  scope: '/'
});
Copy after login

With this approach, you can still have all your scripts in a subdirectory if doing so is important to your project.

But generally speaking, the easiest way is to put your Service Worker in the root directory, thus automatically setting its scope to cover the entire site.

2. Using the Application Panel in Chrome / Chromium Development Tools

While all major browsers support Service Workers, currently Chrome or Chromium is arguably the best browser for developing them. This is thanks to the very useful application panel in the developer tools. As you go through the development process, you will virtually live in this tab:

培养 Service Worker:不容错过的 5 个重要技巧

In this tab there is a dedicated section for service workers where you can verify that your script is registered, active, and running. You can also use this tab to simulate an offline state, temporarily bypassing service workers, and manually unregistering previous scripts that are no longer needed.

3.Don’t use hard reloading

In addition to not placing scripts in subdirectories, another development habit that must be broken when composing Service Workers is using "hard reload" or "clear cache and hard reload". You've probably done this thousands of times while testing your site, use this feature to clear your cache and make sure you see an accurate reflection of your development changes. But with a Service Worker, this doesn't have the desired effect.

培养 Service Worker:不容错过的 5 个重要技巧

when You have a registered and active service person, any user using "hard Reload" will bypass it completely. You may hard reload your site, see Your code doesn't execute the way you expect and think you've made a mistake, only to later realize the script never ran the first time Place.

So the bad news is that "hard reload" and "clear cache and hard reload" are out of scope during Service Worker development, which brings us to the next question:

how Can you refresh the page correctly and test your Service Worker code? Does it change if you can't use "hard reload" or "clear cache and hard reload"?

The answer to this question lies in the following two techniques:

4.Check the "Update on reload" box

By default, when you refresh the page that the Service Worker is being tested on, you won't actually see the results of any code changes. This is because the version of the script you originally registered remains active in the browser, even after the page reloads, unless you take explicit action to update it.

So again we have a situation where you may be refreshing your page and wondering why your code changes didn't take effect unless You know the quirks of your service staff.

培养 Service Worker:不容错过的 5 个重要技巧

To ensure that the latest version of the script is always loaded, go to the Application tab and check the Update on Reload box. This ensures that every time the page is reloaded (remember, only use normal reloads, not hard reloads), the browser automatically updates the Service Worker for you.

NOTE: There is an additional option to click the Update link that appears next to the registered Service Worker, but it is usually easier to use the auto-reload method.

5.Check and manually delete cache objects

Applications We’re Going to Touch The last very handy feature of the tab is the ability to view the objects stored in the cache and delete them manually if necessary. Given that we don't want to use clearing caches and hard reloading, this feature will be an important part of the Service Worker development process.

In the left column of the Applications tab you will see an area Marked Cache Storage. If you expand this area you will be able to see Any cache objects held in storage related to the current URL.

培养 Service Worker:不容错过的 5 个重要技巧

Click on any item and you can check it content, which is very helpful for verifying the resource you want Offline services are being added to the cache correctly by your service workers.

To delete cached objects that are no longer needed, simply right-click on the object and select Delete.

培养 Service Worker:不容错过的 5 个重要技巧

Between this cache object deletion feature and the Update on reload checkbox, you can set it up to stick with normal page reloads while Still make sure you test the latest changes to your work properly.

Summarize

  • Generally speaking, place the service worker script in the root directory of the project so that the entire website is within its scope.

  • Use Chrome/Chromium's "Applications" tab when developing.
  • Do not use hard reload or clear cache and hard reload .

  • Check the Update on reload box in the Application tag to ensure that the registered Service Worker is up to date.

  • If necessary, delete cache objects manually via the Cache Storage section of the Application tab, where you can also inspect the cache object contents.

For more information about Service Workers, check out our new course Simple Service Workers for Offline Websites and Jeremy Keith’s book Going Offline (now available on Envato Elements).

The above is the detailed content of Growing Service Workers: 5 Important Tips You Can't Miss. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template