exportPathMap
This feature is exclusive to
next exportand currentlydeprecated in favor ofgetStaticPathswithpagesorgenerateStaticParamswithapp.
exportPathMap allows you to specify a mapping of request paths to page destinations, to be used during export. Paths defined inexportPathMap will also be available when usingnext dev.
Let's start with an example, to create a customexportPathMap for an app with the following pages:
pages/index.jspages/about.jspages/post.js
Opennext.config.js and add the followingexportPathMap config:
module.exports= {exportPathMap:asyncfunction ( defaultPathMap, { dev, dir, outDir, distDir, buildId } ) {return {'/': { page:'/' },'/about': { page:'/about' },'/p/hello-nextjs': { page:'/post', query: { title:'hello-nextjs' } },'/p/learn-nextjs': { page:'/post', query: { title:'learn-nextjs' } },'/p/deploy-nextjs': { page:'/post', query: { title:'deploy-nextjs' } }, } },}Good to know: the
queryfield inexportPathMapcannot be used withautomatically statically optimized pages orgetStaticPropspages as they are rendered to HTML files at build-time and additional query information cannot be provided duringnext export.
The pages will then be exported as HTML files, for example,/about will become/about.html.
exportPathMap is anasync function that receives 2 arguments: the first one isdefaultPathMap, which is the default map used by Next.js. The second argument is an object with:
dev-truewhenexportPathMapis being called in development.falsewhen runningnext export. In developmentexportPathMapis used to define routes.dir- Absolute path to the project directoryoutDir- Absolute path to theout/directory (configurable with-o). Whendevistruethe value ofoutDirwill benull.distDir- Absolute path to the.next/directory (configurable with thedistDirconfig)buildId- The generated build id
The returned object is a map of pages where thekey is thepathname and thevalue is an object that accepts the following fields:
page:String- the page inside thepagesdirectory to renderquery:Object- thequeryobject passed togetInitialPropswhen prerendering. Defaults to{}
The exported
pathnamecan also be a filename (for example,/readme.md), but you may need to set theContent-Typeheader totext/htmlwhen serving its content if it is different than.html.
Adding a trailing slash
It is possible to configure Next.js to export pages asindex.html files and require trailing slashes,/about becomes/about/index.html and is routable via/about/. This was the default behavior prior to Next.js 9.
To switch back and add a trailing slash, opennext.config.js and enable thetrailingSlash config:
module.exports= { trailingSlash:true,}Customizing the output directory
next export will useout as the default output directory, you can customize this using the-o argument, like so:
nextexport-ooutdirWarning: Using
exportPathMapis deprecated and is overridden bygetStaticPathsinsidepages. We don't recommend using them together.
Was this helpful?