npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

astro-webmanifest

v1.0.0

Published

Generate a webmanifest and icons for Astro to make progressive web apps

Downloads

4,376

Readme

Help Ukraine now!

astro-webmanifest

This Astro integration generates a web application manifest, favicon, icons and inserts appropriate html into <head> section for your Astro project during build.

Release License: MIT


Why astro-webmanifest?

The web app manifest provides information about a Progressive Web App (PWA) in a JSON text file. See Google's advice on web app manifest to learn more. Current standard for the manifest is on W3C. See also on MDN.

This integration provides numerous features beyond manifest configuration to make your life easier, they are:

  • automatic icon generation - generates multiple icon sizes from a single source;
  • favicon support;
  • inserts all required links and meta into <head> section of generated pages;
  • legacy icon support (iOS);
  • localization - provides unique manifests for path-based localization.

Each of these features has extensive configuration available so you are always in control.


Installation

The experimental astro add command-line tool automates the installation for you. Run one of the following commands in a new terminal window. (If you aren't sure which package manager you're using, run the first command.) Then, follow the prompts, and type "y" in the terminal (meaning "yes") for each one.

# Using NPM
npx astro add astro-webmanifest

# Using Yarn
yarn astro add astro-webmanifest

# Using PNPM
pnpx astro add astro-webmanifest

Then, restart the dev server by typing CTRL-C and then npm run astro dev in the terminal window that was running Astro.

Because this command is new, it might not properly set things up. If that happens, log an issue on Astro GitHub and try the manual installation steps below.

First, install the astro-webmanifest package using your package manager. If you're using npm or aren't sure, run this in the terminal:

npm install --save-dev astro-webmanifest

Then, apply this integration to your astro.config.* file using the integrations property:

astro.config.mjs

import webmanifest from 'astro-webmanifest';

export default {
  // ...
  integrations: [
    webmanifest({
      ...
    }),
  ],
};

Then, restart the dev server.

Usage

The astro-webmanifest integration requires the config object to have at least a name option.

astro.config.mjs

import { defineConfig } from 'astro/config';
import webmanifest from 'astro-webmanifest';

export default defineConfig({
  // ...
  integrations: [
    webmanifest({
      /**
       * required
       **/
      name: 'Your App name',

      /**
       * optional
       **/
      icon: 'src/images/your-icon.svg', // source for favicon & icons

      short_name: 'App',
      description: 'Here is your app description',
      start_url: '/',
      theme_color: '#3367D6',
      background_color: '#3367D6',
      display: 'standalone',
    }),
  ],
});

Next put a source file your-icon.svg for icons generation to the src/images folder. This is optional.

Now, build your site for production via the astro build command. You should find your web manifest, all icons, favicons under dist/ folder!

manifest.webmanifest

{
  "name": "Your App name",
  "icons": [
    {
      "src": "icons/icon-48x48.png",
      "sizes": "48x48",
      "type": "image/png"
    },
    {
      "src": "icons/icon-72x72.png",
      "sizes": "72x72",
      "type": "image/png"
    },
    {
      "src": "icons/icon-96x96.png",
      "sizes": "96x96",
      "type": "image/png"
    },
    {
      "src": "icons/icon-144x144.png",
      "sizes": "144x144",
      "type": "image/png"
    },
    {
      "src": "icons/icon-192x192.png",
      "sizes": "192x192",
      "type": "image/png"
    },
    {
      "src": "icons/icon-256x256.png",
      "sizes": "256x256",
      "type": "image/png"
    },
    {
      "src": "icons/icon-384x384.png",
      "sizes": "384x384",
      "type": "image/png"
    },
    {
      "src": "icons/icon-512x512.png",
      "sizes": "512x512",
      "type": "image/png"
    }
  ],
  "short_name": "App",
  "description": "Here is your app description",
  "start_url": "/",
  "theme_color": "#3367D6",
  "background_color": "#3367D6",
  "display": "standalone"
}

All pages generated at build time will contain the following in the <head> section:

<link rel="icon" href="/favicon-32x32.png" type="image/png" />
<link rel="icon" href="/favicon.svg" type="image/svg+xml" />
<meta name="theme-color" content="#3367D6" />
<link rel="manifest" href="/manifest.webmanifest" crossorigin="anonymous" />

Generations modes

There are 3 usage modes of astro-webmanifest integration: auto, hybrid and manual.

For the most users - you need only the icon option to generate all required icons and favicons.

The integration has default icon preset to generate all icons.

astro.config.mjs

import webmanifest from 'astro-webmanifest';

export default {
  integrations: [
    webmanifest({
      name: 'Your app name',
      icon: 'src/images/your-icon.svg',
    }),
  ],
};
...

Additionally to the icon option you need to provide the icons array as a template to generate required icons.

No default icons. Only icons from the array will be created.

astro.config.mjs

import webmanifest from 'astro-webmanifest';

export default {
  integrations: [
    webmanifest({
      name: 'Your app name',
      icon: 'src/images/your-icon.svg',
      icons: [
        {
          src: 'icons/icon-96x96.png',
          sizes: '96x96',
          type: 'image/png',
        },
        {
          src: 'icons/icon-256x256.png',
          sizes: '256x256',
          type: 'image/png',
        },
        // add any other icon sizes
      ],
    }),
  ],
};  

If you don't provide icon option, you will be fully responsible for manifest configuration.

You should create all icons and favicons manually and put them in a public folder.

astro.config.mjs

import webmanifest from 'astro-webmanifest';

export default {
  integrations: [
    webmanifest({
      name: 'Your app name',
      icons: [
        {
          src: 'icons/icon-96x96.png',
          sizes: '96x96',
          type: 'image/png',
        },
        {
          src: 'icons/icon-256x256.png',
          sizes: '256x256',
          type: 'image/png',
        },
        // add any other icons
      ],
    }),
  ],
};

Configuration

To configure this integration, pass an object to the webmanifest() function call in astro.config.mjs.

:bulb: For this integration to work correctly, it is recommended to use the mjs or js configuration file extensions.

astro.config.mjs

...
export default defineConfig({
  integrations: [webmanifest({
    name: ...
  })]
});

The integration option object extends the Webmanifest type and optionally has config and locales properties as an object.

config property

| Type | Required | Default value | | :-----: | :------: | :-------------: | | String| No | manifest.webmanifest |

Template name for generated manifest file.

astro.config.mjs

import webmanifest from 'astro-webmanifest';

export default {
  integrations: [
    webmanifest({
      name: 'Your app name',
      config: {
        outfile: 'site.webmanifest',
      },
    }),
  ],
};  

See also Localization section.

| Type | Required | Default value | | :-----: | :------: | :-------------: | | String| No | 4 spaces |

Leading characters for each line inserted into <head> to make the output more readable.

This only works when any of the insertFaviconLinks, insertThemeColorMeta, insertManifestLink, insertAppleTouchLinks properties are set to true.

astro.config.mjs

import webmanifest from 'astro-webmanifest';

export default {
  integrations: [
    webmanifest({
      name: 'Your app name',
      config: {
        // Empty string to cut off some bytes from html
        indent: '',
        // ...
      },
    }),
  ],
};  

| Type | Required | Default value | | :-----: | :------: | :-------------: | | String| No | \n |

Trailing characters for each line inserted into <head> to make the output more readable.

This only works when any of the insertFaviconLinks, insertThemeColorMeta, insertManifestLink, insertAppleTouchLinks properties are set to true.

astro.config.mjs

import webmanifest from 'astro-webmanifest';

export default {
  integrations: [
    webmanifest({
      name: 'Your app name',
      config: {
        // Empty string to cut off some bytes from html
        eol: '',
        // ...
      },
    }),
  ],
};  

| Type | Required | Default value | | :-----: | :------: | :-------------: | | Boolean | No | true |

Enable or disable a favicon generation.

This only works when the icon property is not empty.

astro.config.mjs

import webmanifest from 'astro-webmanifest';

export default {
  integrations: [
    webmanifest({
      name: 'Your app name',
      config: {
        icon: 'src/images/your-icon.png',
        createFavicon: false, // default - true
        // ...
      },
    }),
  ],
};  

| Type | Required | Default value | | :-----: | :------: | :-------------: | | Boolean | No | true |

Enable or disable the favicon links insertion into <head>.

This only works when the icon property is not empty and createFavicon is true.

astro.config.mjs

import webmanifest from 'astro-webmanifest';

export default {
  integrations: [
    webmanifest({
      name: 'Your app name',
      config: {
        icon: 'src/images/your-icon.png',
        insertFaviconLinks: false, // default - true
        // ...
      },
    }),
  ],
};  

:exclamation: The final output reprocessing is used to insert the links into <head> sections. It can impact build times for large sites.

| Type | Required | Default value | | :-----: | :------: | :-------------: | | Boolean | No | true |

Enable or disable the meta insertion into <head>.

This only works when the theme_color property is not empty.

astro.config.mjs

import webmanifest from 'astro-webmanifest';

export default {
  integrations: [
    webmanifest({
      name: 'Your app name',
      config: {
        theme_color: '0xFFFF',
        insertThemeColorMeta: false, // default - true
        // ...
      },
    }),
  ],
};  

:exclamation: The final output reprocessing is used to insert the links into <head> sections. It can impact build times for large sites.

| Type | Required | Default value | | :-----: | :------: | :-------------: | | Boolean | No | true |

Enable or disable the manifest link insertion into <head>.

astro.config.mjs

import webmanifest from 'astro-webmanifest';

export default {
  integrations: [
    webmanifest({
      name: 'Your app name',
      config: {
        insertManifestLink: false, // default - true
        // ...
      },
    }),
  ],
};  

:exclamation: The final output reprocessing is used to insert the links into <head> sections. It can impact build times for large sites.

| Type | Required | Default value | | :-----: | :------: | :-------------: | | CrossOrigin | No | anonymus |

crossorigin attribute for the manifest link into <head>.

Available values for the CrossOrigin type are anonymous | use-credentials.

More details on MDN.

astro.config.mjs

import webmanifest from 'astro-webmanifest';

export default {
  integrations: [
    webmanifest({
      name: 'Your app name',
      config: {
        crossOrigin: 'use-credentials', // default - anonymus
        // ...
      },
    }),
  ],
};  

| Type | Required | Default value | | :-----: | :------: | :-------------: | | Boolean | No | false |

Enable or disable apple-touch-icon links into <head>.

iOS versions before 11.3 don't have support for web app manifest spec and don't recognize the icons defined in the webmanifest, so the creation of apple-touch-icon links into <head> is needed.

This only works when the icon property is not empty.

astro.config.mjs

import webmanifest from 'astro-webmanifest';

export default {
  integrations: [
    webmanifest({
      name: 'Your app name',
      config: {
        icon: 'src/images/your-icon.png',
        insertAppleTouchLinks: true, // default - false
        // ...
      },
    }),
  ],
};  

:exclamation: The final output reprocessing is used to insert the links into <head> sections. It can impact build times for large sites.

| Type | Required | Default value | | :-----------: | :------: | :-------------: | | IconPurpose[] | No | undefined |

Available values for the IconPurpose type are badge | maskable | any |monochrome.

If provided it will be appended to the purpose property of generated icons.

This only works when the icon property is not empty.

astro.config.mjs

import webmanifest from 'astro-webmanifest';

export default {
  integrations: [
    webmanifest({
      name: 'Your app name',
      config: {
        icon: 'src/images/your-icon.png',
        iconPurpose: ['badge', 'maskable'], 
        // ...
      },
    }),
  ],
};  

:exclamation: The final output reprocessing is used to insert the links into <head> sections. It can impact build times for large sites.

locales property

Record<string, Webmanifest> - key/object pairs. See usage in Localization section.

Type Webmanifest

