javascript 59 lines · 9 steps

Generating a PWA manifest and icon in Next.js

A single file exports a web app manifest plus a dynamically rendered PNG icon using Next.js metadata conventions.

Explained by highlit
1import { ImageResponse } from 'next/og'
2 
3export function manifest() {
4 return {
5 name: 'Acme Analytics Dashboard',
6 short_name: 'Acme',
7 description: 'Real-time metrics and reporting for your team.',
8 start_url: '/',
9 scope: '/',
10 display: 'standalone',
11 orientation: 'portrait',
12 background_color: '#0f172a',
13 theme_color: '#6366f1',
14 categories: ['business', 'productivity'],
15 icons: [
16 {
17 src: '/icon',
18 sizes: '512x512',
19 type: 'image/png',
20 purpose: 'any',
21 },
22 {
23 src: '/apple-icon',
24 sizes: '180x180',
25 type: 'image/png',
26 purpose: 'maskable',
27 },
28 ],
29 }
30}
31 
32export const size = { width: 512, height: 512 }
33export const contentType = 'image/png'
34 
35export function icon() {
36 return new ImageResponse(
37 (
38 <div
39 style={{
40 width: '100%',
41 height: '100%',
42 display: 'flex',
43 alignItems: 'center',
44 justifyContent: 'center',
45 background: 'linear-gradient(135deg, #6366f1, #8b5cf6)',
46 color: '#fff',
47 fontSize: 320,
48 fontWeight: 700,
49 borderRadius: 96,
50 }}
51 >
52 A
53 </div>
54 ),
55 { ...size }
56 )
57}
58 
59export default manifest
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1Next.js turns exported functions and constants in special files into generated static assets at build time.
  2. 2A manifest describes install behavior while its icons array points at routes that themselves produce images.
  3. 3ImageResponse renders JSX to a raster image on the server, so no design tool is needed for simple icons.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Generating a PWA manifest and icon in Next.js — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code