Giter Site home page Giter Site logo

alexandertrefz / astrojs-service-worker Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tatethurston/astrojs-service-worker

0.0 0.0 0.0 594 KB

An Astro integration to generate a Service Worker. Powered by Workbox.

License: MIT License

Shell 1.02% JavaScript 40.27% TypeScript 58.70%

astrojs-service-worker's Introduction

Astro Service Worker

An Astro integration that generates a Service Worker. Powered by Workbox.

What is this? ๐Ÿง

A minimal wrapper around Workbox to quickly add a service worker to your Astro static site. Get precached pages and offline support out of the box.

Installation & Usage ๐Ÿ“ฆ

  1. Add this package to your project:

    • npm install astrojs-service-worker or yarn add astrojs-service-worker
  2. Add astrojs-service-worker to your astro.config.mjs integrations:

    import { defineConfig } from "astro/config";
    + import serviceWorker from "astrojs-service-worker";
    
    export default defineConfig({
    +  integrations: [serviceWorker()],
    });
  3. That's it! A service worker that precaches all of your build's static assets will be generated. Page navigations will be served from the service worker's cache instead of making network calls, speeding up your page views and enabling offline viewing ๐Ÿ™Œ.

Note that when running astro dev a no-op service worker is generated. Service workers interfere with hot module reloading (because they intercept the request for the updated asset), so this no-op service worker clears any existing workers for the page so hot module reloading works as expected.

Verification ๐Ÿค”

  1. To view the production service worker, run astro build && astro preview.
  2. The service worker must first install before it intercepts any traffic. You can view the status of the service worker in Chrome by opening the dev console, clicking the Application tab and then clicking the Service Workers tab.
  3. Disable your internet connection and click around your site. Your pages will be served by the service worker. This is most obvious when you are disconnected from the internet, but even when users have an internet connection your pages will be served from the service worker and not from the network -- markedly speeding up page requests.

API Overview ๐Ÿ› 

Name Description Type
registration.autoRegister

Autoregister the service worker.

If false, then the application must initialize the service worker by invoking register. Set this to false if you'd like to take control over when you service worker is initialized. You'll then need to add something like the following to your application:

if ("serviceWorker" in navigator) {
  navigator.serviceWorker.register("/service-worker.js");
}

Defaults to true. Recommended: true.

boolean | undefined
workbox Options passed to `worbox-build`. See all available configuration options [here](https://developer.chrome.com/docs/workbox/modules/workbox-build/)

Defaults to GenerateSW which will generate a service worker.

Note: injectManifest is not supported at this time. If you would like it to be supported, please open an issue

InjectManifestOptions | GenerateSWOptions

Example:

   import { defineConfig } from "astro/config";
   import serviceWorker from "astrojs-service-worker";

   export default defineConfig({
     integrations: [
       serviceWorker({
+        workbox: { inlineWorkboxRuntime: true }
       })
     ],
   });

Common Service Worker Pitfalls โš ๏ธ

You must serve your application over HTTPS in production environments. Service Workers must be served from the site's origin over HTTPS.

Some browsers special case localhost, so this may not be necessary during local development. HTTPS is not handled by this library. You can use a reverse proxy like Nginx or Caddy if you want to setup HTTPS for local development.

The service worker origin constraint means that service workers can not control pages on a different subdomain. Eg mysite.com can not be controlled by a service worker if that was served from a subdomain such as mycdn.mysite.com. To learn more about how service workers work in general, read MDN's documentation.

Production Sites Using astrojs-service-woker

My blog, tatethurston.com. You can use this site to get a sense of the capabilities enabled by this package. If you have any questions, feel free to open an issue.

Contributing ๐Ÿ‘ซ

PR's and issues welcomed! For more guidance check out CONTRIBUTING.md

Licensing ๐Ÿ“ƒ

See the project's MIT License.

astrojs-service-worker's People

Contributors

barbalex avatar ntsd avatar tatethurston avatar

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.