Deploying to production

Contents

In this chapter you will launch cloud instances of both Enonic and Next.js, link them together, and see the glorious result.

Introduction

To complete this chapter, you’ll be using Enonic to host the XP side of things, and Vercel for the front-end. Vercel are the makers of Next.js.

Both Enonic and Next.js are open source software projects and can be deployed more or less anywhere. Have a look at the Enonic platform hosting page for more details.

Task: Sign up to Enonic

Create an account to get a free developer instance of Enonic XP.

  1. Sign up for a free developer account.

  2. Create a new solution using the CMS essentials template - This installs applications like Content Studio and Guillotine automatically.

  3. Verify the solution - When the new solution has started, launch Enonic XP Admin to verify that everything is up and running.

Task: Install your app

  1. Connect the Enonic CLI to the cloud - Run this command, and follow the instructions:

    enonic cloud login
  2. Install your Enonic app - Run this command from the tutorial-nextjs-enonic folder:

    enonic cloud app install
  3. Verify that the app was installed from Solution → Applications in the Enonic Console.

  4. Open Content Studio - From XP Admin, open Content Studio to verify that your app automatically initialized the Intro project and imported content on the server, like on your local machine.

  5. Create a new site called Hmdb and move all imported content there, like you did locally.

  6. Create a localized version of the site, as you did before.

Task: Expose the API via a route

The purpose of routes is to expose APIs and other endpoints in Enonic XP, like the :8080/api API root, to the internet.

  1. Create a new ingress called "API" from Solution → Ingresses.

    Use the following settings:

    Target path

    /api

    Public path

    /api

    ID provider

    -empty-

  2. Make a note of the route URL - You will find it in the route details.

    The URL should look something like this: https://account-solution-environment.enonic.net/api.

Task: Add your front-end to Git

Vercel, which will host your front-end, needs write access to a Git repo with your code.

  1. Create a new Git repo - Vercel supports GitHub, GitLab and Bitbucket.

  2. Commit and push your Next.js app files to the new repo.

Task: Deploy the Next.js app

You are now ready to deploy the front-end application.

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

  2. Create a new project called enonic-tutorial and connect it to the repo you created in the previous task.

  3. Add the following environment variables to the project (name: value):

    • ENONIC_APP_NAME: <enonic app name (com.example.tutorial.nextjs in this tutorial)>

    • ENONIC_API: <route URL from the previous task, ending with /api>

    • ENONIC_ADMIN: <enonic admin URL> (optional, only if admin is on a different domain than the API)

    • ENONIC_MEDIA_CDN: <media URL> (optional, only if images and attachments are served from a different domain than the API)

    • ENONIC_API_TOKEN: <yourSecret>

    • ENONIC_MAPPINGS: en:intro/hmdb,no:intro-no/hmdb

  4. Verify the result - The front page of your app should now display a 404, as you haven’t published any content yet.

    Make a note of the URL to the Vercel app, you will need it later.
    Front page rendering before a site has been added to it
    Check the Vercel function logs if you are not getting the expected result.

With the front-end running, you can link it to Content Studio to activate the preview, and start building pages once again.

To save time, you may export content from your local machine and import it to the server using the Data Toolbox app.
  1. Install the Next.XP app - Go to Solution → Applications → Install, search for Next.XP, select it and click install.

  2. Add configuration:

    Configure the Next.XP application in the solution console
    Add the following lines to the app config field to override the default values:
    nextjs.default.url = <Vercel application URL>
    nextjs.default.secret = <yourSecret>
    Remember, you can also create named configurations if needed.
    The app automatically adds the configured front-end origins to Content Studio’s Content Security Policy, so the preview from your Vercel URL works without any Content Studio configuration. Unlike your local sandbox, a production server allows only explicitly configured URLs - the http://localhost:3000 default applies to dev mode only, so this configuration step is required.
    If you imported content from your local environment, the following step can be skipped.
    The preview widget and the revalidation hook only work for sites the Next.XP app has been added to. Adding it to the site also lets you select a named configuration from the app config file.
  3. Add the Next.XP app to the site, in addition to your own app.

  4. Save and verify that the configuration is working by testing the Content Studio preview.

Task: Publish a page

It’s time to test that everything is working.

  1. Create the front page - If you did not import content, create the front page once again using the page editor. Maybe something like this will do?

    Front page as seen from Content Studio
  2. Go live - Publish the new page, and verify that your live site gets updated:

    Content rendered on the live server

Summary

You did it! 🎉 You’ve built a Next.js site that renders content from Enonic, with pages composed by editors, live preview in Content Studio, static generation and multiple languages - and deployed it to Enonic Cloud and Vercel. That completes the tutorial.

Further reading

None of this is required, but here are some suggestions for where to go from here:


Contents

Contents