NJAldwin / eleventy-plugin-gen-favicons

Favicon generator plugin for eleventy
MIT License
15 stars 4 forks source link

Support for multiple manifests #9

Open kotoyama opened 10 months ago

kotoyama commented 10 months ago

Hi there! Thank you for this plugin :)

Is there a chance to add support for multiple manifests? I have this structure of my _data/meta.js:

require('dotenv').config()

module.exports = {
  env: process.env.NODE_ENV,
  url: process.env.URL || 'http://localhost:8080',
  en: {
    title: 'title',
    description: 'description',
  },
  ru: {
    title: 'тайтл',
    description: 'описание',
  },
}

And with this structure, I don't see any way to provide metadata to manifestData object to generate multiple manifests and associate them with the corresponding templates. Much appreciated for any help!

kotoyama commented 10 months ago

Came up with this solution as a workaround.

// .eleventy.js

...
eleventyConfig.addPlugin(faviconsPlugin, {
  generateManifest: false,
})
// _layouts/manifest.njk

{
  "lang": "{{ lang }}",
  "dir": "{{ dir or 'ltr' }}",
  "short_name": "{{ meta[lang].title }}",
  "name": "{{ meta[lang].title }}",
  "description": "{{ meta[lang].description }}",
  "orientation": "{{ 'any' }}",
  "scope": "{{ '/' }}",
  "start_url": "{{ '/' }}",
  "theme_color": "{{ '#090d11' }}",
  "background_color": "{{ '#090d11' }}",
  "display": "{{ 'standalone' }}",
  // copy from plugin once
  "icons": [
      { "src": "/icon-192.png", "type": "image/png", "sizes": "192x192" },
      { "src": "/icon-512.png", "type": "image/png", "sizes": "512x512" }
   ]
}
// _layouts/base.njk

...

// needs eleventy i18n plugin for locale url filter
<link rel="manifest" href="{{ "/site.webmanifest" | locale_url }}"">
// make for each lang
// example: en/manifest.njk

---
permalink: /{{ lang }}/site.webmanifest
layout: manifest
eleventyExcludeFromCollections: true
---