Coder Social home page Coder Social logo

voxylu / gatsby-plugin-sw Goto Github PK

View Code? Open in Web Editor NEW
5.0 0.0 1.0 10 KB

Similar to gatsby-plugin-offline but you can customize the service worker (e.g. add notifications).

Home Page: https://npm.im/gatsby-plugin-sw

License: MIT License

JavaScript 100.00%
gatsby gatsby-plugin offline notifications web-push pwa sw service-worker

gatsby-plugin-sw's Introduction

gatsby-plugin-sw

Tested only on linux with gatsby v2

Add support for making a Gatsby site work offline and more resistant to bad network connections. It creates a service worker using workboxBuild.injectManifest.

If you're using this plugin with gatsby-plugin-manifest (recommended) this plugin should be listed after that plugin so the manifest file can be included in the service worker.

Install

npm install --save gatsby-plugin-sw

How to use

// In your gatsby-config.js
plugins: [
  {
    resolve: 'gatsby-plugin-sw',
    options: {
      swPath: 'src/utils/my-service-worker.js', // Default to 'src/sw.js'
    },
  },
]
// In your service worker e.g. src/sw.js

// Required stuff
importScripts(
  'https://storage.googleapis.com/workbox-cdn/releases/3.4.1/workbox-sw.js'
)

// The plugin will pass the files to cache here
workbox.precaching.precacheAndRoute([])

// Another things
self.addEventListener('push', () => {
  // ...
})

Options

For now the only option avaible is swPath and it's the location of your service worker file.

gatsby-plugin-sw's People

Contributors

voxylu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Forkers

appcoreopc

gatsby-plugin-sw's Issues

Set service worker version

How we can set the version of the service worker cache? Currently, because of cache, my website loads older versions of the content. Thus, I'd like to check for service worker version to load new content. This is my first Gatsby website, so, I am not sure how to add the version in the service worker and the contents of the sw.js are generated automatically during export.

My current sw.js contents:

importScripts(
    'https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js'
  )
  workbox.precaching.precacheAndRoute([])

P.S.: I haven't yet tested the build with the new (5.1.2) version. I just happened to find that verion now, my current live build uses the version mentioned in the docs of the plugin (3.4.1). I'll publish another build to the website, tomorrow, and will test the version update then, hopefully, shouldn't cause any problems.

Is there any way you can import some configs?

Thanks for this, really needed this plugin.

Was just wondering how can I include a list of configs that I can pass on to my custom service worker? Seems like gatsby doesn't build sw.js file so the imports won't work correctly.

Lots of 404 errors in conole

I'm seeing a lot of 404 errors in browser console on the Netlify deployed website. The path to the requested resource is domain/undefined. Chrome mentions the cause of the error as gatsby-browser.js:39, clicking on which, I get this part of code fetch(jsUrl) which comes here:

  // Loop over all resources and fetch the page component and JSON
  // to add it to the sw cache.
  prefetchedPathnames.forEach(path => {
    const { jsUrl, dataUrl } = getResourceURLsForPathname(path)
    fetch(jsUrl)
    fetch(dataUrl)
  })

I've also attached a screenshot from the network tab.

image

Can this be fixed?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.