Giter Site home page Giter Site logo

superpow-me's Introduction

A statically generated blog example using Next.js and Sanity

This example showcases Next.js's Static Generation feature using Sanity as the data source.

Demo

Deploy your own

Once you have access to the environment variables you'll need, deploy the example using Vercel:

Deploy with Vercel

Related examples

How to use

Execute create-next-app with npm or Yarn to bootstrap the example:

npx create-next-app --example cms-sanity cms-sanity-app
# or
yarn create next-app --example cms-sanity cms-sanity-app

Configuration

Step 1. Create an account and a project on Sanity

First, create an account on Sanity.

After creating an account, install the Sanity cli from npm npm i -g @sanity/cli.

Step 2. Create a new Sanity project

In a separate folder run sanity init to initialize a new studio project.

This will be where we manage our data.

When going through the init phase make sure to select Yes to the Use the default dataset configuration step and select Clean project with no predefined schemas for the Select project template step.

Step 3. Generate an API token

Log into https://manage.sanity.io/ and choose the project you just created. Then from Settings, select API, then click Add New Token and create a token with the Read permission.

Step 4. Set up environment variables

Copy the .env.local.example file in this directory to .env.local (which will be ignored by Git):

cp .env.local.example .env.local

Then set each variable on .env.local:

  • NEXT_PUBLIC_SANITY_PROJECT_ID should be the projectId value from the sanity.json file created in step 2.
  • SANITY_API_TOKEN should be the API token generated in the previous step.
  • SANITY_PREVIEW_SECRET can be any random string (but avoid spaces), like MY_SECRET - this is used for Preview Mode.

Your .env.local file should look like this:

NEXT_PUBLIC_SANITY_PROJECT_ID=...
SANITY_API_TOKEN=...
SANITY_PREVIEW_SECRET=...

Step 5. Prepare project for previewing

Go to https://www.sanity.io/docs/preview-content-on-site and follow the three steps on that page. It should be done inside the studio project generated in Step 2.

When you get to the second step about creating a file called resolveProductionUrl.js, copy the following instead:

const previewSecret = 'MY_SECRET' // Copy the string you used for SANITY_PREVIEW_SECRET
const projectUrl = 'http://localhost:3000'

export default function resolveProductionUrl(document) {
  return `${projectUrl}/api/preview?secret=${previewSecret}&slug=${document.slug.current}`
}

Step 6. Copy the schema file

After initializing your Sanity studio project there should be a schemas folder.

Replace the contents of schema.js in the Sanity studio project directory with ./schemas/schema.js in this example directory. This will set up the schema we’ll use this for this example.

Step 7. Populate Content

To add some content go to your Sanity studio project directory and run sanity start.

After the project has started and you have navigated to the URL given in the terminal, select Author and create a new record.

  • You just need 1 Author record.
  • Use dummy data for the text.
  • For the image, you can download one from Unsplash.

Next, select Post and create a new record.

  • We recommend creating at least 2 Post records.
  • Use dummy data for the text.
  • You can write markdown for the Content field.
  • For the images, you can download ones from Unsplash.
  • Pick the Author you created earlier.

Important: For each post record, you need to click Publish after saving. If not, the post will be in the draft state.

Step 8. Run Next.js in development mode

npm install
npm run dev

# or

yarn install
yarn dev

Your blog should be up and running on http://localhost:3000! If it doesn't work, post on GitHub discussions.

Step 9. Try preview mode

On Sanity, go to one of the posts you've created and:

  • Update the title. For example, you can add [Draft] in front of the title.
  • As you edit the document it will be saved as a draft, but DO NOT click Publish. By doing this, the post will be in the draft state.

Now, if you go to the post page on localhost, you won't see the updated title. However, if you use the Preview Mode, you'll be able to see the change (Documentation).

To view the preview, go to the post edit page on Sanity, click the three dots above the document and select Open preview (see the instruction here)

You should now be able to see the updated title. To exit Preview Mode, you can click on "Click here to exit preview mode" at the top.

Step 10. Deploy on Vercel

You can deploy this app to the cloud with Vercel (Documentation).

Deploy Your Local Project

To deploy your local project to Vercel, push it to GitHub/GitLab/Bitbucket and import to Vercel.

Important: When you import your project on Vercel, make sure to click on Environment Variables and set them to match your .env.local file.

Deploy from Our Template

Alternatively, you can deploy using our template by clicking on the Deploy button below.

Deploy with Vercel

superpow-me's People

Contributors

juangrafista avatar

Watchers

 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.