Skip to content

Dynamic manifest file #578

Open
Open
@jopms

Description

@jopms

I have came up with a solution for changing the manifest file, but I don't think it is really appropriate.

I basically have a plugin in the vite.config to remove the href attribute from the <link rel='manifest' (..)>.

{
  name: 'remove-manifest-href',
  transformIndexHtml(html) {
    return html.replace(/<link\s+rel="manifest"\s+href="[^"]*"/, '<link rel="manifest"')
  },
    enforce: 'post'
  }
}

Then I create a manifest file and, and add the href to the <link rel='manifest'> again.

const changeManifestFile = (): void => {
  const dynamicManifestContent = {
    background_color: '#ffffff',
    description: 'Description',
    display: 'standalone',
    name: 'Name',
    scope: '/',
    short_name: 'Short name',
    start_url: '/',
    theme_color: '#ffffff'
  }

  const blob = new Blob([JSON.stringify(dynamicManifestContent)], { type: 'application/json' })

  const manifestUrl = URL.createObjectURL(blob)

  const linkElement = document.querySelector('link[rel="manifest"]')

  if (linkElement !== null) {
    linkElement.setAttribute('href', manifestUrl)
  }
}

If I just change the href it won't work, I need to have the build without it and then add it manually so I can set the manifest dynamically.

This solution works, but it does not seem right.

Is there any other way to do this?
If not, can we at least have the option to not generate the manifest.webmanifest and not have the href set in the link tag?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions