Coder Social home page Coder Social logo

contentful / template-blog-webapp-nextjs Goto Github PK

View Code? Open in Web Editor NEW
74.0 17.0 87.0 1.66 MB

Next.js blog starter template

Home Page: https://blog.templates.contentful.com/?referrer=github

License: MIT License

Shell 0.99% JavaScript 6.76% TypeScript 91.77% CSS 0.49%
contentful graphql nextjs typescript blog react tailwindcss starter-templates

template-blog-webapp-nextjs's Introduction

Contentful Blog Starter Template

A Blog Starter Template powered by Next.js & Contentful, pre-designed with optimized & adjustable pages, components, and data management.

The homepage of the Blog Starter Template

$~$

What is Contentful?

Contentful provides content infrastructure for digital teams to power websites, apps, and devices. Unlike a CMS, Contentful was built to integrate with the modern software stack. It offers a central hub for structured content, powerful management, and delivery APIs, and a customizable web app that enables developers and content creators to ship their products faster.

$~$

DISCLAIMER ⚠️

The Starter Templates experience is currently only available to new users.

To benefit from this experience, please follow this link to create a new account and select the template to install: https://www.contentful.com/starter-templates/nextjs-blog/sign-up/?action=create_starter_template.

Alternatively, to immediately start the auto installation of this template after creating a new account, please follow this link: https://www.contentful.com/starter-templates/nextjs-blog/sign-up/?action=create_starter_template&template_name=blog.

$~$

Begin your journey with Contentful and the Blog Starter Template

Follow this guide to understand the relationship between Contentful and the Starter Template source code through guided steps:

  • Entry editing, and updates preview in the Starter Template application (online/locally)
  • Content type editing in the Contentful web app, as well as in the Starter Template's code

$~$

Features

  • Composable content through powerful & flexible content modeling.
  • Localization ready.
  • SEO ready.
  • Incremental Static Regeneration with Next.js1.
  • Generation of GraphQL2 typed code (schema, and types), in sync with the content types through graphql-codegen3.
  • Enhanced Developer Experience with TypeScript4.

$~$

Getting started

To get started, read the following guidelines.

$~$

Environment variables

In order to authenticate the requests to the Contentful APIs, the following values are necessary:

Rename the .env.example file to .env and add the necessary values.

$~$

Dependencies

To install the necessary dependencies, run:

yarn

Run the Starter Template in development mode

yarn dev

The Starter Template should be up and running on http://localhost:3000.

All necessary dependencies are installed under node_modules and any necessary tools can be accessed via npm scripts.

$~$

Development

Node

It is recommended to use the Node version listed in the .nvmrc file, we recommend using nvm to easily switch between Node versions.

$~$

Husky & git hooks

This repository makes use of Husky to enforce commit hooks.

The config for both the pre-commit and pre-push hooks can be found in the .husky folder, located in the root of the project.


Pre-commit

Before allowing a commit, we require a successful result from the TypeScript compiler (tsc) and our lint-staged script will be run.

This ensures all ESLint and Prettier rules are enforced on the files that are staged to be committed.

The tsc command is run separately from the lint-staged step because we require the Typescript compiler to sample all files.

This is important to ensure that no deviating types were introduced by the codegen for example.


Pre-push

The same two tasks are run for pre-push and for pre-commit.


Overriding the Husky git hooks

In case of wanting to bypass the pre-commit or pre-push hooks, pass a --noVerify flag to your Git commands.

⚠️ Make sure you only use this if you know why you're using it. ⚠️

$~$

Contentful API & GraphQL

This project makes use of Contentful's GraphQL API.

API calls made to the Contentful GraphQL endpoint are made through graphql-request.

The types are generated from the .graphql files located in the /lib/graphql/ directory:

  1. lib/graphql/[fileName].graphql is detected by the codegen
  2. lib/__generated/sdk.ts is generated
  3. Within the generated file, their types and a new getSdk function are generated
  4. The getSdk function can now be imported and used within the getStaticProps in the pages files

$~$

GraphQL & code generation

We use graphql-codegen to generate a type-safe API client, utilizing GraphQLClient as the "client".


Commands

In order to (re-)generate the GraphQL schema, types and sdk, please use either of the following commands:

  • yarn graphql-codegen:generate generates a schema, types and code to fetch data from the Contentful APIs
  • yarn graphql-codegen:watch similar to the generate command, but it runs as a watch task which will rerun the steps when changes are made in the .graphql files

The first steps of the codegen generate files that contain the GraphQL schema and matching TypeScript types. All these files are located in the src/lib/graphql folder. They're generated to the src/lib/__generated folder and ought to be committed once altered/added to the repository.

