I can not register my service worker using VUE










1















I'm trying to create a PWA with VUE.



When trying to register the service worker I do it in the file main.js in the following way:



main.js



import Vue from 'vue'
import App from './App.vue'
import router from './router'

Vue.config.productionTip = false

new Vue(
router,
render: h => h(App)
).$mount('#app')

const prod = process.env.NODE_ENV === 'production';
const swLocation = '../public/sw-config.js';

if (
prod &&
('https:' === location.protocol || location.host.match(/(localhost|127.0.0.1)/)) &&
navigator.serviceWorker
)
navigator.serviceWorker.register(swLocation).catch( error => console.log)
console.log('sw register');



As you can see my service worker file is called sw-config.js that is in the public folder at the same level as the index.html.



But at the moment of executing npm run build and executing this from a local server with npm http-server I get the message by console: "sw register"
But also the following errors:



A bad HTTP response code (404) was received when fetching the script.
Failed to load resource: net::ERR_INVALID_RESPONSE



I also noticed that this error is not shown in the console the first time the application is executed but if it is reloaded it is when it starts.



I tried to fix it by cleaning all the registered service workers to verify if that was the error but it did not work, this was the code that I used in the Chrome console:



navigator.serviceWorker.getRegistrations().then(function(registrations) for(let registration of registrations) registration.unregister() )


On Chrome Developer Tools I have this:



Chrome=>Dev Tools=>Application=>Service Workers=>Update on reload (selected)



I do not know how to avoid that error in the console, could someone help me?



Thanks!



Errors on Console










share|improve this question




























    1















    I'm trying to create a PWA with VUE.



    When trying to register the service worker I do it in the file main.js in the following way:



    main.js



    import Vue from 'vue'
    import App from './App.vue'
    import router from './router'

    Vue.config.productionTip = false

    new Vue(
    router,
    render: h => h(App)
    ).$mount('#app')

    const prod = process.env.NODE_ENV === 'production';
    const swLocation = '../public/sw-config.js';

    if (
    prod &&
    ('https:' === location.protocol || location.host.match(/(localhost|127.0.0.1)/)) &&
    navigator.serviceWorker
    )
    navigator.serviceWorker.register(swLocation).catch( error => console.log)
    console.log('sw register');



    As you can see my service worker file is called sw-config.js that is in the public folder at the same level as the index.html.



    But at the moment of executing npm run build and executing this from a local server with npm http-server I get the message by console: "sw register"
    But also the following errors:



    A bad HTTP response code (404) was received when fetching the script.
    Failed to load resource: net::ERR_INVALID_RESPONSE



    I also noticed that this error is not shown in the console the first time the application is executed but if it is reloaded it is when it starts.



    I tried to fix it by cleaning all the registered service workers to verify if that was the error but it did not work, this was the code that I used in the Chrome console:



    navigator.serviceWorker.getRegistrations().then(function(registrations) for(let registration of registrations) registration.unregister() )


    On Chrome Developer Tools I have this:



    Chrome=>Dev Tools=>Application=>Service Workers=>Update on reload (selected)



    I do not know how to avoid that error in the console, could someone help me?



    Thanks!



    Errors on Console










    share|improve this question


























      1












      1








      1








      I'm trying to create a PWA with VUE.



      When trying to register the service worker I do it in the file main.js in the following way:



      main.js



      import Vue from 'vue'
      import App from './App.vue'
      import router from './router'

      Vue.config.productionTip = false

      new Vue(
      router,
      render: h => h(App)
      ).$mount('#app')

      const prod = process.env.NODE_ENV === 'production';
      const swLocation = '../public/sw-config.js';

      if (
      prod &&
      ('https:' === location.protocol || location.host.match(/(localhost|127.0.0.1)/)) &&
      navigator.serviceWorker
      )
      navigator.serviceWorker.register(swLocation).catch( error => console.log)
      console.log('sw register');



      As you can see my service worker file is called sw-config.js that is in the public folder at the same level as the index.html.



      But at the moment of executing npm run build and executing this from a local server with npm http-server I get the message by console: "sw register"
      But also the following errors:



      A bad HTTP response code (404) was received when fetching the script.
      Failed to load resource: net::ERR_INVALID_RESPONSE



      I also noticed that this error is not shown in the console the first time the application is executed but if it is reloaded it is when it starts.



      I tried to fix it by cleaning all the registered service workers to verify if that was the error but it did not work, this was the code that I used in the Chrome console:



      navigator.serviceWorker.getRegistrations().then(function(registrations) for(let registration of registrations) registration.unregister() )


      On Chrome Developer Tools I have this:



      Chrome=>Dev Tools=>Application=>Service Workers=>Update on reload (selected)



      I do not know how to avoid that error in the console, could someone help me?



      Thanks!



      Errors on Console










      share|improve this question
















      I'm trying to create a PWA with VUE.



      When trying to register the service worker I do it in the file main.js in the following way:



      main.js



      import Vue from 'vue'
      import App from './App.vue'
      import router from './router'

      Vue.config.productionTip = false

      new Vue(
      router,
      render: h => h(App)
      ).$mount('#app')

      const prod = process.env.NODE_ENV === 'production';
      const swLocation = '../public/sw-config.js';

      if (
      prod &&
      ('https:' === location.protocol || location.host.match(/(localhost|127.0.0.1)/)) &&
      navigator.serviceWorker
      )
      navigator.serviceWorker.register(swLocation).catch( error => console.log)
      console.log('sw register');



      As you can see my service worker file is called sw-config.js that is in the public folder at the same level as the index.html.



      But at the moment of executing npm run build and executing this from a local server with npm http-server I get the message by console: "sw register"
      But also the following errors:



      A bad HTTP response code (404) was received when fetching the script.
      Failed to load resource: net::ERR_INVALID_RESPONSE



      I also noticed that this error is not shown in the console the first time the application is executed but if it is reloaded it is when it starts.



      I tried to fix it by cleaning all the registered service workers to verify if that was the error but it did not work, this was the code that I used in the Chrome console:



      navigator.serviceWorker.getRegistrations().then(function(registrations) for(let registration of registrations) registration.unregister() )


      On Chrome Developer Tools I have this:



      Chrome=>Dev Tools=>Application=>Service Workers=>Update on reload (selected)



      I do not know how to avoid that error in the console, could someone help me?



      Thanks!



      Errors on Console







      vuejs2 service-worker






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 7:03







      OscarDev

















      asked Nov 16 '18 at 6:54









      OscarDevOscarDev

      10711




      10711






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Everything in public folder is in the root folder of the application after the build.



          So your url should be: const swLocation = '/sw-config.js';






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332837%2fi-can-not-register-my-service-worker-using-vue%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Everything in public folder is in the root folder of the application after the build.



            So your url should be: const swLocation = '/sw-config.js';






            share|improve this answer



























              0














              Everything in public folder is in the root folder of the application after the build.



              So your url should be: const swLocation = '/sw-config.js';






              share|improve this answer

























                0












                0








                0







                Everything in public folder is in the root folder of the application after the build.



                So your url should be: const swLocation = '/sw-config.js';






                share|improve this answer













                Everything in public folder is in the root folder of the application after the build.



                So your url should be: const swLocation = '/sw-config.js';







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 19 '18 at 10:49









                FinrodFinrod

                1,66731629




                1,66731629





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332837%2fi-can-not-register-my-service-worker-using-vue%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    27

                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                    Category:Rhetoric