create-react-app use custom service worker without ejecting










3














I have an existing app I'm trying to convert to use React. I've copied its functionality with a brand new create-react-app one (using react-scripts 1.0.13).



I want to use the existing service worker I have. I've noticed CRA creates its own service worker; there is code in the webpack config (using SWPrecacheWebpackPlugin) that creates an unbundled module, service-worker.js. All other JS modules are bundled together.



From what I understand, I can't just copy my existing service worker existing-service-worker.js and try to import that, as all JS modules are bundled together.



I don't want to eject.



I've forked create-react-app in order to customise react-scripts and use different logic in the webpack config, which will allow me to use my existing service worker instead of the one it creates with SWPrecacheWebpackPlugin... However, I don't know how to do this. This is my first time using React and webpack.



Can someone point me in the right direction, and help me use my existing service worker in React, without ejecting?










share|improve this question


























    3














    I have an existing app I'm trying to convert to use React. I've copied its functionality with a brand new create-react-app one (using react-scripts 1.0.13).



    I want to use the existing service worker I have. I've noticed CRA creates its own service worker; there is code in the webpack config (using SWPrecacheWebpackPlugin) that creates an unbundled module, service-worker.js. All other JS modules are bundled together.



    From what I understand, I can't just copy my existing service worker existing-service-worker.js and try to import that, as all JS modules are bundled together.



    I don't want to eject.



    I've forked create-react-app in order to customise react-scripts and use different logic in the webpack config, which will allow me to use my existing service worker instead of the one it creates with SWPrecacheWebpackPlugin... However, I don't know how to do this. This is my first time using React and webpack.



    Can someone point me in the right direction, and help me use my existing service worker in React, without ejecting?










    share|improve this question
























      3












      3








      3







      I have an existing app I'm trying to convert to use React. I've copied its functionality with a brand new create-react-app one (using react-scripts 1.0.13).



      I want to use the existing service worker I have. I've noticed CRA creates its own service worker; there is code in the webpack config (using SWPrecacheWebpackPlugin) that creates an unbundled module, service-worker.js. All other JS modules are bundled together.



      From what I understand, I can't just copy my existing service worker existing-service-worker.js and try to import that, as all JS modules are bundled together.



      I don't want to eject.



      I've forked create-react-app in order to customise react-scripts and use different logic in the webpack config, which will allow me to use my existing service worker instead of the one it creates with SWPrecacheWebpackPlugin... However, I don't know how to do this. This is my first time using React and webpack.



      Can someone point me in the right direction, and help me use my existing service worker in React, without ejecting?










      share|improve this question













      I have an existing app I'm trying to convert to use React. I've copied its functionality with a brand new create-react-app one (using react-scripts 1.0.13).



      I want to use the existing service worker I have. I've noticed CRA creates its own service worker; there is code in the webpack config (using SWPrecacheWebpackPlugin) that creates an unbundled module, service-worker.js. All other JS modules are bundled together.



      From what I understand, I can't just copy my existing service worker existing-service-worker.js and try to import that, as all JS modules are bundled together.



      I don't want to eject.



      I've forked create-react-app in order to customise react-scripts and use different logic in the webpack config, which will allow me to use my existing service worker instead of the one it creates with SWPrecacheWebpackPlugin... However, I don't know how to do this. This is my first time using React and webpack.



      Can someone point me in the right direction, and help me use my existing service worker in React, without ejecting?







      javascript reactjs webpack service-worker






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Oct 2 '17 at 10:05









      CRA User

      161




      161






















          4 Answers
          4






          active

          oldest

          votes


















          1














          There is an npm library specifically built for this use case. Its called cra-append-sw .



          Most of the details are in the npm page, but simply put you just need to install the library (npm i --save cra-append-sw).



          Make a few changes to your package.json:



          "start": "react-scripts start && cra-append-sw --mode dev ./public/custom-sw-import.js",
          "build": "react-scripts build && cra-append-sw --skip-compile ./public/custom-sw-import.js"


          And finally create a file in your public folder called custom-sw-import.js and all of the service worker code you write there will simply be appended to cra's service worker.



          I can verify this works since I applied the same principle to make my website www.futurist-invenzium.com which gives a demo of all the features provided by PWA's.



          I also found this blogpost to be helpful if you want a more in depth answer.






          share|improve this answer






























            0














            You'll need to eject.



            (Or, create your own fork -- which I don't recommend)




            You don’t have to ever use eject. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.




            https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#npm-run-eject



            You won't be able to customize the generated service worker otherwise.



            However, if you are afraid about a permanent eject operation, you can always clone the project directory and npm run eject in the clone to play around with things. The original will remain intact.






            share|improve this answer




























              0














              I've done it without eject



              You can place your workers in the public folder, then you'd have to transpile and minify them by yourself.



              As discussed in here https://github.com/facebook/create-react-app/issues/1277






              share|improve this answer




























                0














                here is another solution without Ejecting:



                https://medium.freecodecamp.org/how-to-build-a-pwa-with-create-react-app-and-custom-service-workers-376bd1fdc6d3






                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%2f46523468%2fcreate-react-app-use-custom-service-worker-without-ejecting%23new-answer', 'question_page');

                  );

                  Post as a guest















                  Required, but never shown

























                  4 Answers
                  4






                  active

                  oldest

                  votes








                  4 Answers
                  4






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  1














                  There is an npm library specifically built for this use case. Its called cra-append-sw .



                  Most of the details are in the npm page, but simply put you just need to install the library (npm i --save cra-append-sw).



                  Make a few changes to your package.json:



                  "start": "react-scripts start && cra-append-sw --mode dev ./public/custom-sw-import.js",
                  "build": "react-scripts build && cra-append-sw --skip-compile ./public/custom-sw-import.js"


                  And finally create a file in your public folder called custom-sw-import.js and all of the service worker code you write there will simply be appended to cra's service worker.



                  I can verify this works since I applied the same principle to make my website www.futurist-invenzium.com which gives a demo of all the features provided by PWA's.



                  I also found this blogpost to be helpful if you want a more in depth answer.






                  share|improve this answer



























                    1














                    There is an npm library specifically built for this use case. Its called cra-append-sw .



                    Most of the details are in the npm page, but simply put you just need to install the library (npm i --save cra-append-sw).



                    Make a few changes to your package.json:



                    "start": "react-scripts start && cra-append-sw --mode dev ./public/custom-sw-import.js",
                    "build": "react-scripts build && cra-append-sw --skip-compile ./public/custom-sw-import.js"


                    And finally create a file in your public folder called custom-sw-import.js and all of the service worker code you write there will simply be appended to cra's service worker.



                    I can verify this works since I applied the same principle to make my website www.futurist-invenzium.com which gives a demo of all the features provided by PWA's.



                    I also found this blogpost to be helpful if you want a more in depth answer.






                    share|improve this answer

























                      1












                      1








                      1






                      There is an npm library specifically built for this use case. Its called cra-append-sw .



                      Most of the details are in the npm page, but simply put you just need to install the library (npm i --save cra-append-sw).



                      Make a few changes to your package.json:



                      "start": "react-scripts start && cra-append-sw --mode dev ./public/custom-sw-import.js",
                      "build": "react-scripts build && cra-append-sw --skip-compile ./public/custom-sw-import.js"


                      And finally create a file in your public folder called custom-sw-import.js and all of the service worker code you write there will simply be appended to cra's service worker.



                      I can verify this works since I applied the same principle to make my website www.futurist-invenzium.com which gives a demo of all the features provided by PWA's.



                      I also found this blogpost to be helpful if you want a more in depth answer.






                      share|improve this answer














                      There is an npm library specifically built for this use case. Its called cra-append-sw .



                      Most of the details are in the npm page, but simply put you just need to install the library (npm i --save cra-append-sw).



                      Make a few changes to your package.json:



                      "start": "react-scripts start && cra-append-sw --mode dev ./public/custom-sw-import.js",
                      "build": "react-scripts build && cra-append-sw --skip-compile ./public/custom-sw-import.js"


                      And finally create a file in your public folder called custom-sw-import.js and all of the service worker code you write there will simply be appended to cra's service worker.



                      I can verify this works since I applied the same principle to make my website www.futurist-invenzium.com which gives a demo of all the features provided by PWA's.



                      I also found this blogpost to be helpful if you want a more in depth answer.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 11 at 7:40









                      Billal Begueradj

                      5,631132637




                      5,631132637










                      answered Nov 11 at 7:15









                      NatSerAchilles

                      112




                      112























                          0














                          You'll need to eject.



                          (Or, create your own fork -- which I don't recommend)




                          You don’t have to ever use eject. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.




                          https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#npm-run-eject



                          You won't be able to customize the generated service worker otherwise.



                          However, if you are afraid about a permanent eject operation, you can always clone the project directory and npm run eject in the clone to play around with things. The original will remain intact.






                          share|improve this answer

























                            0














                            You'll need to eject.



                            (Or, create your own fork -- which I don't recommend)




                            You don’t have to ever use eject. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.




                            https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#npm-run-eject



                            You won't be able to customize the generated service worker otherwise.



                            However, if you are afraid about a permanent eject operation, you can always clone the project directory and npm run eject in the clone to play around with things. The original will remain intact.






                            share|improve this answer























                              0












                              0








                              0






                              You'll need to eject.



                              (Or, create your own fork -- which I don't recommend)




                              You don’t have to ever use eject. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.




                              https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#npm-run-eject



                              You won't be able to customize the generated service worker otherwise.



                              However, if you are afraid about a permanent eject operation, you can always clone the project directory and npm run eject in the clone to play around with things. The original will remain intact.






                              share|improve this answer












                              You'll need to eject.



                              (Or, create your own fork -- which I don't recommend)




                              You don’t have to ever use eject. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.




                              https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#npm-run-eject



                              You won't be able to customize the generated service worker otherwise.



                              However, if you are afraid about a permanent eject operation, you can always clone the project directory and npm run eject in the clone to play around with things. The original will remain intact.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Oct 2 '17 at 12:18









                              Raphael Rafatpanah

                              7,4701353101




                              7,4701353101





















                                  0














                                  I've done it without eject



                                  You can place your workers in the public folder, then you'd have to transpile and minify them by yourself.



                                  As discussed in here https://github.com/facebook/create-react-app/issues/1277






                                  share|improve this answer

























                                    0














                                    I've done it without eject



                                    You can place your workers in the public folder, then you'd have to transpile and minify them by yourself.



                                    As discussed in here https://github.com/facebook/create-react-app/issues/1277






                                    share|improve this answer























                                      0












                                      0








                                      0






                                      I've done it without eject



                                      You can place your workers in the public folder, then you'd have to transpile and minify them by yourself.



                                      As discussed in here https://github.com/facebook/create-react-app/issues/1277






                                      share|improve this answer












                                      I've done it without eject



                                      You can place your workers in the public folder, then you'd have to transpile and minify them by yourself.



                                      As discussed in here https://github.com/facebook/create-react-app/issues/1277







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Apr 4 at 11:08









                                      manelgarcia

                                      4891414




                                      4891414





















                                          0














                                          here is another solution without Ejecting:



                                          https://medium.freecodecamp.org/how-to-build-a-pwa-with-create-react-app-and-custom-service-workers-376bd1fdc6d3






                                          share|improve this answer

























                                            0














                                            here is another solution without Ejecting:



                                            https://medium.freecodecamp.org/how-to-build-a-pwa-with-create-react-app-and-custom-service-workers-376bd1fdc6d3






                                            share|improve this answer























                                              0












                                              0








                                              0






                                              here is another solution without Ejecting:



                                              https://medium.freecodecamp.org/how-to-build-a-pwa-with-create-react-app-and-custom-service-workers-376bd1fdc6d3






                                              share|improve this answer












                                              here is another solution without Ejecting:



                                              https://medium.freecodecamp.org/how-to-build-a-pwa-with-create-react-app-and-custom-service-workers-376bd1fdc6d3







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Sep 8 at 12:31









                                              Leon

                                              4,17211522




                                              4,17211522



























                                                  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.





                                                  Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                  Please pay close attention to the following guidance:


                                                  • 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%2f46523468%2fcreate-react-app-use-custom-service-worker-without-ejecting%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

                                                  How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                                                  Syphilis

                                                  Darth Vader #20