Skip to content

Next.js

Next.js is a React framework with support for static export, server rendering, and hybrid modes. The pageflare/next integration supports static export mode only (output: 'export'), where Next.js generates a fully static site to the out/ directory.

Terminal window
npm install -D pageflare

The simplest approach is to chain pageflare after next build in your npm scripts:

package.json
{
"scripts": {
"build": "next build && pageflare out/"
}
}

This requires @pageflare/cli to be available on PATH (it’s installed automatically as a dependency of pageflare).

For more control, use the optimizeNextExport function in a custom build script:

scripts/optimize.mjs
import { optimizeNextExport } from 'pageflare/next'
await optimizeNextExport({
platform: 'vercel',
log: 'info',
})
package.json
{
"scripts": {
"build": "next build && node scripts/optimize.mjs"
}
}
OptionTypeDefaultDescription
platformstring'auto'Deployment platform: auto, vercel, netlify, cloudflare-pages, none
logstring'warn'Log level: off, error, warn, info, debug
argsstring[][]Extra CLI flags passed to pageflare optimize
outputDirstring'out'Path to the static export directory

The package also exports a withpageflare config wrapper that validates your Next.js config is in export mode:

next.config.mjs
import withpageflare from 'pageflare/next'
export default withpageflare()({
output: 'export',
// ... rest of your Next.js config
})

This doesn’t hook into the build — it only warns if output: 'export' is not set. Optimization still happens via the npm script or programmatic API.

Your next.config.mjs must have output: 'export':

next.config.mjs
export default {
output: 'export',
}

pageflare is a single binary that replaces several Next.js packages. After adding pageflare, you can remove:

Packagepageflare equivalent
next-optimized-imagesImage optimization, responsive srcset, lazy loading
next-pwa / @ducanh2912/next-pwaNot replaced — keep if you need PWA
next-sitemapSitemap generation via llm.sitemap config
next-seoNot replaced — keep for meta tags; pageflare handles performance, not SEO tags
@next/bundle-analyzerNot replaced — different purpose (bundle analysis vs output optimization)

Static exports work with any hosting provider. For platform-specific image optimization:

Set the PAGEFLARE_LICENSE environment variable to unlock Pro optimizations:

Terminal window
PAGEFLARE_LICENSE=your-key npm run build

“Next.js integration only supports output: 'export' mode” Add output: 'export' to your next.config.mjs. Without it, Next.js generates server bundles that pageflare cannot optimize.

out/ directory is empty Make sure next build completes before pageflare runs. If using the npm script approach (next build && pageflare out/), the && ensures correct ordering.

Images not optimized Next.js’s built-in <Image> component uses server-side optimization by default, which is disabled in static export mode. pageflare’s image optimization (lazy loading, dimensions, responsive srcset) works on standard <img> tags in the exported HTML.