| Name | Type | Required | Description | | :---------------------------- | :--------------------: | :------: | :------------------------------------------------------------------------------------------------------------------------------ | | icon | String | No | This is a source for automatically generated favicon and icons. It's a part of Webmanifest type.Icon format: JPEG, PNG, WebP, TIFF, GIF or SVGIcon size: at least as big as the largest icon being generated (512x512 by default).Icon geometry: preferably square, otherwise the results will be padded with transparent bars to be square.If the icon is empty - no automatic icon generation.See more about usage in Generation modes section. | | icons | Icon[] | No | See usage in Generation modes section. | | name | String | Yes | You must provide the name of your app. | | short_name | String | No | | | description | String | No | | | categories | String[] | No | | | lang | String | No | | | dir | Dir | No | auto | ltr | rtl | | iarc_rating_id | String | No | | | id | String | No | | | start_url | String | No | | | scope | String | No | | | theme_color | String | No | Source for meta in <head> | | background_color | String | No | | | display | Display | No | fullscreen | standalone | minimal-ui | browser | | display_override | Display[] | No | | | orientation | Orientation | No | any | natural | landscape | landscape-primary | landscape-secondary | portrait | portrait-primary | portrait-secondary | | protocol_handlers | ProtocolHandler[] | No | | | prefer_related_applications | Boolean | No | | | related_applications | RelatedApplication[] | No | | | screenshots | Image[] | No | | | shortcuts | Shortcut[] | No | |

Type Icon

| Name | Type | Required | Description | | :-------- | :------: | :------: | :--------------------------------------------------------------------------------------------------------------------------- | | src | String | Yes | The path to the image file in URL form. | | sizes | String | No | Space-separated image dimensions. Syntax on MDN | | type | String | No | Media type of the image | | purpose | String | No | Space separated list of image purposes (IconPurpose type). See on W3C |

:bulb: If icon entry sizes property has value any or contains more then one size (96x96 128x128) in that case such entry will be excluded from automatic generation.

For Image, Shortcut, RelatedApplication, ProtocolHandler look on content of index.ts. Also you can find the detailed descriptions on W3C and MDN.

Demo with advanced configuration is in this repo.

Localization

Localization allows you to create a unique manifest, icon set and <head> html for each individual language.

You need to provide locales property as a Record<string, Webmanifest>.

The integration uses the keys of the locales property to make the manifest name unique and to determine the page language by page's path.

The integration expects page paths in the following format: /{locale}{path}, where the locale is a key from the locales property.

Sample configuration below:

astro.config.mjs

import webmanifest from 'astro-webmanifest';

export default {
  integrations: [
    webmanifest({
      icon: 'src/images/logomark-light.svg',

      name: 'Webmanifest i18n test',
      short_name: 'Test',
      description: 'This is the application description',
      lang: 'en-US',
      start_url: '/',

      theme_color: '#3367D6',
      background_color: '#3367D6',
      display: 'standalone',

      locales: {
        es: {
          name: 'Prueba Webmanifest i18n',
          short_name: 'Prueba',
          description: 'Esta es la descripción de la aplicación.',
          lang: 'es-ES',
          start_url: '/es',
        },
        fr: {
          name: 'Test i18n du manifeste Web',
          short_name: 'Test',
          description: "Ceci est la description de l'application",
          lang: 'fr-CA',
          start_url: '/fr',
        },
      },
    }),
  ],
};  

By this configuration three separate manifests will be generated:

  • manifest.webmanifest - for default language;
  • manifest-fr.webmanifest - for fr language;
  • manifest-es.webmanifest - for es language.

And language specific html will be inserted into <head> section of generated pages.

If you need a separate set of icons for each language, add the icon property.

astro.config.mjs

import webmanifest from 'astro-webmanifest';

export default {
  integrations: [
    webmanifest({
      name: 'Your app name',  
      icon: 'src/images/logo.svg',
      lang: 'en-US',
      // ...
      locales: {
        es: {
          icon: 'src/images/logo-es.svg',
          lang: 'es-ES',
          // ...
        },
        fr: {
          lang: 'fr-CA',
          // ...
        },
      },
    }),
  ],
};

In this configuration, the default en language and fr language will have a common icon set, es will have its own icon set.

:bulb: The favicon will be the same for all languages. The source for generation will be taken from the default language.

You can explore a localization usage in this demo repo.

Examples

| Example | Source | Playground | | -------- | -------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------- | | basic | GitHub | Play Online | | advanced | GitHub | Play Online | | i18n | GitHub | Play Online |

Contributing

You're welcome to submit an issue or PR!

Changelog

See CHANGELOG.md for a history of changes to this integration.