From e7b41fdd9ae09884d76e8b1239f631c05917e03a Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Mon, 9 Mar 2020 12:09:23 -0500 Subject: [PATCH 01/23] Add start of Sanity CMS example --- examples/cms-sanity/.env.example | 2 + examples/cms-sanity/.gitignore | 2 + examples/cms-sanity/README.md | 212 ++++++++++++++++++ examples/cms-sanity/components/alert.js | 42 ++++ examples/cms-sanity/components/avatar.js | 8 + examples/cms-sanity/components/container.js | 3 + examples/cms-sanity/components/cover-image.js | 27 +++ examples/cms-sanity/components/date.js | 6 + examples/cms-sanity/components/footer.js | 30 +++ examples/cms-sanity/components/header.js | 12 + examples/cms-sanity/components/hero-post.js | 41 ++++ examples/cms-sanity/components/intro.js | 28 +++ examples/cms-sanity/components/layout.js | 16 ++ .../components/markdown-styles.module.css | 18 ++ examples/cms-sanity/components/meta.js | 42 ++++ .../cms-sanity/components/more-stories.js | 24 ++ examples/cms-sanity/components/post-body.js | 12 + examples/cms-sanity/components/post-header.js | 29 +++ .../cms-sanity/components/post-preview.js | 35 +++ examples/cms-sanity/components/post-title.js | 7 + .../components/section-separator.js | 3 + examples/cms-sanity/lib/api.js | 42 ++++ examples/cms-sanity/lib/constants.js | 5 + examples/cms-sanity/lib/sanity.js | 11 + examples/cms-sanity/next.config.js | 8 + examples/cms-sanity/now.json | 11 + examples/cms-sanity/package.json | 25 +++ examples/cms-sanity/pages/_app.js | 7 + examples/cms-sanity/pages/_document.js | 15 ++ examples/cms-sanity/pages/api/exit-preview.js | 8 + examples/cms-sanity/pages/api/preview.js | 28 +++ examples/cms-sanity/pages/index.js | 44 ++++ examples/cms-sanity/pages/posts/[slug].js | 75 +++++++ examples/cms-sanity/postcss.config.js | 18 ++ .../public/favicon/android-chrome-192x192.png | Bin 0 -> 4795 bytes .../public/favicon/android-chrome-512x512.png | Bin 0 -> 14640 bytes .../public/favicon/apple-touch-icon.png | Bin 0 -> 1327 bytes .../public/favicon/browserconfig.xml | 9 + .../public/favicon/favicon-16x16.png | Bin 0 -> 595 bytes .../public/favicon/favicon-32x32.png | Bin 0 -> 880 bytes .../cms-sanity/public/favicon/favicon.ico | Bin 0 -> 15086 bytes .../public/favicon/mstile-150x150.png | Bin 0 -> 3567 bytes .../public/favicon/safari-pinned-tab.svg | 33 +++ .../public/favicon/site.webmanifest | 19 ++ examples/cms-sanity/public/images/author.jpg | Bin 0 -> 40904 bytes examples/cms-sanity/public/images/image.jpg | Bin 0 -> 297961 bytes examples/cms-sanity/styles/index.css | 5 + examples/cms-sanity/tailwind.config.js | 32 +++ 48 files changed, 994 insertions(+) create mode 100644 examples/cms-sanity/.env.example create mode 100644 examples/cms-sanity/.gitignore create mode 100644 examples/cms-sanity/README.md create mode 100644 examples/cms-sanity/components/alert.js create mode 100644 examples/cms-sanity/components/avatar.js create mode 100644 examples/cms-sanity/components/container.js create mode 100644 examples/cms-sanity/components/cover-image.js create mode 100644 examples/cms-sanity/components/date.js create mode 100644 examples/cms-sanity/components/footer.js create mode 100644 examples/cms-sanity/components/header.js create mode 100644 examples/cms-sanity/components/hero-post.js create mode 100644 examples/cms-sanity/components/intro.js create mode 100644 examples/cms-sanity/components/layout.js create mode 100644 examples/cms-sanity/components/markdown-styles.module.css create mode 100644 examples/cms-sanity/components/meta.js create mode 100644 examples/cms-sanity/components/more-stories.js create mode 100644 examples/cms-sanity/components/post-body.js create mode 100644 examples/cms-sanity/components/post-header.js create mode 100644 examples/cms-sanity/components/post-preview.js create mode 100644 examples/cms-sanity/components/post-title.js create mode 100644 examples/cms-sanity/components/section-separator.js create mode 100644 examples/cms-sanity/lib/api.js create mode 100644 examples/cms-sanity/lib/constants.js create mode 100644 examples/cms-sanity/lib/sanity.js create mode 100644 examples/cms-sanity/next.config.js create mode 100644 examples/cms-sanity/now.json create mode 100644 examples/cms-sanity/package.json create mode 100644 examples/cms-sanity/pages/_app.js create mode 100644 examples/cms-sanity/pages/_document.js create mode 100644 examples/cms-sanity/pages/api/exit-preview.js create mode 100644 examples/cms-sanity/pages/api/preview.js create mode 100644 examples/cms-sanity/pages/index.js create mode 100644 examples/cms-sanity/pages/posts/[slug].js create mode 100644 examples/cms-sanity/postcss.config.js create mode 100644 examples/cms-sanity/public/favicon/android-chrome-192x192.png create mode 100644 examples/cms-sanity/public/favicon/android-chrome-512x512.png create mode 100644 examples/cms-sanity/public/favicon/apple-touch-icon.png create mode 100644 examples/cms-sanity/public/favicon/browserconfig.xml create mode 100644 examples/cms-sanity/public/favicon/favicon-16x16.png create mode 100644 examples/cms-sanity/public/favicon/favicon-32x32.png create mode 100644 examples/cms-sanity/public/favicon/favicon.ico create mode 100644 examples/cms-sanity/public/favicon/mstile-150x150.png create mode 100644 examples/cms-sanity/public/favicon/safari-pinned-tab.svg create mode 100644 examples/cms-sanity/public/favicon/site.webmanifest create mode 100644 examples/cms-sanity/public/images/author.jpg create mode 100644 examples/cms-sanity/public/images/image.jpg create mode 100644 examples/cms-sanity/styles/index.css create mode 100644 examples/cms-sanity/tailwind.config.js diff --git a/examples/cms-sanity/.env.example b/examples/cms-sanity/.env.example new file mode 100644 index 0000000000000..63c2bd0486a7a --- /dev/null +++ b/examples/cms-sanity/.env.example @@ -0,0 +1,2 @@ +NEXT_EXAMPLE_CMS_SANITY_API_TOKEN= +NEXT_EXAMPLE_CMS_SANITY_PREVIEW_SECRET= \ No newline at end of file diff --git a/examples/cms-sanity/.gitignore b/examples/cms-sanity/.gitignore new file mode 100644 index 0000000000000..5900fa96e8d3e --- /dev/null +++ b/examples/cms-sanity/.gitignore @@ -0,0 +1,2 @@ +.env +.now \ No newline at end of file diff --git a/examples/cms-sanity/README.md b/examples/cms-sanity/README.md new file mode 100644 index 0000000000000..ddc4b4b3f1ba3 --- /dev/null +++ b/examples/cms-sanity/README.md @@ -0,0 +1,212 @@ +# A statically generated blog example using Next.js and Sanity + +This example showcases Next.js's [Static Generation](/docs/basic-features/pages.md) feature using [Sanity](https://www.sanity.io/) as the data source. + +## How to use + +### Using `create-next-app` + +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: + +```bash +npm init next-app --example cms-Sanity cms-Sanity-app +# or +yarn create next-app --example cms-Sanity cms-Sanity-app +``` + +### Download manually + +Download the example: + +```bash +curl https://codeload.github.com/zeit/next.js/tar.gz/canary | tar -xz --strip=2 next.js-canary/examples/cms-Sanity +cd cms-sanity +``` + +## Configuration + +### Step 1. Create an account and a project on Sanity + +First, [create an account on Sanity](https://sanity.io). + +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. + +After initializing get the `projectId` and `dataset` values from the `sanity.json` file that was generated and add them to `lib/sanity.js` in the example. + +### Step 3. Create an `Author` schema + +In your Sanity studio project open `schemas/schema.js` and add the following schema that will be used for authors inside of `schemaTypes.concat([])` + +```js +{ + name: 'author', + type: 'document', + title: 'Author', + fields: [ + { + name: 'name', + title: 'Name', + type: 'string' + }, + { + name: 'picture', + title: 'Picture', + type: 'image' + }, + ] +} +``` + +### Step 4. Create a `Post` schema + +In the same file that we edited above `schemas/schema.js` add the following schema that will be used for posts. + +```js +{ + name: 'post', + type: 'document', + title: 'Post', + fields: [ + { + name: 'title', + title: 'Title', + type: 'string' + }, + { + name: 'content', + title: 'Content', + type: 'array', + of: [{type: 'block'}] + }, + { + name: 'excerpt', + title: 'Excerpt', + type: 'string' + }, + { + name: 'coverImage', + title: 'Cover Image', + type: 'image', + }, + { + name: 'date', + title: 'Date', + type: 'datetime', + }, + { + name: 'author', + title: 'Author', + type: 'reference', + to: [{ type: 'author' }] + }, + { + name: 'slug', + title: 'Slug', + type: 'slug' + } + ] +} +``` + +### Step 4. Populate Content + +To add some content go to your Sanity studio project where we added the schema types and run `sanity start`. + +After the project has started and you have navigated to the provided URL, 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](https://unsplash.com/). + +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](https://unsplash.com/). +- 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 5. Set up environment variables + +Go to the **Settings** menu at the top and click **API tokens**. + +Then click **Read-only API token** and copy the token. + +Next, copy the `.env.example` file in this directory to `.env` (which will be ignored by Git): + +```bash +cp .env.example .env +``` + +Then set each variable on `.env`: + +- `NEXT_EXAMPLE_CMS_Sanity_API_TOKEN` should be the API token you just copied. +- `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). + +Your `.env` file should look like this: + +```bash +NEXT_EXAMPLE_CMS_Sanity_API_TOKEN=... +NEXT_EXAMPLE_CMS_Sanity_PREVIEW_SECRET=... +``` + +### Step 6. Run Next.js in development mode + +```bash +npm install +npm run dev + +# or + +yarn install +yarn dev +``` + +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). + +### Step 7. 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. +- Click **Save**, but **DO NOT** click **Publish**. By doing this, the post will be in the draft state. + +(If it doesn't become draft, you need to go to the model settings for `Post`, go to **Additional Settings**, and turn on **Enable draft/published system**.) + +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)). + +To enable the Preview Mode, go to this URL: + +``` +http://localhost:3000/api/preview?secret=&slug= +``` + +- `` should be the string you entered for `NEXT_EXAMPLE_CMS_Sanity_PREVIEW_SECRET`. +- `` should be the post's `slug` attribute (you can check on Sanity). + +You should now be able to see the updated title. To exit the preview mode, you can click **Click here to exit preview mode** at the top. + +### Step 8. Deploy on ZEIT Now + +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)). + +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)). + +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 `` and `` with the corresponding strings in `.env`. + +``` +now secrets add next_example_cms_Sanity_api_token +now secrets add next_example_cms_Sanity_preview_secret +``` + +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. diff --git a/examples/cms-sanity/components/alert.js b/examples/cms-sanity/components/alert.js new file mode 100644 index 0000000000000..3530e66e59c8d --- /dev/null +++ b/examples/cms-sanity/components/alert.js @@ -0,0 +1,42 @@ +import Container from './container' +import cn from 'classnames' +import { EXAMPLE_PATH } from '../lib/constants' + +export default function Alert({ preview }) { + return ( +
+ +
+ {preview ? ( + <> + This is page is a preview.{' '} + + Click here + {' '} + to exit preview mode. + + ) : ( + <> + The source code for this blog is{' '} + + available on GitHub + + . + + )} +
+
+
+ ) +} diff --git a/examples/cms-sanity/components/avatar.js b/examples/cms-sanity/components/avatar.js new file mode 100644 index 0000000000000..4b03b5f0ed09e --- /dev/null +++ b/examples/cms-sanity/components/avatar.js @@ -0,0 +1,8 @@ +export default function Avatar({ name, url }) { + return ( +
+ {name} +
{name}
+
+ ) +} diff --git a/examples/cms-sanity/components/container.js b/examples/cms-sanity/components/container.js new file mode 100644 index 0000000000000..fc1c29dfb0747 --- /dev/null +++ b/examples/cms-sanity/components/container.js @@ -0,0 +1,3 @@ +export default function Container({ children }) { + return
{children}
+} diff --git a/examples/cms-sanity/components/cover-image.js b/examples/cms-sanity/components/cover-image.js new file mode 100644 index 0000000000000..5d9406ebf995b --- /dev/null +++ b/examples/cms-sanity/components/cover-image.js @@ -0,0 +1,27 @@ +// import { Image } from 'react-datocms' +// import cn from 'classnames' +import Link from 'next/link' + +export default function CoverImage({ title, responsiveImage, slug }) { + const image = null + // + return ( +
+ {slug ? ( + + {image} + + ) : ( + image + )} +
+ ) +} diff --git a/examples/cms-sanity/components/date.js b/examples/cms-sanity/components/date.js new file mode 100644 index 0000000000000..eac5681378bfd --- /dev/null +++ b/examples/cms-sanity/components/date.js @@ -0,0 +1,6 @@ +import { parseISO, format } from 'date-fns' + +export default function Date({ dateString }) { + const date = parseISO(dateString) + return +} diff --git a/examples/cms-sanity/components/footer.js b/examples/cms-sanity/components/footer.js new file mode 100644 index 0000000000000..dbde8ff306efd --- /dev/null +++ b/examples/cms-sanity/components/footer.js @@ -0,0 +1,30 @@ +import Container from './container' +import { EXAMPLE_PATH } from '../lib/constants' + +export default function Footer() { + return ( + + ) +} diff --git a/examples/cms-sanity/components/header.js b/examples/cms-sanity/components/header.js new file mode 100644 index 0000000000000..562e7e3eebb6a --- /dev/null +++ b/examples/cms-sanity/components/header.js @@ -0,0 +1,12 @@ +import Link from 'next/link' + +export default function Header() { + return ( +

+ + Blog + + . +

+ ) +} diff --git a/examples/cms-sanity/components/hero-post.js b/examples/cms-sanity/components/hero-post.js new file mode 100644 index 0000000000000..dbe5acac7e3c4 --- /dev/null +++ b/examples/cms-sanity/components/hero-post.js @@ -0,0 +1,41 @@ +import Avatar from '../components/avatar' +import Date from '../components/date' +// import CoverImage from '../components/cover-image' +import Link from 'next/link' + +export default function HeroPost({ + title, + coverImage, + date, + excerpt, + author, + slug, +}) { + return ( +
+
+ {/* */} +
+
+
+

+ + {title} + +

+
+ +
+
+
+

{excerpt}

+ +
+
+
+ ) +} diff --git a/examples/cms-sanity/components/intro.js b/examples/cms-sanity/components/intro.js new file mode 100644 index 0000000000000..5931b3c5961bd --- /dev/null +++ b/examples/cms-sanity/components/intro.js @@ -0,0 +1,28 @@ +import { CMS_NAME, CMS_URL } from '../lib/constants' + +export default function Intro() { + return ( +
+

+ Blog. +

+

+ A statically generated blog example using{' '} + + Next.js + {' '} + and{' '} + + {CMS_NAME} + + . +

+
+ ) +} diff --git a/examples/cms-sanity/components/layout.js b/examples/cms-sanity/components/layout.js new file mode 100644 index 0000000000000..99d95353131e0 --- /dev/null +++ b/examples/cms-sanity/components/layout.js @@ -0,0 +1,16 @@ +import Alert from '../components/alert' +import Footer from '../components/footer' +import Meta from '../components/meta' + +export default function Layout({ preview, children }) { + return ( + <> + +
+ +
{children}
+
+