Skip to content

PWA Manifest Generator — TypeScript source

Assemble a valid Web App Manifest — name, colors, display mode, icon sizes, maskable purpose — with a stable key order and instant JSON you can copy or download.

This is the TypeScript implementation — the same logic the interactive tool runs, in a shareable, citable form.

// pwa-manifest — pure Web App Manifest builder (SEO/meta trio). Form fields
// in, a stable-key-order manifest.json out; the island owns preview + copy.

export interface ManifestInput {
  name: string;
  shortName?: string;
  description?: string;
  startUrl?: string;
  display?: 'browser' | 'standalone' | 'minimal-ui' | 'fullscreen';
  orientation?: 'any' | 'portrait' | 'landscape';
  backgroundColor?: string;
  themeColor?: string;
  /** Icon edge sizes in px; defaults to [192, 512]. */
  iconSizes?: number[];
  /** Marks every icon purpose "maskable" instead of "any". */
  maskable?: boolean;
}

export interface ManifestResult {
  ok: boolean;
  errors: string[];
  warnings: string[];
  json: string;
}

const HEX = /^#[0-9a-fA-F]{6}$/;

/**
 * Build the manifest document with a fixed key order (name, short_name,
 * description, start_url, display, orientation, background_color,
 * theme_color, icons). Blank optional fields are omitted; icon sizes
 * default to 192 and 512 and emit one entry each. Hard errors (missing
 * name, non-hex colors, invalid icon sizes) suppress emission; the
 * short_name >12 warning never blocks.
 */
export function buildManifest(input: ManifestInput): ManifestResult {
  const errors: string[] = [];
  const warnings: string[] = [];

  const name = input.name?.trim() ?? '';
  const shortName = input.shortName?.trim() ?? '';
  const description = input.description?.trim() ?? '';
  const startUrl = input.startUrl?.trim() ?? '';
  const backgroundColor = input.backgroundColor?.trim() ?? '';
  const themeColor = input.themeColor?.trim() ?? '';
  const sizes = input.iconSizes ?? [192, 512];

  if (!name) errors.push('name is required.');
  if (backgroundColor && !HEX.test(backgroundColor)) errors.push('background_color must be a #rrggbb hex value.');
  if (themeColor && !HEX.test(themeColor)) errors.push('theme_color must be a #rrggbb hex value.');
  if (sizes.some((s) => !Number.isInteger(s) || s <= 0)) errors.push('icon sizes must be positive integers.');
  const seenSizes = new Set<number>();
  for (const s of sizes) {
    if (seenSizes.has(s)) errors.push(`icon size ${s} is listed twice.`);
    seenSizes.add(s);
  }
  if (errors.length > 0) return { ok: false, errors, warnings: [], json: '' };

  if (shortName.length > 12) warnings.push(`short_name is ${shortName.length} characters — home-screen labels truncate around 12.`);

  const doc: Record<string, unknown> = { name };
  if (shortName) doc.short_name = shortName;
  if (description) doc.description = description;
  if (startUrl) doc.start_url = startUrl;
  if (input.display) doc.display = input.display;
  if (input.orientation) doc.orientation = input.orientation;
  if (backgroundColor) doc.background_color = backgroundColor;
  if (themeColor) doc.theme_color = themeColor;
  doc.icons = sizes.map((s) => ({
    src: `icons/icon-${s}x${s}.png`,
    sizes: `${s}x${s}`,
    type: 'image/png',
    purpose: input.maskable ? 'maskable' : 'any',
  }));

  return { ok: true, errors, warnings, json: JSON.stringify(doc, null, 2) + '\n' };
}

Also available in 13 other languages

Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →