Skip to content

Instantly share code, notes, and snippets.

@kosamari
Last active March 8, 2026 20:01
Show Gist options
  • Select an option

  • Save kosamari/7c5d1e8449b2fbc97d372675f16b566e to your computer and use it in GitHub Desktop.

Select an option

Save kosamari/7c5d1e8449b2fbc97d372675f16b566e to your computer and use it in GitHub Desktop.

Revisions

  1. kosamari revised this gist Mar 16, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion _ServiceWorker_for_github_pages.md
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ Make sure your github pages have HTTPS enforced, you can check `Settings > GitHu
    Persoanl github pages are all hosted under one root domain (`https://[username].github.io/`). This caused some stumblings...

    - When deleting outdated caches for the app, `caches.keys()` returns all the caches under the domain. We need to filter and delete only caches associated with the app.
    - Path for ServiceWorker shoudld be specified in absolute path
    - Path for ServiceWorker should be specified in absolute path
    - the last '/' in URL is important. See following section "When is SwerviceWorker summoned"

    ## When is ServiceWorker summoned ?
  2. kosamari revised this gist Nov 22, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion _ServiceWorker_for_github_pages.md
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ Persoanl github pages are all hosted under one root domain (`https://[username].
    - Path for ServiceWorker shoudld be specified in absolute path
    - the last '/' in URL is important. See following section "When is SwerviceWorker summoned"

    ## When is SwerviceWorker summoned ?
    ## When is ServiceWorker summoned ?
    In this example ServiceWorker is present to every page under `https://{guthub_username}.github.io/{repository}/`, but not `https://{guthub_username}.github.io/{repository}`. Notice any difference? that's right, __the trailing__ `/`

    Assuming you have `index.html` at the root of your repository, a visitor may access `/{repository}` or `/{repository}/` to get same `index.html`. This is bad practice in general and one should redirect to the other (or that's what [Jake said](https://twitter.com/jaffathecake/status/800966521015205888)), but most importantly when a visitor is seeing `index.html` from `/{repository}` (no '/' at the end), ServiceWorker __will not__ be present on the page.
  3. kosamari revised this gist Nov 22, 2016. 3 changed files with 14 additions and 3 deletions.
    10 changes: 10 additions & 0 deletions _ServiceWorker_for_github_pages.md
    Original file line number Diff line number Diff line change
    @@ -12,3 +12,13 @@ Persoanl github pages are all hosted under one root domain (`https://[username].

    - When deleting outdated caches for the app, `caches.keys()` returns all the caches under the domain. We need to filter and delete only caches associated with the app.
    - Path for ServiceWorker shoudld be specified in absolute path
    - the last '/' in URL is important. See following section "When is SwerviceWorker summoned"

    ## When is SwerviceWorker summoned ?
    In this example ServiceWorker is present to every page under `https://{guthub_username}.github.io/{repository}/`, but not `https://{guthub_username}.github.io/{repository}`. Notice any difference? that's right, __the trailing__ `/`

    Assuming you have `index.html` at the root of your repository, a visitor may access `/{repository}` or `/{repository}/` to get same `index.html`. This is bad practice in general and one should redirect to the other (or that's what [Jake said](https://twitter.com/jaffathecake/status/800966521015205888)), but most importantly when a visitor is seeing `index.html` from `/{repository}` (no '/' at the end), ServiceWorker __will not__ be present on the page.

    Just in case your server doesn't handle trailing `/`, I have `<link rel="canonical" href="https://{guthub_username}.github.io/{repository}/" />` in my index.html header *1.

    *1 I mean ... I don't know why I had `link rel="canonical"` in my code. I do remember having issue with trailing `/` thought ! So it's the only reason I can think of me wanting to use unfamiliar thing like canonical ¯\_(ツ)_
    3 changes: 2 additions & 1 deletion index.html
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@
    <html>
    <head>
    <title>Title</title>
    <link rel="canonical" href="https://{guthub_username}.github.io/{repository}/" />
    <style type="text/css">
    /* --- application CSS here --- */
    *{
    @@ -15,7 +16,7 @@ <h1>Content</h1>
    <script>
    // register ServiceWorker, remember to use absolute path!
    if (navigator.serviceWorker) {
    navigator.serviceWorker.register('/[repository]/sw.js', {scope: '/[repository]/'})
    navigator.serviceWorker.register('/{repository}/sw.js', {scope: '/{repository}/'})
    }

    // --- Application code here ---
    4 changes: 2 additions & 2 deletions sw.js
    Original file line number Diff line number Diff line change
    @@ -2,8 +2,8 @@ var APP_PREFIX = 'ApplicationName_' // Identifier for this app (this needs t
    var VERSION = 'version_01' // Version of the off-line cache (change this value everytime you want to update cache)
    var CACHE_NAME = APP_PREFIX + VERSION
    var URLS = [ // Add URL you want to cache in this list.
    '/[repository]/', // If you have separate JS/CSS files,
    '/[repository]/index.html' // add path to those files here
    '/{repository}/', // If you have separate JS/CSS files,
    '/{repository}/index.html' // add path to those files here
    ]

    // Respond with cached resources
  4. kosamari revised this gist Jun 21, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion sw.js
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@ self.addEventListener('fetch', function (e) {
    return fetch(e.request)
    }

    // You can omit if/else for console.log & put one line below here too.
    // You can omit if/else for console.log & put one line below like this too.
    // return request || fetch(e.request)
    })
    )
  5. kosamari revised this gist Jun 21, 2016. 1 changed file with 14 additions and 7 deletions.
    21 changes: 14 additions & 7 deletions sw.js
    Original file line number Diff line number Diff line change
    @@ -9,13 +9,20 @@ var URLS = [ // Add URL you want to cache in this lis
    // Respond with cached resources
    self.addEventListener('fetch', function (e) {
    console.log('fetch request : ' + e.request.url)
    var url = new URL(e.request.url)
    if (URLS.indexOf(url.pathname) === -1) {
    console.log('file is not cached : ' + e.request.url)
    return
    }
    console.log('responding with cache : ' + e.request.url)
    e.respondWith(caches.match(e.request))
    e.respondWith(
    caches.match(e.request).then(function (request) {
    if (request) { // if cache is available, respond with cache
    console.log('responding with cache : ' + e.request.url)
    return request
    } else { // if there are no cache, try fetching request
    console.log('file is not cached, fetching : ' + e.request.url)
    return fetch(e.request)
    }

    // You can omit if/else for console.log & put one line below here too.
    // return request || fetch(e.request)
    })
    )
    })

    // Cache resources
  6. kosamari revised this gist Jun 21, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions sw.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    var APP_PREFIX = 'ApplicationName_' // Identifier for this app (this needs to be consistent)
    var VERSION = 'version_01' // Version of the off-line cache (change this everytime you want to update cache)
    var APP_PREFIX = 'ApplicationName_' // Identifier for this app (this needs to be consistent across every cache update)
    var VERSION = 'version_01' // Version of the off-line cache (change this value everytime you want to update cache)
    var CACHE_NAME = APP_PREFIX + VERSION
    var URLS = [ // Add URL you want to cache in this list.
    '/[repository]/', // If you have separate JS/CSS files,
  7. kosamari revised this gist Jun 21, 2016. 2 changed files with 3 additions and 3 deletions.
    2 changes: 1 addition & 1 deletion _ServiceWorker_for_github_pages.md
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ This is a ServiceWorker template to turn small github pages into offline ready a

    ## Why ?
    Whenever I make small tools & toys, I create github repo and make a demo page using github pages (like [this one](https://kosamari.github.io/translucent/)).
    Often these "apps" are just an `index.html` file with all the nessesary CSS and JavaScript in it. I wanted to cache this html file so that I can access my tools offline as well.
    Often these "apps" are just an `index.html` file with all the nessesary CSS and JavaScript in it (or maybe 2-3 `html/css/js` files). I wanted to cache these files so that I can access my tools offline as well.

    ## Notes
    Make sure your github pages have HTTPS enforced, you can check `Settings > GitHub Pages > Enforce HTTPS` of your repository.
    4 changes: 2 additions & 2 deletions sw.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    var APP_PREFIX = 'ApplicationName_' // Identifier for this app
    var VERSION = 'version_01' // Version of the off-line cache
    var APP_PREFIX = 'ApplicationName_' // Identifier for this app (this needs to be consistent)
    var VERSION = 'version_01' // Version of the off-line cache (change this everytime you want to update cache)
    var CACHE_NAME = APP_PREFIX + VERSION
    var URLS = [ // Add URL you want to cache in this list.
    '/[repository]/', // If you have separate JS/CSS files,
  8. kosamari revised this gist Jun 21, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion _ServiceWorker_for_github_pages.md
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ Often these "apps" are just an `index.html` file with all the nessesary CSS and
    ## Notes
    Make sure your github pages have HTTPS enforced, you can check `Settings > GitHub Pages > Enforce HTTPS` of your repository.

    Persoanl github pages are hosted under one root domain (`https://[username].github.io/[repository]`). This caused some stumblings...
    Persoanl github pages are all hosted under one root domain (`https://[username].github.io/`). This caused some stumblings...

    - When deleting outdated caches for the app, `caches.keys()` returns all the caches under the domain. We need to filter and delete only caches associated with the app.
    - Path for ServiceWorker shoudld be specified in absolute path
  9. kosamari revised this gist Jun 21, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions sw.js
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@ var APP_PREFIX = 'ApplicationName_' // Identifier for this app
    var VERSION = 'version_01' // Version of the off-line cache
    var CACHE_NAME = APP_PREFIX + VERSION
    var URLS = [ // Add URL you want to cache in this list.
    '/[repository]/', // If you have external JS/CSS files,
    '/[repository]/', // If you have separate JS/CSS files,
    '/[repository]/index.html' // add path to those files here
    ]

    @@ -31,7 +31,7 @@ self.addEventListener('install', function (e) {
    // Delete outdated caches
    self.addEventListener('activate', function (e) {
    e.waitUntil(
    caches.keys().then(function(keyList) {
    caches.keys().then(function (keyList) {
    // `keyList` contains all cache names under your username.github.io
    // filter out ones that has this app prefix to create white list
    var cacheWhitelist = keyList.filter(function (key) {
  10. kosamari renamed this gist Jun 21, 2016. 1 changed file with 0 additions and 0 deletions.
  11. kosamari revised this gist Jun 21, 2016. 2 changed files with 5 additions and 5 deletions.
    4 changes: 2 additions & 2 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    <head>
    <title>Title</title>
    <style type="text/css">
    /* CSS goes here*/
    /* --- application CSS here --- */
    *{
    background-color: #F5F4F0;
    font-family: Georgia, serif;
    @@ -18,7 +18,7 @@ <h1>Content</h1>
    navigator.serviceWorker.register('/[repository]/sw.js', {scope: '/[repository]/'})
    }

    // --- Application code goes here ---
    // --- Application code here ---
    </script>
    </body>
    </html>
    6 changes: 3 additions & 3 deletions sw.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    var APP_PREFIX = 'ApplicationName_' // Identifier for this app
    var VERSION = 'version_01' // Version of the off-line cache
    var APP_PREFIX = 'ApplicationName_' // Identifier for this app
    var VERSION = 'version_01' // Version of the off-line cache
    var CACHE_NAME = APP_PREFIX + VERSION
    var URLS = [ // Add URL you want to cache in this list.
    var URLS = [ // Add URL you want to cache in this list.
    '/[repository]/', // If you have external JS/CSS files,
    '/[repository]/index.html' // add path to those files here
    ]
  12. kosamari renamed this gist Jun 21, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  13. kosamari revised this gist Jun 21, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -6,9 +6,9 @@ Whenever I make small tools & toys, I create github repo and make a demo page us
    Often these "apps" are just an `index.html` file with all the nessesary CSS and JavaScript in it. I wanted to cache this html file so that I can access my tools offline as well.

    ## Notes
    Make sure your github pages has HTTPS enforced, you can check `Settings > GitHub Pages > Enforce HTTPS` of your repository.
    Make sure your github pages have HTTPS enforced, you can check `Settings > GitHub Pages > Enforce HTTPS` of your repository.

    Persoanl github pages are hosted under one root domain (i.e `https://kosamari.github.io/[repository]`). This caused some stumblings...
    Persoanl github pages are hosted under one root domain (`https://[username].github.io/[repository]`). This caused some stumblings...

    - When deleting outdated caches for the app, `caches.keys()` returns all the caches under the domain. We need to filter and delete only caches associated with the app.
    - Path for ServiceWorker shoudld be specified in absolute path
  14. kosamari revised this gist Jun 21, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    This is a ServiceWorker template to turn small github pages into offline ready app.

    ## Why ?
    Whenever I make small tools & toys, I create github repo and make a demo page using github pages. (like [this one](https://kosamari.github.io/translucent/))
    Whenever I make small tools & toys, I create github repo and make a demo page using github pages (like [this one](https://kosamari.github.io/translucent/)).
    Often these "apps" are just an `index.html` file with all the nessesary CSS and JavaScript in it. I wanted to cache this html file so that I can access my tools offline as well.

    ## Notes
  15. kosamari revised this gist Jun 21, 2016. 3 changed files with 4 additions and 4 deletions.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ Often these "apps" are just an `index.html` file with all the nessesary CSS and
    ## Notes
    Make sure your github pages has HTTPS enforced, you can check `Settings > GitHub Pages > Enforce HTTPS` of your repository.

    Persoanl github pages are hosted under one root domain (i.e `https://kosamari.github.io/[name of repo]`). This caused some stumblings...
    Persoanl github pages are hosted under one root domain (i.e `https://kosamari.github.io/[repository]`). This caused some stumblings...

    - When deleting outdated caches for the app, `caches.keys()` returns all the caches under the domain. We need to filter and delete only caches associated with the app.
    - Path for ServiceWorker shoudld be specified in absolute path
    2 changes: 1 addition & 1 deletion index.html
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ <h1>Content</h1>
    <script>
    // register ServiceWorker, remember to use absolute path!
    if (navigator.serviceWorker) {
    navigator.serviceWorker.register('/[repo name]/sw.js', {scope: '/[repo name]/'})
    navigator.serviceWorker.register('/[repository]/sw.js', {scope: '/[repository]/'})
    }

    // --- Application code goes here ---
    4 changes: 2 additions & 2 deletions sw.js
    Original file line number Diff line number Diff line change
    @@ -2,8 +2,8 @@ var APP_PREFIX = 'ApplicationName_' // Identifier for this app
    var VERSION = 'version_01' // Version of the off-line cache
    var CACHE_NAME = APP_PREFIX + VERSION
    var URLS = [ // Add URL you want to cache in this list.
    '/translucent/', // If you have external JS/CSS files,
    '/translucent/index.html' // add path to those files here
    '/[repository]/', // If you have external JS/CSS files,
    '/[repository]/index.html' // add path to those files here
    ]

    // Respond with cached resources
  16. kosamari revised this gist Jun 21, 2016. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,13 @@
    # ServiceWorker for github pages
    This is a ServiceWorker template to turn small github pages into offline ready app.

    ## why ?
    Whenever I make make simple tools and toys, I create github repo and make a demo page using github pages. (like [this](https://kosamari.github.io/translucent/))
    Often these "app" are single `index.html` with all the nessesary CSS and JavaScript in it. I wanted to cache these html files so that I can access my tools offline as well.
    ## Why ?
    Whenever I make small tools & toys, I create github repo and make a demo page using github pages. (like [this one](https://kosamari.github.io/translucent/))
    Often these "apps" are just an `index.html` file with all the nessesary CSS and JavaScript in it. I wanted to cache this html file so that I can access my tools offline as well.

    ## Notes
    Make sure your github pages has HTTPS enforced, you can check `Settings > GitHub Pages > Enforce HTTPS` of your repository.

    Persoanl github pages are hosted under one root domain (i.e `https://kosamari.github.io/[name of repo]`). This caused some stumblings...

    - When deleting outdated caches for the app, `caches.keys()` returns all the caches under the domain. We need to filter and delete only caches associated with the app.
  17. kosamari revised this gist Jun 21, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # ServiceWorker for guthub pages
    # ServiceWorker for github pages
    This is a ServiceWorker template to turn small github pages into offline ready app.

    ## why ?
  18. kosamari revised this gist Jun 21, 2016. 3 changed files with 84 additions and 4 deletions.
    13 changes: 9 additions & 4 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,12 @@
    # ServiceWorker for guthub pages
    ======
    This is a template to turn small github pages into offline ready app.
    This is a ServiceWorker template to turn small github pages into offline ready app.

    ## why ?
    When ever I make make simple tools and toys, I create github repo and make demo page using github pages. (like [this](https://kosamari.github.io/translucent/))
    Often these "app" are single `index.html` file with all the CSS and JS nessesary in it. Something I would like to access offline
    Whenever I make make simple tools and toys, I create github repo and make a demo page using github pages. (like [this](https://kosamari.github.io/translucent/))
    Often these "app" are single `index.html` with all the nessesary CSS and JavaScript in it. I wanted to cache these html files so that I can access my tools offline as well.

    ## Notes
    Persoanl github pages are hosted under one root domain (i.e `https://kosamari.github.io/[name of repo]`). This caused some stumblings...

    - When deleting outdated caches for the app, `caches.keys()` returns all the caches under the domain. We need to filter and delete only caches associated with the app.
    - Path for ServiceWorker shoudld be specified in absolute path
    24 changes: 24 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    <!DOCTYPE html>
    <html>
    <head>
    <title>Title</title>
    <style type="text/css">
    /* CSS goes here*/
    *{
    background-color: #F5F4F0;
    font-family: Georgia, serif;
    }
    </style>
    </head>
    <body>
    <h1>Content</h1>
    <script>
    // register ServiceWorker, remember to use absolute path!
    if (navigator.serviceWorker) {
    navigator.serviceWorker.register('/[repo name]/sw.js', {scope: '/[repo name]/'})
    }

    // --- Application code goes here ---
    </script>
    </body>
    </html>
    51 changes: 51 additions & 0 deletions sw.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    var APP_PREFIX = 'ApplicationName_' // Identifier for this app
    var VERSION = 'version_01' // Version of the off-line cache
    var CACHE_NAME = APP_PREFIX + VERSION
    var URLS = [ // Add URL you want to cache in this list.
    '/translucent/', // If you have external JS/CSS files,
    '/translucent/index.html' // add path to those files here
    ]

    // Respond with cached resources
    self.addEventListener('fetch', function (e) {
    console.log('fetch request : ' + e.request.url)
    var url = new URL(e.request.url)
    if (URLS.indexOf(url.pathname) === -1) {
    console.log('file is not cached : ' + e.request.url)
    return
    }
    console.log('responding with cache : ' + e.request.url)
    e.respondWith(caches.match(e.request))
    })

    // Cache resources
    self.addEventListener('install', function (e) {
    e.waitUntil(
    caches.open(CACHE_NAME).then(function (cache) {
    console.log('installing cache : ' + CACHE_NAME)
    return cache.addAll(URLS)
    })
    )
    })

    // Delete outdated caches
    self.addEventListener('activate', function (e) {
    e.waitUntil(
    caches.keys().then(function(keyList) {
    // `keyList` contains all cache names under your username.github.io
    // filter out ones that has this app prefix to create white list
    var cacheWhitelist = keyList.filter(function (key) {
    return key.indexOf(APP_PREFIX)
    })
    // add current cache name to white list
    cacheWhitelist.push(CACHE_NAME)

    return Promise.all(keyList.map(function (key, i) {
    if (cacheWhitelist.indexOf(key) === -1) {
    console.log('deleting cache : ' + keyList[i] )
    return caches.delete(keyList[i])
    }
    }))
    })
    )
    })
  19. kosamari created this gist Jun 21, 2016.
    7 changes: 7 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    # ServiceWorker for guthub pages
    ======
    This is a template to turn small github pages into offline ready app.

    ## why ?
    When ever I make make simple tools and toys, I create github repo and make demo page using github pages. (like [this](https://kosamari.github.io/translucent/))
    Often these "app" are single `index.html` file with all the CSS and JS nessesary in it. Something I would like to access offline