Git Product home page Git Product logo

jtiala / static-nextjs-contentful Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 298 KB

An example repository for a Next.js site using the app router, fetching data from Contentful, and generating TypeScript types for the content model.

Home Page: https://jtiala.github.io/static-nextjs-contentful/

License: MIT License

JavaScript 8.04% TypeScript 91.19% CSS 0.77%
contentful github-pages nextjs typescript

static-nextjs-contentful's Introduction

Next.js static export with app router and Contentful integration

This is an example repository for a static Next.js site using the app router, fetching data from Contentful, and generating TypeScript types for the content model. It also includes setups for local previewing of unpublished Contentful content and automatic site generation with GitHub Actions when content changes in Contentful.

The repo is inspired by this great article by Max Schmitt but I've made some major changes.

Running the site locally

If you want to run this project yourself, you'll need to create a new Contentful space first. Also, create a new API key in Settings -> API keys -> Add API key and a personal access token in Settings -> CMA Tokens -> Create personal access token.

Next, copy .env.local.example as .env.local and fill in your API keys from Contentful. If you want to include unpublished content in your local build, set CONTENTFUL_CONTENT_PREVIEW=true. BASE_PATH should be left empty for local builds.

Now you can import the included content model in your Contentful space. Use the following script:

npm run import-content-model contentful-content-model.json

Then, run the development server:

npm run dev

Open http://localhost:3000 with your browser to see the result.

Building the static export

You can build the static site with the build script:

npm run build

To preview the build locally, run:

npm run start

Generating types from the Contentful Content Model

If you make changes to the content model in Contentful, you can run the following command to generate updated TypeScript types.

npm run generate-types

Setting up local content preview in Contentful

Because the goal of this example repository is to have a static site without the Next.js server instance, we cannot use content preview in the deployed site. If you would rather use SSR and have the server instance running, you can follow Max's article to set up content preview with Next.js's draft mode. In this example, we can preview the content using a locally running site. Of course, you could also deploy the preview site somewhere, but that setup is beyond our scope.

In Contentful, go to Settings -> Content preview. Set the preview mode to Preview in new tab. Next, click Add content preview. Check all the content types and set URLs like this for each content type: http://localhost:3000/[CONTENT_TYPE_ID in plural]/{entry.sys.id}. Now you should be able to open unpublished content in localhost, just make sure that you have set the CONTENTFUL_CONTENT_PREVIEW as true and that the dev server or static build preview is running.

Triggering GitHub Actions workflow when content changes in Contentful

You'll probably want to rebuild the static site after you publish new content in Contentful. This can be achieved with a webhook.

  1. Follow this guide to create a new personal access token (classic) in GitHub. The token needs only repo access.

  2. Create a new webhook in Contentful: Settings -> Webhooks -> Add Webhook.

    • Give the webhook a name of your choice.

    • For the URL, use POST https://api.github.com/repos/<username>/<repository>/dispatches.

    • Select specific triggering events: Publish, Unpublish, and Delete for Entry events.

    • Add two custom headers: Accept: application/vnd.github.v3+json and User-Agent: Contentful Webhook

    • Add one secret header: Authorization: Bearer <GitHub Personal access token

    • Customize the webhook payload:

      {
        "event_type": "publish-event"
      }

      Note: The event type must match the repository_dispatch in your GitHub workflow, in our case .github/workflows/build-deploy.yml.

    • Leave other settings as default.

static-nextjs-contentful's People

Contributors

jtiala avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.