Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

361
Views
When I try to register a service worker file in react app it will give me a reference error: navigator is not defined
export default function swDev() {
    let swUrl = `${process.env.PUBLIC_URL}/sw.js`
    if('serviceWorker' in navigator) {
        navigator.serviceWorker.register(swUrl).then((res) => {
            console.warn("response", res)
        }).catch((err) => {
            console.warn("error is: ", err)
        })
    } else {
        console.log("service worker is not supported")
    } 
}

this is the following code I have also called this function in index.js file in my react app. Also created a simple sw.js file with a message that file is attached.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should as well check if window is defined. The service worker should only be initialized client side

(typeof window != 'undefined' && window.document);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!