|
| 1 | +# A statically generated blog example using Next.js and Sanity |
| 2 | + |
| 3 | +This example showcases Next.js's [Static Generation](https://nextjs.org/docs/basic-features/pages) feature using [Sanity](https://www.sanity.io/) as the data source. |
| 4 | + |
| 5 | +## Demo |
| 6 | + |
| 7 | +### [https://next-blog-sanity.now.sh/](https://next-blog-sanity.now.sh/) |
| 8 | + |
| 9 | +### Related examples |
| 10 | + |
| 11 | +- [Blog Starter](/examples/blog-starter) |
| 12 | +- [DatoCMS](/examples/cms-datocms) |
| 13 | + |
| 14 | +## How to use |
| 15 | + |
| 16 | +### Using `create-next-app` |
| 17 | + |
| 18 | +Execute [`create-next-app`](https://github.com/zeit/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example: |
| 19 | + |
| 20 | +```bash |
| 21 | +npm init next-app --example cms-sanity cms-sanity-app |
| 22 | +# or |
| 23 | +yarn create next-app --example cms-sanity cms-sanity-app |
| 24 | +``` |
| 25 | + |
| 26 | +### Download manually |
| 27 | + |
| 28 | +Download the example: |
| 29 | + |
| 30 | +```bash |
| 31 | +curl https://codeload.github.com/zeit/next.js/tar.gz/canary | tar -xz --strip=2 next.js-canary/examples/cms-sanity |
| 32 | +cd cms-sanity |
| 33 | +``` |
| 34 | + |
| 35 | +## Configuration |
| 36 | + |
| 37 | +### Step 1. Create an account and a project on Sanity |
| 38 | + |
| 39 | +First, [create an account on Sanity](https://sanity.io). |
| 40 | + |
| 41 | +After creating an account, install the Sanity cli from npm `npm i -g @sanity/cli`. |
| 42 | + |
| 43 | +### Step 2. Create a new Sanity project |
| 44 | + |
| 45 | +In a separate folder run `sanity init` to initialize a new studio project. |
| 46 | + |
| 47 | +This will be where we manage our data. |
| 48 | + |
| 49 | +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. |
| 50 | + |
| 51 | +### Step 3. Generate an API token |
| 52 | + |
| 53 | +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. |
| 54 | + |
| 55 | +### Step 4. Set up environment variables |
| 56 | + |
| 57 | +Copy the `.env.example` file in this directory to `.env` (which will be ignored by Git): |
| 58 | + |
| 59 | +```bash |
| 60 | +cp .env.example .env |
| 61 | +``` |
| 62 | + |
| 63 | +Then set each variable on `.env`: |
| 64 | + |
| 65 | +- `NEXT_EXAMPLE_CMS_SANITY_PREVIEW_SECRET` can be any random string (but avoid spaces), like `MY_SECRET` - this is used for [the Preview Mode](/docs/advanced-features/preview-mode.md). |
| 66 | +- `NEXT_EXAMPLE_CMS_SANITY_PROJECT_ID`: Get the `projectId` value from the `sanity.json` file created in step 2. |
| 67 | +- `NEXT_EXAMPLE_CMS_SANITY_API_TOKEN`: Copy the API token generated in the previous step. |
| 68 | + |
| 69 | +Your `.env` file should look like this: |
| 70 | + |
| 71 | +```bash |
| 72 | +NEXT_EXAMPLE_CMS_SANITY_PREVIEW_SECRET=... |
| 73 | +NEXT_EXAMPLE_CMS_SANITY_PROJECT_ID=... |
| 74 | +NEXT_EXAMPLE_CMS_SANITY_API_TOKEN=... |
| 75 | +``` |
| 76 | + |
| 77 | +### Step 5. Prepare project for previewing |
| 78 | + |
| 79 | +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. |
| 80 | + |
| 81 | +When you get to the second step about creating a file called `resolveProductionUrl.js`, copy the following instead: |
| 82 | + |
| 83 | +```js |
| 84 | +const previewSecret = 'MY_SECRET' // Copy the string you used for NEXT_EXAMPLE_CMS_SANITY_PREVIEW_SECRET |
| 85 | +const projectUrl = 'http://localhost:3000' |
| 86 | + |
| 87 | +export default function resolveProductionUrl(document) { |
| 88 | + return `${projectUrl}/api/preview?secret=${previewSecret}&slug=${document.slug.current}` |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +### Step 6. Copy the schema file |
| 93 | + |
| 94 | +After initializing your Sanity studio project there should be a `schemas` folder. |
| 95 | + |
| 96 | +Replace the contents of `schema.js` in the Sanity studio project directory with [`./schemas/schema.js`](./schemas/schema.js) in this example directory. This will set up the schema we’ll use this for this example. |
| 97 | + |
| 98 | +### Step 7. Populate Content |
| 99 | + |
| 100 | +To add some content go to your Sanity studio project directory and run `sanity start`. |
| 101 | + |
| 102 | +After the project has started and you have navigated to the URL given in the terminal, select **Author** and create a new record. |
| 103 | + |
| 104 | +- You just need **1 Author record**. |
| 105 | +- Use dummy data for the text. |
| 106 | +- For the image, you can download one from [Unsplash](https://unsplash.com/). |
| 107 | + |
| 108 | +Next, select **Post** and create a new record. |
| 109 | + |
| 110 | +- We recommend creating at least **2 Post records**. |
| 111 | +- Use dummy data for the text. |
| 112 | +- You can write markdown for the **Content** field. |
| 113 | +- For the images, you can download ones from [Unsplash](https://unsplash.com/). |
| 114 | +- Pick the **Author** you created earlier. |
| 115 | + |
| 116 | +**Important:** For each post record, you need to click **Publish** after saving. If not, the post will be in the draft state. |
| 117 | + |
| 118 | +### Step 8. Run Next.js in development mode |
| 119 | + |
| 120 | +```bash |
| 121 | +npm install |
| 122 | +npm run dev |
| 123 | + |
| 124 | +# or |
| 125 | + |
| 126 | +yarn install |
| 127 | +yarn dev |
| 128 | +``` |
| 129 | + |
| 130 | +Your blog should be up and running on [http://localhost:3000](http://localhost:3000)! If it doesn't work, post on [GitHub discussions](https://github.com/zeit/next.js/discussions). |
| 131 | + |
| 132 | +### Step 9. Try preview mode |
| 133 | + |
| 134 | +On Sanity, go to one of the posts you've created and: |
| 135 | + |
| 136 | +- **Update the title**. For example, you can add `[Draft]` in front of the title. |
| 137 | +- 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. |
| 138 | + |
| 139 | +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](/docs/advanced-features/preview-mode.md)). |
| 140 | + |
| 141 | +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](https://www.sanity.io/docs/preview-content-on-site)) |
| 142 | + |
| 143 | +You should now be able to see the updated title. To exit the preview mode, you can click on _"Click here to exit preview mode"_ at the top. |
| 144 | + |
| 145 | +### Step 10. Deploy on ZEIT Now |
| 146 | + |
| 147 | +You can deploy this app to the cloud with [ZEIT Now](https://zeit.co/import?filter=next.js&utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)). |
| 148 | + |
| 149 | +To deploy on ZEIT Now, you need to set the environment variables with **Now Secrets** using [Now CLI](https://zeit.co/download) ([Documentation](https://zeit.co/docs/now-cli#commands/secrets)). |
| 150 | + |
| 151 | +Install [Now CLI](https://zeit.co/download), log in to your account from the CLI, and run the following commands to add the environment variables. Replace each variable with the corresponding strings in `.env`. |
| 152 | + |
| 153 | +``` |
| 154 | +now secrets add next_example_cms_sanity_preview_secret <NEXT_EXAMPLE_CMS_SANITY_PREVIEW_SECRET> |
| 155 | +now secrets add next_example_cms_sanity_api_token <NEXT_EXAMPLE_CMS_SANITY_API_TOKEN> |
| 156 | +now secrets add next_example_cms_sanity_project_id <NEXT_EXAMPLE_CMS_SANITY_PROJECT_ID> |
| 157 | +``` |
| 158 | + |
| 159 | +Then push the project to GitHub/GitLab/Bitbucket and [import to ZEIT Now](https://zeit.co/import?filter=next.js&utm_source=github&utm_medium=readme&utm_campaign=next-example) to deploy. |
0 commit comments