The TS types for these files are generated in the same location, in a __generated folder and like the other files ought to be committed.


Configuration

The configuration for the codegen can be found in codegen.ts, located in the root of the project.

$~$


$~$

Deployment

The Starter Template can be deployed to your hosting provider of choice.

We offer integrations with Vercel and Netlify to speed up the process by clicking one of the deploy buttons below. The GitHub repository and the necessary environment variables keys are pre-configured in the hosting provider space.

Vercel Netlify
Deploy with Vercel Deploy to Netlify Button
Environment variables docs Environment variables docs

Make sure to add the necessary environment variables values to the hosting provider environment variables.


Content preview

Once you have the Starter Template deployed on your hosting provider, you can update the Content preview URL in your space settings.

You can follow our guide to learn how to do so: https://www.contentful.com/help/setup-content-preview.

For the live preview the basic field tagging for the inspector mode and live updates are already implemented. For custom components, you can find the instructions at our guide.

Adjustments in code

  1. Set a unique value for process.env.CONTENTFUL_PREVIEW_SECRET in your environment variables. This value should be kept secret and only known to the API route and the CMS.
  2. Configure the entry preview URLs in Contentful to match the draft API route's URL structure. This can be done in the Contentful web interface under "Settings" for each content type. For more information see: https://www.contentful.com/help/setup-content-preview/#preview-content-in-your-online-environment
  3. The draft mode API route is already written in the app and can be found in pages/api/draft.page.tsx. This route checks for a valid secret and slug before redirecting to the corresponding page*.
  4. To disable draft mode, navigate to the /api/disable-draft route. This route already exists in the app and can be found in pages/api/disable-draft.page.tsx.

*The slug field is optional; When not passed we redirect the page to the root of the domain.

Adjustments in Contentful

  1. Next, you will need to configure your Contentful space to use the correct preview URLs. To do this, go to the "Settings" section of your space, and click on the "Content Preview" tab. From here, you can configure the preview URLs for each of your content models.
  2. Edit all content models that need a preview url. We usually expect that to only be the models prefixed with 📄 page -.
  3. Add a new URL with the following format: https://<your-site>/api/draft?secret=<token>&slug={entry.fields.slug}. Make sure to replace <your-site> with the URL of your Next.js site, and <token> with the value of process.env.CONTENTFUL_PREVIEW_SECRET. Optionally, a locale parameter can be passed.
  4. Now, when you view an unpublished entry in Contentful, you should see a "Preview" button that will take you to the preview URL for that entry. Clicking this button should show you a preview of the entry on your Next.js site, using the draft API route that we set up earlier.

Exiting the Content Preview

To disable draft mode, navigate to the /api/disable-draft route. This route already exists in the app and can be found in pages/api/disable-draft.page.tsx.

$~$


$~$

Support

If you have a problem with this Starter Template, post a message in our Contentful Community Slack.

Can't find your answer there? You can file a feedback issue through this template.

If you have other problems with Contentful not related to the Starter Template, you can contact the Customer Support.

$~$

Contributing

See CONTRIBUTING.md.

$~$

License

MIT License, see LICENSE.

$~$

Footnotes

  1. Next.js docs

  2. GraphQL docs

  3. graphql-codegen

  4. TypeScript

template-blog-webapp-nextjs's People

Contributors

adrianlthomas avatar cf-allstar[bot] avatar cf-remylenoir avatar chrishelgert avatar contentfulcormac avatar denkristoffer avatar dependabot[bot] avatar karankohli-cf avatar mattvanvoorst-contentful avatar mayagillilan avatar missating avatar roosterhack avatar stephanleece avatar wynn5a avatar yvesrijckaert avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

template-blog-webapp-nextjs's Issues

💬 Feedback - Fragment Generation in graphql folder

Template feedback

Can you elaborate on how the .graphql fragments were generated originally in the lib/graphql folder? I'm a bit confused where to find the fragment info when a content model gets updated. These will manually need to be updated in that case, correct?

🐛 Bug - type declaration not found in generated sdk

Bug report

Summary

when generating the sdk.ts using yarn graphql-codegen:generate, Cannot find module 'graphql-request/dist/types.dom' or its corresponding type declarations.ts(2307) error in sdk.ts:2

Environment

node version: 18.17.0
yarn version: 1.22.19

Steps to reproduce

yarn graphql-codegen:generate

Expected results

working SDK

Actual results

error in sdk.ts file. Cannot find module 'graphql-request/dist/types.dom' or its corresponding type declarations.ts(2307)

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.