New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

next-offline

Package Overview
Dependencies
Maintainers
1
Versions
126
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

next-offline

Use [sw-precache](https://github.com/GoogleChrome/sw-precache) with [Next.js](https://github.com/zeit/next.js)

  • 0.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5.7K
decreased by-46.04%
Maintainers
1
Weekly downloads
 
Created
Source

Next.js + sw-precache

Use sw-precache with Next.js

Installation

npm install --save next-offline

or

yarn add next-offline

Usage

Create a next.config.js in your project

// next.config.js
const withOffline = require('next-offline')
module.exports = withOffline()

Then create a server.js

// server.js
const { createServer } = require('http')
const next = require('next')
const { join } = require('path')

const app = next({ dev: process.env.NODE_ENV !== 'production' })
const handle = app.getRequestHandler()

app.prepare()
  .then(() => {
    createServer((req, res) => {
      const parsedUrl = parse(req.url, true)
      const { pathname } = parsedUrl

      if (pathname === '/service-worker.js') {
        const filePath = join(__dirname, '.next', pathname)
        app.serveStatic(req, res, filePath)
      } else {
        handle(req, res, parsedUrl)
      }
    })
    .listen(port, () => {
      console.log(`> Ready on http://localhost:${port}`)
    })
  })

Finally you'll need to ensure you're registering the service worker in each of your pages. next-offline comes with a HOC that will wrap your component which you can see the usage below:

// pages/index.js
import withOffline from 'next-offline/hoc'

class Index extends PureComponent {
  ..

  render () {
    return (
      <h1>Im the index page!</h1>
    )
  }
}

export default withOffline(Index)

Alternatively you can register the service worker manually

// pages/index.js
export default class Index extends PureComponent {
  componentDidMount () {
    if ('serviceWorker' in navigator) {
      navigator.serviceWorker
        .register('/service-worker.js')
        .then(registration => console.info('service worker registration successful'))
        .catch(err => console.warn('service worker registration failed', err.message))
    }
  }
  ..
}

Optionally you can add your custom Next.js configuration as parameter

// next.config.js
const withOffline = require('next-offline')

module.exports = withOffline({
  webpack(config, options) {
    return config
  }
})

FAQs

Package last updated on 07 Feb 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc