17

I have a marketplace/web application with thousands of static single page apps.

Wish to add a Web App Manifest for each single page app in the <head> </head> tag of their corresponding stem_url (The {root}/index.html for all the urls of a given SPA).

The standard method:

<link rel="manifest" href="/manifest.json">

…does not seem like a good way to go forward because this would mean thousands of manifest.js files being dumped into the /public folder (it's a rails app!) and it would eventually make the app/assets compilation job very heavy as this number goes up.

Is there a way we could inline manifest json just like we do the style tags:

<style>
  body { // style here }
  …
</style>

An equivalent of manifest declaration:

<manifest>
 { 
   "name": "HackerWeb",
   "short_name": "HackerWeb",
   …
}
</manifest>
Marvin Danig
  • 3,738
  • 6
  • 39
  • 71

3 Answers3

29

You can inline the json by using a data:url. So instead of the standard

<link rel="manifest" href="/manifest.json">

it would be

<link rel="manifest" href='data:application/manifest+json,{ "name": "theName", "short_name": "shortName", "description": "theDescription"}' />

I wanted to inline it too and tried it just now. It works

RADXack
  • 1,032
  • 3
  • 11
  • 17
8

Improved answer

As mentioned by RADXack, this works great

<link rel="manifest" href='data:application/manifest+json,{ "name": "theName", "short_name": "shortName", "description": "theDescription"}' />

But what if you want to add more attributes like the colors or start_url?

Then on your server you could add:

const manifest = JSON.stringify({
        name: "React Doc",
        short_name: "React"
        start_url: "/",
        background_color: "#fffff",
        theme_color: "#ff00ff",
        display: "standalone",
    });

const HTML = `<!DOCTYPE html>
<html lang="en">
  <head>
   <link rel="manifest" href='data:application/manifest+json,${encodeURIComponent(manifest)}' />
   ...rest of your code`

encodeURIComponent will convert all special characters for you.

This way, you are sure that whatever the data being passed is, it'll be URL friendly

  • This worked perfect in Chrome v111 on Desktop, it was possible to install the App, icons, shortcuts and screenshots worked as expected. But it didn't worked well on Chrome Mobile v112 on Android, on debug, it's possible to see that the manifest properties are ok, but it doesn't presents the Install option, just the Add to Home Screen. I needed to keep manifest as a separated file to have the Install App option available, even events such as beforeinstallprompt just fires with a separated manifest file. – Fernando Ulisses dos Santos Apr 14 '23 at 13:17
2

The main thing to remember is that the manifest request is still just a network request.

So you can Add Query Params

/manifest.json?title=Hello&icon=.....

Or you could do:

/manifest.json?appId=1234

OR you can just use a pretty URL:

/manifest/1234

Then on your server you can return the JSON that you want.

Matt Gaunt
  • 9,434
  • 3
  • 36
  • 57
  • Just keep in mind security concerns... passing title or any option as a argument (like in the first example) to be rendered can be exploited and create issues. Prefer to pass a argument that the backend will query to get the details to be rendered, such as second and third examples. – Fernando Ulisses dos Santos Apr 14 '23 at 13:27