TGUA.DEV | Home | Blog | About | More | GUEST  
Reference ID: 40 | Category:
sw.js

Today we're going to show you how to create a function full-site cache with a service worker(sw.js(The sw.js for this blog)); To accomplish this we’ll use a template I have created an show you how to set it up for your website, to show this I will be creating an implementation for tomxcd’s website, his website uses PHP and runs nginx/1.22.1. His website doesn’t have many pages yet, so this should be a relatively simple setup if we follow my template.
While you should overlook the entire code of everything you put on your website–and that stays true for this, the main part you need to worry about is:

 const cacheName = 'my-page-cache-v001';//remember to increase every time you change a page
const staticAssets = [
'/',
'/url1',
'url2',
'url3'
];

First of all, as the comment says, every time you make changes to your website(Or more specifically any cached page) you need to increment the page cache version at the top.
Secondly the array directly below is what you need to change to configure the template to fit your website; In this example the root url and 3 example urls are included, to begin to figure out what pages you want to cache. In this case with derg.rest there is the following urls:
https://derg.rest/index.php or just root which is already included https://derg.rest/archive.php https://derg.rest/coming_soon.php
See? not many Urls, with that number of urls we can easily just change the config section of the service worker to:
 const cacheName = 'my-page-cache-v001';//remember to increase every time you change a page
const staticAssets = [
'/',
'/index.php',
'/archive.php',
'/coming_soon.php'
];
See? Easy, now just save that customized service worker file as sw.js in your website's home directory, and include the following code on your website:
And voila! Cached website!

Date of writing: --



Comments section
Posts are stored on there server they are sent to; Comments will not be viewable if you are connected to a different server.





Comments


Tech Geek United(ADMIN)
This post explains the entirety of my website.
(2025-07-05 14:31)