├── .gitignore
├── public
├── favicon.ico
├── nextjs-logo.svg
└── sanity-logo.svg
├── utils
└── imageUrlFor.js
├── lib
└── sanity.js
├── package.json
├── styles
└── list.js
├── .github
└── workflows
│ ├── node.js.yml
│ └── tnode.js.yml
├── README.md
├── pages
├── people.js
├── index.js
├── person
│ └── [id].js
└── movie
│ └── [id].js
└── components
├── GithubCorner.js
└── Layout.js
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | .next
--------------------------------------------------------------------------------
/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sanity-io/example-frontend-next-js/HEAD/public/favicon.ico
--------------------------------------------------------------------------------
/utils/imageUrlFor.js:
--------------------------------------------------------------------------------
1 | import sanity from "../lib/sanity";
2 | import imageUrlBuilder from "@sanity/image-url";
3 |
4 | const imageBuilder = imageUrlBuilder(sanity);
5 |
6 | const imageUrlFor = source => imageBuilder.image(source);
7 |
8 | export default imageUrlFor;
9 |
--------------------------------------------------------------------------------
/lib/sanity.js:
--------------------------------------------------------------------------------
1 | import sanityClient from "@sanity/client";
2 |
3 | export default sanityClient({
4 | // Find your project ID and dataset in `sanity.json` in your studio project
5 | projectId: "zp7mbokg",
6 | dataset: "production",
7 | useCdn: true
8 | // useCdn == true gives fast, cheap responses using a globally distributed cache.
9 | // Set this to false if your application require the freshest possible
10 | // data always (potentially slightly slower and a bit more expensive).
11 | });
12 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "example-frontend-next-js",
3 | "description": "Sanity + Next.js frontend example",
4 | "private": true,
5 | "dependencies": {
6 | "@sanity/block-content-to-react": "^1.3.12",
7 | "@sanity/client": "^0.132.10",
8 | "@sanity/image-url": "^0.132.8",
9 | "next": "^9.3.1",
10 | "react": "^16.13.0",
11 | "react-dom": "^16.13.0"
12 | },
13 | "scripts": {
14 | "dev": "next",
15 | "build": "next build",
16 | "start": "next start"
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/styles/list.js:
--------------------------------------------------------------------------------
1 | /* styles.js */
2 | import css from "styled-jsx/css";
3 |
4 | export default css`
5 | .list {
6 | display: grid;
7 | margin: 0;
8 | padding: 0;
9 | grid-gap: 1rem;
10 | grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
11 | }
12 |
13 | .list > li {
14 | display: block;
15 | margin: 0;
16 | padding: 0;
17 | display: flex;
18 | align-items: stretch;
19 | }
20 |
21 | .list a {
22 | text-decoration: none;
23 | display: block;
24 | flex-grow: 1;
25 | color: #333;
26 | }
27 |
28 | .list h3 {
29 | margin: 0;
30 | padding: 0;
31 | line-height: 1em;
32 | }
33 |
34 | .list img {
35 | display: block;
36 | height: auto;
37 | width: 100%;
38 | margin-right: 0.5rem;
39 | }
40 |
41 | .list .noImage {
42 | border: 1px solid red;
43 | }
44 |
45 | .link {
46 | cursor: pointer;
47 | }
48 | `;
49 |
--------------------------------------------------------------------------------
/.github/workflows/node.js.yml:
--------------------------------------------------------------------------------
1 | # This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3 |
4 | name: Node.js CI
5 |
6 | on:
7 | push:
8 | branches: [ "master" ]
9 | pull_request:
10 | branches: [ "master" ]
11 |
12 | jobs:
13 | build:
14 |
15 | runs-on: ubuntu-latest
16 |
17 | strategy:
18 | matrix:
19 | node-version: [12.x, 14.x, 16.x]
20 | # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
21 |
22 | steps:
23 | - uses: actions/checkout@v3
24 | - name: Use Node.js ${{ matrix.node-version }}
25 | uses: actions/setup-node@v3
26 | with:
27 | node-version: ${{ matrix.node-version }}
28 | cache: 'npm'
29 | - run: npm ci
30 | - run: npm run build --if-present
31 | - run: npm test --if-present
32 |
--------------------------------------------------------------------------------
/.github/workflows/tnode.js.yml:
--------------------------------------------------------------------------------
1 | # This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3 |
4 | name: Node.js CI
5 |
6 | on:
7 | push:
8 | branches: [ "master" ]
9 | pull_request:
10 | branches: [ "master" ]
11 |
12 | jobs:
13 | build:
14 |
15 | runs-on: ubuntu-latest
16 |
17 | strategy:
18 | matrix:
19 | node-version: [12.x, 14.x, 16.x]
20 | # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
21 |
22 | steps:
23 | - uses: actions/checkout@v3
24 | - name: Use Node.js ${{ matrix.node-version }}
25 | uses: actions/setup-node@v3
26 | with:
27 | node-version: ${{ matrix.node-version }}
28 | cache: 'npm'
29 | - run: npm ci
30 | - run: npm run build --if-present
31 | - run: npm test --if-present
32 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Sanity + Next.js frontend example
2 |
3 | DEMO 👉 https://example-frontend-next-js.sanity-io.now.sh
4 |
5 | This is an example [Sanity](https://www.sanity.io/) powered frontend for the movie dataset using [Next.js](https://github.com/zeit/next.js/).
6 |
7 | ## Prerequisites
8 |
9 | You will need [Node.js](https://nodejs.org) version 8.0 or greater installed on your system.
10 |
11 | ## Setup
12 |
13 | Get the code by either cloning this repository using git
14 |
15 | ```
16 | git clone https://github.com/sanity-io/example-frontend-next-js.git
17 | ```
18 |
19 | ... or [downloading source code](https://github.com/sanity-io/example-frontend-next-js/archive/master.zip) code as a zip archive.
20 |
21 | Once downloaded, open the terminal in the project directory, and install dependencies with:
22 |
23 | ```
24 | npm install
25 | ```
26 |
27 | If you're running your own Sanity project with the example movie dataset, go to `lib/sanity.js` and change the following lines:
28 |
29 | ```
30 | projectId: 'YOUR_PROJECT_ID',
31 | dataset: 'NAME_OF_YOUR_DATASET',
32 | ```
33 |
34 | You can locate the ID of your project in the header of the [management page for your project](https://manage.sanity.io/).
35 |
36 | You also need to enable `localhost:3000` in your CORS Origins settings! Either through the [management page](https://manage.sanity.io/) under `settings` or by running the below in the project folder you set up with `sanity init`:
37 |
38 | ```
39 | sanity cors add http://localhost:3000
40 | ```
41 |
42 | Then start the example app with:
43 |
44 | ```
45 | npm run dev
46 | ```
47 |
48 | The app should now be up and running at http://localhost:3000 🚀
49 |
--------------------------------------------------------------------------------
/pages/people.js:
--------------------------------------------------------------------------------
1 | import React from "react";
2 | import Link from "next/link";
3 | import Layout from "../components/Layout";
4 | import sanity from "../lib/sanity";
5 | import listStyles from "../styles/list";
6 | import imageUrlFor from "../utils/imageUrlFor";
7 |
8 | const query = `*[_type == "person"] {
9 | _id,
10 | name,
11 | image,
12 | "imageAspect": image.asset->.metadata.dimensions.aspectRatio,
13 | }[0...50]
14 | `;
15 |
16 | const People = ({ people }) => {
17 | return (
18 |
21 | {people.map(person => (
22 |
38 |
31 | )}
32 |
{person.name}
33 |
34 |
35 |