Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

astro-webmanifest

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

astro-webmanifest

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

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
845
increased by2.92%
Maintainers
1
Weekly downloads
 
Created
Source

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

Quick Install

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.

Manual Install

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!

Example of generated `manifest.webmanifest` file

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"
}
Example of inserted HTML

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.

Automatic mode

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',
    }),
  ],
};
...
Hybrid mode

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
      ],
    }),
  ],
};  
Manual mode

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

outfile
TypeRequiredDefault value
StringNomanifest.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.

indent
TypeRequiredDefault value
StringNo4 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: '',
        // ...
      },
    }),
  ],
};  
eol
TypeRequiredDefault value
StringNo\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: '',
        // ...
      },
    }),
  ],
};  
createFavicon
TypeRequiredDefault value
BooleanNotrue

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
        // ...
      },
    }),
  ],
};  
insertFaviconLinks
TypeRequiredDefault value
BooleanNotrue

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.

insertThemeColorMeta
TypeRequiredDefault value
BooleanNotrue

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.

insertManifestLink
TypeRequiredDefault value
BooleanNotrue

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.

crossOrigin
TypeRequiredDefault value
CrossOriginNoanonymus

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
        // ...
      },
    }),
  ],
};  
insertAppleTouchLinks
TypeRequiredDefault value
BooleanNofalse

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.

iconPurpose
TypeRequiredDefault value
IconPurpose[]Noundefined

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

NameTypeRequiredDescription
iconStringNoThis is a source for automatically generated favicon and icons. It's a part of Webmanifest type.

Icon format: JPEG, PNG, WebP, TIFF, GIF or SVG
Icon 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.
iconsIcon[]NoSee usage in Generation modes section.
nameStringYesYou must provide the name of your app.
short_nameStringNo
descriptionStringNo
categoriesString[]No
langStringNo
dirDirNoauto | ltr | rtl
iarc_rating_idStringNo
idStringNo
start_urlStringNo
scopeStringNo
theme_colorStringNoSource for meta in <head>
background_colorStringNo
displayDisplayNofullscreen | standalone | minimal-ui | browser
display_overrideDisplay[]No
orientationOrientationNoany | natural | landscape | landscape-primary | landscape-secondary | portrait | portrait-primary | portrait-secondary
protocol_handlersProtocolHandler[]No
prefer_related_applicationsBooleanNo
related_applicationsRelatedApplication[]No
screenshotsImage[]No
shortcutsShortcut[]No

Type Icon

NameTypeRequiredDescription
srcStringYesThe path to the image file in URL form.
sizesStringNoSpace-separated image dimensions. Syntax on MDN
typeStringNoMedia type of the image
purposeStringNoSpace 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

ExampleSourcePlayground
basicGitHubPlay Online
advancedGitHubPlay Online
i18nGitHubPlay Online

Contributing

You're welcome to submit an issue or PR!

Changelog

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

Keywords

FAQs

Package last updated on 08 Sep 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc