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.

  1. Create a sandbox - Run this command in your terminal. It works from any folder:

    enonic sandbox create demo_nextjs -t hmdb -f

    The hmdb template installs Content Studio, the Guillotine GraphQL API, the Next.XP app and the Headless Movie DB app with its sample content. The -f option 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.
  2. 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

  1. 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.git

    This creates a folder called tutorial-nextjs/, holding the application built in this version of the tutorial.

  2. Install the dependencies - Enter the new folder and install:

    cd tutorial-nextjs
    
    npm install
  3. Start the application - Run this command from the tutorial-nextjs folder:

    npm run dev

    The application needs no configuration. Its .env file points at the sandbox on http://localhost:8080, and at the hmdb project and site that the Headless Movie DB app created.

  4. Verify the result - Open http://localhost:3000. It shows the front page of the Headless Movie DB:

    Front page showing a heading, text with links and a picture
  5. 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.

  1. Open Content Studio - On the Welcome page at http://localhost:8080, click Log In as Guest, then select Content Studio from the XP menu. The Headless Movie DB project opens automatically.

  2. Add the Next.XP app to the site - Select the Headless Movie Database site and click Edit. In the Applications field, select Next.XP in addition to the Headless Movie DB app, and click Apply.

    By default, the Next.XP app expects the front-end on http://localhost:3000, with the secret mySecretKey. This matches the application you started, so no further configuration is needed.

  3. 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:

    Content Studio showing the front page rendered by Next.js in the preview
  4. Publish the site - Click Mark as ready, and then Publish now in 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

  1. Sign up for a free Enonic account at https://www.enonic.com/sign-up

  2. 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.

  3. Find the API URL - From your new solution, click Environments and make a note of the URL to the API ingress. 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.

  1. 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 Fork button 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.

  1. Sign up to Vercel at https://vercel.com/

  2. Create a new project - Name it enonic-demo or similar, and follow the steps to connect it to the Git repo you created in the previous task.

  3. 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_NAME

    com.enonic.app.hmdb

    ENONIC_API

    URL to the Enonic API root, for example https://<environment>/api

    ENONIC_ADMIN

    Optional. Enonic admin URL, only needed if admin is on a different domain than the API

    ENONIC_MEDIA_CDN

    Optional. Media URL, only needed if images and attachments are served from a different domain than the API

    ENONIC_API_TOKEN

    A secret of your choice

    ENONIC_MAPPINGS

    en:hmdb/hmdb

    ENONIC_MAPPINGS uses the pattern <locale>:<project>/<site-path> to tell the front-end which project and site to use.
  4. Verify the deployment - After deploying, the Vercel app should render the front page:

    Front page showing a heading, text with links and a picture

    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.

  1. Open the Next.XP app - In the Enonic Cloud console, go to Applications and select Next.XP.

    The Next.XP application selected in the Applications list
  2. 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>
    Edit application dialog with the URL and secret in the Configuration field
    This overrides the default preview configuration for all sites using the Next.XP app. Use a different keyword than default to define multiple front-ends.
  3. Open Content Studio - Save, then launch Enonic XP Admin from the solution dashboard and open Content Studio.

  4. Add the Next.XP app to the site - Select the Headless Movie Database site and click Edit. In the Applications field, select Next.XP in addition to the Headless Movie DB app, and click Apply.

  5. Verify the preview - The preview should now show your front-end:

    Front page as seen from Content Studio
  6. Publish the site - Click Mark as ready, and then Publish now in 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:

  1. Sends an encrypted request to the configured Next.js front-end to fetch the mappings from XP paths to front-end URLs

  2. Uses those mappings to generate a front-end URL for the given content

  3. Returns the URL to Content Studio, with the encrypted xp parameter, for direct rendering

  4. When Content Studio loads the URL, the front-end proxy goes through /api/preview if 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.


Contents

Contents