Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trouble in setting up next-offline and getting started #220

Open
vidishaparab opened this issue Jan 29, 2020 · 4 comments
Open

Trouble in setting up next-offline and getting started #220

vidishaparab opened this issue Jan 29, 2020 · 4 comments

Comments

@vidishaparab
Copy link

Hi,
I have just started using the next framework.I have read through the official document and also some of the chats at spectrum but I'm still unable to set up the next-offline in my project and get it started.
I'm currently just aiming at having atleast the service-worker registered in development.I'm not using "now" for deployment so as suggested have followed the "now 1" example in the documentation - the express server approach.
Here is the code for server.ts. The server.ts is inside the "server" folder in the root directory (next to "pages")

 server.get('/service-worker.js', (req, res) => {
      console.log('service worker called')
      // Don't cache service worker is a best practice (otherwise clients wont get emergency bug fix)
      res.set('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate');
      res.set('Content-Type', 'application/javascript');
      const filePath = join(__dirname, '.next', '/service-worker.js')
      app.serveStatic(req, res, filePath)
    });
 

I have created the next.config.js in the root directory. I haven't provided any workbox options yet for what I have understood is if we don't provide then the default would be taken.

const withOffline = require('next-offline')

module.exports = withOffline({
    generateInDevMode:true
})

however when I try to run the code in dev mode I get the following error

(node:6176) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be one of type string, Buffer, or URL. Received type undefined
at readFile (fs.js:296:3)

Additional question -
I have prior knowledge of service workers and workbox. In its most basic form you define a sw.js at the root level and import the workbox plugin with an injection point. You could then register the service worker explicitly in the "parent" page or the "index" page.
The next-offline doc points that the registering script is already included but I'm still facing issues understanding how this works and why we have to rely on an express server by defining a custom route.

Any help would be appreciated.

Thanks ,
VP

@Banzaci
Copy link

Banzaci commented Feb 17, 2020

Any luck? I find the documentation terrible.

@vidishaparab
Copy link
Author

@Banzaci Hej ! no , I dropped the idea to use it. I agree with the documentation part , I found it confusing.
However I was able to solve that path error with this fix .
Also as far as the service workers are concerned with next-js , this article helped shed some light on it but I haven't really implemented that yet.

@hanford
Copy link
Owner

hanford commented Feb 18, 2020

@Banzaci that feedback isn't helpful for me.

How can I improve the documentation? I'd also love a PR if you have ideas

@hanford
Copy link
Owner

hanford commented Feb 18, 2020

The generateInDevMode stuff is admittedly jank, we're likely going to improve it at some point.

#155

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants