Walkthrough: Exporting a JSS Next.js app to static HTML files with next export
Next.js allows you to export your application as static HTML files that can be run without a Node.js server using any hosting service that supports serving static files or a content delivery network.
This walkthrough describes the steps for exporting a JSS Next.js application to static HTML files using the Next.js command next export.
Static HTML export does not support:
-
Server-side rendered (SSR) pages (using
getServerSideProps). -
Pages with incremental static regeneration.
-
API routes.
-
Routes with
fallback: true. -
Internationalized routing.
-
Request rewrites.
-
Visitor identification and personalization.
-
Exporting the Sitecore Media Library. You still need a running Content Delivery instance for the media library.
-
The default image
loaderused by thenext/imagecomponent.
If you are creating a hybrid application, where some pages are server-side rendered and others are statically generated, Next.js automatically builds routes/pages that use getStaticProps as a static HTML files.
This walkthrough describes how to:
-
Prepare your application for static HTML export.
-
Export the application.
Prepare your application for static HTML export
To address some of the limitations of the export process, you must make some changes to the JSS Next.js app.
Before exporting your application to static HTML files:
-
In the root directory of the JSS Next.js app, modify the file
sitecore/config/<appname>.configto:-
Include the Sitecore server URL as part of the media requests:
-
Alternatively, remove the
<IncludeServerUrlInMediaUrls>configuration patch:
-
-
Run
jss deploy config. -
In the root folder of the Next.js JSS app, in the
.envfile, add the environment variable: -
In the
next.config.jsfile, use the environment variableEXPORT_MODEto instruct Next.js to usei18nandrewritesoptions only ifEXPORT_MODEis set tofalse: -
In the
src/components/Layout.tsxfile, update the use of the<VisitorIdentification />component to account for the value of theEXPORT_MODEenvironment variable: -
In the
.envfile, define thePUBLIC_URLvariable. -
In the
package.jsonfile, add the following scripts:-
A basic export script:
-
A script for exporting apps that use content from local files:
Replace the
PORTvalue with the port of your disconnected server. -
A script for exporting connected apps that use Sitecore content and data:
-
-
In any component or file where you used the JSS
NextImagecomponent or thenext/imagecomponent, replace the instances with the JSSImagecomponent. Image optimization using theNextImageandnext/imagecomponents is not supported with static export.
Export the application
To export the application to static HTML files, you use one of the scripts defined for this purpose in the package.json file.
To export a disconnected JSS Next.js application that uses content and data defined in local files:
-
In the root directory of the JSS application, start the app with the disconnected proxy:
-
In a terminal, in the root directory of the JSS Next.js app, run the script:
To export a JSS Next.js application connected to Sitecore:
-
In a terminal, in the root directory of the JSS Next.js app, run the script:
In a CI/CD context, you can run these scripts using npm instead of jss. For example:
The export scripts place the resulting files in the out directory.