Running the demo
Contents
On this page you will run the finished application from this tutorial, without going through the chapters. You get a Next.js front-end that renders content from Enonic, and a working preview in Content Studio.
| Want to learn how it works? Start with the first chapter of the tutorial instead. |
There are two ways to run the demo:
-
Run it locally, against an Enonic sandbox on your own machine. This takes a few minutes and requires no accounts.
-
Deploy it to the cloud, with the front-end on Vercel and the back-end on Enonic Cloud.
Run the demo locally
To run the demo locally you will need the Enonic CLI and Git, in addition to the prerequisites of the tutorial.
Task: Start Enonic
The application reads its content from a sandbox, which is a local instance of Enonic XP.
-
Create a sandbox - Run this command in your terminal. It works from any folder:
enonic sandbox create demo_nextjs -t hmdb -fThe
hmdbtemplate installs Content Studio, the Guillotine GraphQL API, the Next.XP app and the Headless Movie DB app with its sample content. The-foption accepts the default values for the remaining options, which includes the latest version of Enonic XP, and starts the sandbox.The demo uses its own sandbox and sample content, separate from the sandbox you create in the tutorial. All sandboxes use the same ports by default, so stop any other sandbox before starting this one. -
Verify that the sandbox is running - Open http://localhost:8080. The Enonic XP Welcome page appears.
Leave the sandbox running in this terminal window. To stop it, press ctrl-c. To start it again, run enonic sandbox start demo_nextjs from any folder.
Task: Run the application
-
Get the code - Open a new terminal window in the folder where you keep your tutorial projects, and run this command:
git clone --branch master https://github.com/enonic/tutorial-nextjs.gitThis creates a folder called
tutorial-nextjs/, holding the application built in this version of the tutorial. -
Install the dependencies - Enter the new folder and install:
cd tutorial-nextjs npm install -
Start the application - Run this command from the
tutorial-nextjsfolder:npm run devThe application needs no configuration. Its
.envfile points at the sandbox onhttp://localhost:8080, and at thehmdbproject and site that the Headless Movie DB app created. -
Verify the result - Open http://localhost:3000. It shows the front page of the Headless Movie DB:
-
Browse the site - Follow the links on the front page, or go directly to the movies or to Léa Seydough. Her page shows a biography with an image, links and a fact box.
To stop the application, press ctrl-c in its terminal window.
Task: Activate preview
The Next.XP app provides the "Next.js Preview" widget in Content Studio. It is already installed in the sandbox, but Content Studio only uses it for sites the app has been added to.
-
Open Content Studio - On the Welcome page at http://localhost:8080, click Log In as Guest, then select Content Studio from the
XP menu. TheHeadless Movie DBproject opens automatically. -
Add the Next.XP app to the site - Select the
Headless Movie Databasesite and clickEdit. In theApplicationsfield, selectNext.XPin addition to theHeadless Movie DBapp, and clickApply.By default, the Next.XP app expects the front-end on
http://localhost:3000, with the secretmySecretKey. This matches the application you started, so no further configuration is needed. -
Verify the preview - The preview now shows the front page rendered by Next.js. The same goes for any item you select in the content tree:
-
Publish the site - Click
Mark as ready, and thenPublish nowin the Publishing Wizard. From now on, the Next.XP app notifies the front-end whenever content is published.
Deploy the demo to the cloud
To deploy the demo you will need an account with GitHub, GitLab or Bitbucket. You will sign up for free Enonic and Vercel accounts along the way.
Task: Set up Enonic
-
Sign up for a free Enonic account at https://www.enonic.com/sign-up
-
Create a new solution - After logging in to your account, select
Solutions → Create → Headless Movie DB. This installs all required Enonic apps and sets up the routes needed. -
Find the API URL - From your new solution, click
Environmentsand make a note of the URL to theAPIingress. You will need it when deploying the front-end.
Task: Create a Git repo
To deploy an app, Vercel (the Next.js hosting provider) needs access to a Git repo with the app code, where you have write access.
Vercel uses this access to automatically fetch and build the app based on events. The write permission is used to set up the required webhooks in your Git provider.
-
Clone or fork the demo app - Copy https://github.com/enonic/tutorial-nextjs to your personal GitHub, Bitbucket or GitLab account.
If you are using GitHub, press the Forkbutton at the top right.
Task: Deploy the Next.js front-end
Vercel are the makers of Next.js, and you’ll be using their service to host the front-end.
-
Sign up to Vercel at https://vercel.com/
-
Create a new project - Name it
enonic-demoor similar, and follow the steps to connect it to the Git repo you created in the previous task. -
Add environment variables - These tell the app where to find the API endpoints, and define a secret you will need to access preview mode in a later step.
Name Value ENONIC_APP_NAMEcom.enonic.app.hmdbENONIC_APIURL to the Enonic API root, for example
https://<environment>/apiENONIC_ADMINOptional. Enonic admin URL, only needed if admin is on a different domain than the API
ENONIC_MEDIA_CDNOptional. Media URL, only needed if images and attachments are served from a different domain than the API
ENONIC_API_TOKENA secret of your choice
ENONIC_MAPPINGSen:hmdb/hmdbENONIC_MAPPINGSuses the pattern<locale>:<project>/<site-path>to tell the front-end which project and site to use. -
Verify the deployment - After deploying, the Vercel app should render the front page:
Make a note of the Vercel application URL, you will need it when setting up the preview.
Check the Vercel function logs if you are not getting the expected result.
Task: Activate preview
With the front-end running, it is time to enable preview in Content Studio.
The Next.XP app, which provides the "Next.js Preview" widget, is already installed in the solution. It needs to know where to find the front-end server, and it must be added to the site before Content Studio uses it for preview.
-
Open the Next.XP app - In the Enonic Cloud console, go to
Applicationsand selectNext.XP.
-
Configure the app - Select
Edit…from the app context menu, then add the URL and secret of the Vercel app you deployed in the previous task:nextjs.default.url=<Vercel application URL> nextjs.default.secret=<yourSecret>
This overrides the default preview configuration for all sites using the Next.XP app. Use a different keyword than defaultto define multiple front-ends. -
Open Content Studio - Save, then launch Enonic XP Admin from the solution dashboard and open Content Studio.
-
Add the Next.XP app to the site - Select the
Headless Movie Databasesite and clickEdit. In theApplicationsfield, selectNext.XPin addition to theHeadless Movie DBapp, and clickApply. -
Verify the preview - The preview should now show your front-end:
-
Publish the site - Click
Mark as ready, and thenPublish nowin the Publishing Wizard. From now on, the Next.XP app notifies the front-end whenever content is published.
How it works
Content Studio asks every widget if it supports preview, and if so, asks the widget to render a preview of the content. When asked, the Next.js Preview widget does the following:
-
Sends an encrypted request to the configured Next.js front-end to fetch the mappings from XP paths to front-end URLs
-
Uses those mappings to generate a front-end URL for the given content
-
Returns the URL to Content Studio, with the encrypted
xpparameter, for direct rendering -
When Content Studio loads the URL, the front-end proxy goes through
/api/previewif the draft/build or XP-session cookies need synchronization, then redirects to the clean content URL
Your edits are only visible in Content Studio, until published.
When you publish changes, the Next.XP app automatically triggers revalidation of all pages in the front-end, and the changes go live.
Each part of the application is explained in the tutorial, starting with setting up Enonic.