Skip to content

PWA Manifest Generator — JavaScript 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 JavaScript implementation — the same logic the interactive tool runs, in a shareable, citable form.

// pwa-manifest — build a fixed-key-order manifest.json from form fields,
// the plain-JS twin of src/lib/pwa-manifest.ts. Same contract as the TS lib
// and this dir's typescript.ts: fixed key order (name, short_name,
// description, start_url, display, orientation, background_color,
// theme_color, icons), blank optional fields omitted, icon sizes defaulting
// to [192, 512], hard errors suppressing emission, and the short_name >12
// warning never blocking.

/**
 * Build the manifest document. Returns { ok, errors, warnings, json } —
 * on hard errors ok is false, errors lists every problem and json is ''.
 */
export function buildManifest(input) {
  const errors = [];
  const warnings = [];

  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 && !/^#[0-9a-fA-F]{6}$/.test(backgroundColor)) errors.push('background_color must be a #rrggbb hex value.');
  if (themeColor && !/^#[0-9a-fA-F]{6}$/.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();
  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 lines = [];
  const add = (key, value) => lines.push('  ' + JSON.stringify(key) + ': ' + JSON.stringify(value));
  add('name', name);
  if (shortName) add('short_name', shortName);
  if (description) add('description', description);
  if (startUrl) add('start_url', startUrl);
  if (input.display) add('display', input.display);
  if (input.orientation) add('orientation', input.orientation);
  if (backgroundColor) add('background_color', backgroundColor);
  if (themeColor) add('theme_color', themeColor);

  const purpose = input.maskable ? 'maskable' : 'any';
  const iconEntries = sizes.map(
    (s) =>
      '    {\n' +
      '      "src": ' + JSON.stringify(`icons/icon-${s}x${s}.png`) + ',\n' +
      '      "sizes": ' + JSON.stringify(`${s}x${s}`) + ',\n' +
      '      "type": "image/png",\n' +
      '      "purpose": ' + JSON.stringify(purpose) + '\n' +
      '    }',
  );
  // JSON.stringify renders an empty array inline.
  lines.push(iconEntries.length === 0 ? '  "icons": []' : '  "icons": [\n' + iconEntries.join(',\n') + '\n  ]');

  return { ok: true, errors, warnings, json: '{\n' + lines.join(',\n') + '\n}\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 →