itgalaxy / favicons

Favicons generator for Node.js
MIT License
1.19k stars 165 forks source link

If a relative path is used for icons path, it is transformed to an absolute path #449

Open andsouto opened 7 months ago

andsouto commented 7 months ago

I am using as configuration a relative value for path property:

    const configuration = {
        path: 'assets/icons',
        // rest of options
    }

But the generated manifest.webmanifest adds an initial /. e.g.:

    {
      "src": "/assets/icons/android-chrome-36x36.png",
      "sizes": "36x36",
      "type": "image/png",
      "purpose": "any"
    },

Am I missing something or that / shouldn't be added? According to the specification if a relative path is used it is relative to the location of the webmanifest.

andy128k commented 7 months ago

path configuration is an absolute path to an icons directory. In that regard assets/icons and /assets/icons are equivalent.

andsouto commented 7 months ago

You are right, I think I was mixing things...

But the problem is the generated manifest is always generated using an absolute path and there is no way to use a relative path. And a relative path makes sense as it is interpreted as relative to the manifest.webmanifest location. If the web app is deployed in a subpath and not in the / of an origin relative paths work but absolute paths don't. And if deployed in / both work exactly the same way.

I'm fixing the problem doing this before writing the webmanifest to distk:

            if (file.name === 'manifest.webmanifest') {
                const contents = file.contents.replaceAll('/assets/icons/', 'assets/icons/');
                return fs.writeFile(`${path}/${file.name}`, contents);
            }

But I think it should be a cleaner way of doing this just by configuration.

andy128k commented 7 months ago

Things are a bit backwards here. A Manifest file is secondary to icons. Historically the library was designed around icons and whatever else (e.g. manifests) were "extra files to generate".