4/30/2024
Dynamic manifest.json
If you want the users of your web application to "install" it on their devices, it is enough to fill in the manifest.json
file. It is already widely supported by the browsers and the operating systems and it is very easy to do it, so you should not skip it.
I maintain a Vue application that communicates with a SaaS system. The application is generated almost statically, without having a back-end behind it and has one entry point, but depending on the site that loads it, I save a variable in LocaleStorage that contains the identifier of this site. The application has a built-in manifest.json
, but over time I had to personalize it for each site.
The dynamic loading of the manifest.json
file should be done at the client (in the browser). We excluded Vue & Vue Router from the calculations, because the manifest must be available, even if their 'engines' do not work. So, we transferred everything to the index.html
file of the application.
I wrote a small Node console that goes through the active databases and generates a static manifest.siteId.json
files for each site in a specific folder. Then I added a small script to the index.html
file that loads this file and adds it to document.head
.
<!-- index.html -->
<script>
const siteId = localStorage.getItem("siteId");
const manifestPath = `/manifests/manifest.${siteId || ""}.json`;
fetch(manifestPath)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(() => {
const link = document.createElement('link');
link.rel = "manifest";
link.href = manifestPath;
document.head.appendChild(link);
})
.catch(() => {
const link = document.createElement('link');
link.rel = "manifest";
link.href = '/manifests/manifest.json';
document.head.appendChild(link);
});
</script>
Why fetch?
This is a very good question. When generating the manifest.siteId.json
files, there may be a situation where such a file does not exist. Since JavaScript cannot check for a file on the server - I make a fetch request for it. If the request does not work, then I load the manifest.json
file with the basic descriptions.
If you have an opinion or questions about the article, don't hesitate to share them.
Nitro, i18n and Dev Proxy
When you use Nuxt, it is normal to use Nitro as well, but sometimes this does not fit into the scenario you have been prepared for. The API requests are handled by another back-end server and in order to develop the application locally, a proxy must be set up.
Removing the trailing slash from the URLs in Nuxt
Every page on your website should have unique content. If you have a second page with the same content, the weight of the information for the search engines is divided between the two, and this greatly reduces their chances of appearing earlier in the search results.