title: How can I use dynamic manifest file in a PWA?
body: <p>We have a SaaS platform and need to be able to load different manifest files depending on user settings. The user settings are fetched once the app is started and once this is done I want to load the correct manifest. However, this is not working. I have tried several different ways of doing this but I can't get the <code>beforeinstallprompt</code> event to fire.</p>
  <p>If I load a manifest in my index.html and then don't change the manifest href it seems to work fine every time.</p>
  <p>I have tried to leave the href empty on the manifest element in the index.html and then to update it later, but when I do this the <code>beforeinstallprompt</code> is not fired.</p>
  <p>In my index.html:</p>
  <pre><code>&lt;link rel=&quot;manifest&quot; href=&quot;&quot;&gt;
  </code></pre>
  <p>or</p>
  <pre><code>&lt;link rel=&quot;manifest&quot; href=&quot;/manifest.json&quot;&gt;
  </code></pre>
  <p>In my bootstrap js file to update the href to the manifest, once I know which manifest file to load:</p>
  <pre><code>var href = &quot;https://xxx.s3.eu-central-1.amazonaws.com/sites/1/manifest2.json&quot;;
  var manifestJson = {
    &quot;name&quot;: &quot;test in code&quot;,
    &quot;short_name&quot;: &quot;test in code&quot;,
    &quot;icons&quot;: [
      {
        &quot;src&quot;: &quot;https://xxx.s3.eu-central-1.amazonaws.com/sites/0/icons/android-chrome-192x192.png&quot;,
        &quot;sizes&quot;: &quot;192x192&quot;,
        &quot;type&quot;: &quot;image/png&quot;
      },
      {
        &quot;src&quot;: &quot;https://xxx.s3.eu-central-1.amazonaws.com/sites/0/icons/android-chrome-256x256.png&quot;,
        &quot;sizes&quot;: &quot;256x256&quot;,
        &quot;type&quot;: &quot;image/png&quot;
      },
      {
        &quot;src&quot;: &quot;https://xxx.s3.eu-central-1.amazonaws.com/sites/0/icons/android-chrome-512x512.png&quot;,
        &quot;sizes&quot;: &quot;512x512&quot;,
        &quot;type&quot;: &quot;image/png&quot;
      }
    ],
    &quot;start_url&quot;: &quot;http://localhost:9000/home/&quot;,
    &quot;display&quot;: &quot;standalone&quot;,
    &quot;background_color&quot;: &quot;#2B3D4B&quot;,
    &quot;theme_color&quot;: &quot;#00afec&quot;,
    &quot;prefer_related_applications&quot;: false
  };
  var stringManifest = JSON.stringify(manifestJson);
  var blob = new Blob([stringManifest], {type: 'application/json'});
  var manifestURL = URL.createObjectURL(blob);
  
  // I have tried both ways of updating the manifest below
  
  // $(&quot;head&quot;).find(&quot;link[rel='manifest']&quot;).attr(&quot;href&quot;, manifestURL);
  $(&quot;head&quot;).find(&quot;link[rel='manifest']&quot;).attr(&quot;href&quot;, href);
  
  window.addEventListener('beforeinstallprompt', function(event) {
      console.log(&quot;poa: \nxxxxxxxxxx BEFOREINSTALLPROMPT xxxxxxxxxxx\n\n&quot;);
      $(&quot;body&quot;).addClass(&quot;hypo-install-prompt&quot;);
  
      // Save the event for later use (when the user clicks an install button)
      app.cache.beforeinstallprompt = event;
  });
  
  navigator.serviceWorker.getRegistrations().then(function(registrations) {
      console.log(&quot;Unregister ServiceWorker&quot;, registrations);
      for (var i = 0; i &lt; registrations.length; i++) {
         var registration = registrations[i];
         registration.unregister();
      }
  });
  
  navigator.serviceWorker.register('/home/serviceWorker.js', { scope: '/home/' }).then(function(registration) {
     // Registration was successful
     console.log('ServiceWorker registration successful with scope: ', registration.scope);
  }, function(err) {
     console.log('ServiceWorker registration failed: ', err);
  });
  </code></pre>
  <p>I know that this is a fairly common scenario and that many people have solved it but nothing worked for me so far. Anyone got a tip?</p>