Giter VIP home page Giter VIP logo

next-workbox-webpack-plugin's Introduction

next-workbox-webpack-plugin

Webpack plugin with workbox, it helps you build a Progressive Web App powered by Next.js. Generating service worker scripts and precache manifest of Next.js's pages and chunks.

Install

$ npm install -d next-workbox-webpack-plugin

Usage

const nextWorkboxWebpackPlugin = require('next-workbox-webpack-plugin');

nextWorkboxWebpackPlugin({
  // must, see next.config.js below
  buildId,
  // optional, next.js dist path as compiling. most of cases you don't need to fix it.
  distDir: '.next',
  // optional, which version of workbox will be used in between 'local' or 'cdn'. 'local'
  // option will help you use copy of workbox libs in localhost.
  importWorkboxFrom: 'local',
  // optional ,whether make a precache manifest of pages and chunks of Next.js app or not.
  precacheManifest: true,
  // optional, whether delete workbox path generated by the plugin.
  removeDir: true,
  // optional, path for generating sw files in build, `./static/workbox` is default
  swDestRoot: './static/',
  // optional, path for serving sw files in build, `./static/workbox` is default
  swURLRoot: '/static'
  // optional, you can use workbox-build options. except swDest because of output location is fixed in 'static/workbox',
  ...WorkboxBuildOptions,
});

Visit workbox-build page for more information.

Usage in next.config.js

const NextWorkboxWebpackPlugin = require('next-workbox-webpack-plugin');

module.exports = {
  webpack: (config, {isServer, dev, buildId, config: {distDir}}) => {
    if (!isServer && !dev) {
      config.plugins.push(new NextWorkboxWebpackPlugin({
        distDir,
        buildId
      }))
    }

    return config
  }
}

How it works

Custom Server

static/workbox
├── next-precache-manifest-d42167a04499e1887dad3156b93e064d.js
├── sw.js
└── workbox-v3.0.0-beta.0
    ├── workbox-background-sync.dev.js
    ├── ...
    ├── workbox-sw.js

Now 2.0

To use this plugin on now 2.0, you should have more work below

  1. Using fixed build id with generateBuildId. It will be used at path of sw asserts
  2. Set fixed path for sw assets. one is swDestRoot, another is swURLRoot. For service sw.js and manifest on now 2.0, we have to put those of files under .next as a part of app
const NextWorkboxWebpackPlugin = require('next-workbox-webpack-plugin');

module.exports = {
	webpack: (config, { isServer, dev, buildId, config: { distDir } }) => {
		if (!isServer && !dev) {
			config.plugins.push(
        new NextWorkboxWebpackPlugin({
          importWorkboxFrom: 'cdn',
          distDir,
          buildId,
          // destination root for sw assets, sw.js
          swDestRoot: '.next/static/my-build-id/pages',
          // root url for sw.js
          swURLRoot: '_next/static/my-build-id/pages'
        })
      );
    }
		return config;
  },
  generateBuildId: async () => {
    return 'my-build-id'
  }
};
  1. Update your routes for sw.js
{
	"version": 2,
	"routes": [{ "src": "/sw.js", "dest": "_next/static/my-build-id/pages/sw.js" }],
	"builds": [{ "src": "next.config.js", "use": "@now/next" }]
}
  1. type now, and congrats!

Examples

  • Hello PWA: You can learn how to use the webpack plugin basically
  • HNPWA: Simple HNPWA apps with Next.js

License

MIT © Jimmy Moon

next-workbox-webpack-plugin's People

Contributors

megoos avatar ooade avatar ragingwind avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

next-workbox-webpack-plugin's Issues

compiler.plugin is not a function , with Next.js 12 which use Webpack 5 by default

> Build error occurred
TypeError: compiler.plugin is not a function
    at NextWorkboxWebpackPlugin.apply (D:\Work\Edify\Frontend\edifytalent.presentation.web\node_modules\next-workbox-webpack-plugin\index.js:153:14)
    at createCompiler (D:\Work\Edify\Frontend\edifytalent.presentation.web\node_modules\next\dist\compiled\webpack\bundle5.js:133259:12)
    at create (D:\Work\Edify\Frontend\edifytalent.presentation.web\node_modules\next\dist\compiled\webpack\bundle5.js:133320:16)
    at webpack (D:\Work\Edify\Frontend\edifytalent.presentation.web\node_modules\next\dist\compiled\webpack\bundle5.js:133344:32)
    at Object.f [as webpack] (D:\Work\Edify\Frontend\edifytalent.presentation.web\node_modules\next\dist\compiled\webpack\bundle5.js:88032:16)
    at D:\Work\Edify\Frontend\edifytalent.presentation.web\node_modules\next\dist\build\compiler.js:31:40
    at new Promise (<anonymous>)
    at Object.runCompiler (D:\Work\Edify\Frontend\edifytalent.presentation.web\node_modules\next\dist\build\compiler.js:30:12)
    at D:\Work\Edify\Frontend\edifytalent.presentation.web\node_modules\next\dist\build\index.js:369:59
    at Span.traceAsyncFn (D:\Work\Edify\Frontend\edifytalent.presentation.web\node_modules\next\dist\trace\trace.js:79:26)
error Command failed with exit code 1.

Next.js 12 uses webpack5 completely and does not support webpack 4. next-workbox-webpack-plugin uses compiler.plugin it the apply function in index.js . compiler.plugin is removed from webpack 5, should use compiler.hooks.done.tap instead.

use compiler.hooks.done.tap as mentioned here.

Not generate workbox.setConfig({modulePathPrefix

Not generate

workbox.setConfig({modulePathPrefix: ...

in sw.js when importWorkboxFrom: "local"

new NextWorkboxWebpackPlugin({ importWorkboxFrom: 'local', distDir, buildId, swDestRoot: './static/workbox', swURLRoot: '/static/workbox' })

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.