1

I have project where there are two different js bundles served to the user. If the user is logged in bundle B is served and if the user is not logged in, user is served bundle A. I also have an index.html page where the bundle A is injected like below.

 <script
      type="text/javascript"
      src="dist/A.js"
 ></script>

Bundle A has code that is capable of making api calls to log the user in. Now, when the user logs in and gets response from the server, I want to serve bundle B to the user using the same index.html file. How can this be achieved?

My questions

  • Can this be achieved? How?
  • Even if this can be achieved, will bundle A still have effects after B is applied?
Bhuwan Adhikari
  • 879
  • 1
  • 9
  • 21
  • are you trying to append the script after the page is fully loaded? If not then I think you are gonna have to reload the whole page with a different script. – seriously Jan 17 '22 at 12:05
  • well, `index.html` page has blank body but has dependencies on jquery, and some other third party libraries. so I think appending the script after the page is loaded will not cause any issue. – Bhuwan Adhikari Jan 17 '22 at 12:19
  • why are you taking this approach though? I'm guessing when a user loads your page if they have previously logged in and has enabled 'remember me' you want them to access their private account without loging in again. If so then let's say you saved their login permission in cookies or so then can't you just check if that cookie exist then if they do send them back to your server for verification then grant them access to their account. No swapping scripts required. – seriously Jan 17 '22 at 12:25
  • see: - [cant-remove-dynamically-added-scripts](https://stackoverflow.com/questions/15986850/cant-remove-dynamically-added-scripts) and - [dynamically-load-js-inside-js](https://stackoverflow.com/questions/14521108/dynamically-load-js-inside-js) (and probably others) – kca Jan 18 '22 at 17:09

0 Answers0