Next.js
Web App Manifest in Next.js
A manifest.(json|webmanifest)
file in Next.js provides information about your web application to the browser. It follows the Web Manifest Specification and is crucial for Progressive Web Apps (PWAs) and for providing metadata about your application.
Adding a Manifest File
You can add a manifest file to your Next.js application in two ways:
- Using a static JSON file
- Generating the manifest file using code
Both methods require placing the file in the root of the app
directory.
Static Manifest File
Create a manifest.json
or manifest.webmanifest
file in the root of your app
directory:
{
"name": "My Next.js Application",
"short_name": "Next.js App",
"description": "An application built with Next.js",
"start_url": "/",
// ... other properties
}
Generate a Manifest File
Alternatively, you can generate the manifest file using code. Create a manifest.js
or manifest.ts
file that returns a Manifest
object:
// app/manifest.ts
import type { MetadataRoute } from 'next'
export default function manifest(): MetadataRoute.Manifest {
return {
name: 'Next.js App',
short_name: 'Next.js App',
description: 'Next.js App',
start_url: '/',
display: 'standalone',
background_color: '#fff',
theme_color: '#fff',
icons: [
{
src: '/favicon.ico',
sizes: 'any',
type: 'image/x-icon',
},
],
}
}
Good to know: manifest.js
is a special Route Handler that is cached by default unless it uses dynamic functions or dynamic config options.
Manifest Object Properties
The manifest object can contain numerous properties as defined by the Web Manifest Specification. Some common properties include:
name
: The full name of your applicationshort_name
: A short name for use in constrained spacesdescription
: A description of your applicationstart_url
: The starting URL of your applicationdisplay
: The preferred display mode (e.g., 'standalone', 'fullscreen')background_color
: The background color of the applicationtheme_color
: The theme color of the applicationicons
: An array of icon objects for different sizes and purposes
For a complete list of available properties, refer to:
- The
MetadataRoute.Manifest
type in your code editor if using TypeScript - The MDN Web App Manifest documentation
Best Practices
Provide multiple icon sizes: Include various icon sizes to ensure your app looks good on different devices and in different contexts.
Use meaningful short_name: The
short_name
is used on the user's home screen, launcher, or other places where space may be limited.Set appropriate display mode: Choose a
display
mode that best fits your application's needs (e.g., 'standalone' for a more app-like experience).Specify theme_color: This color is used in the browser's UI when displaying your app, providing a more integrated look.
Keep the manifest up-to-date: Update your manifest file as your application evolves to ensure it accurately represents your current app.
By properly configuring your web app manifest, you can enhance the user experience of your Next.js application, especially when users add it to their home screens or when it's used as a Progressive Web App.