Building Blazing-Fast Websites with Next.js & Sanity.io
Hello Everyone!
Table of Contents
01 Intro
02 Setting up the Repo
� 10m pause: Set up your repo!
03 Setting up Sanity.io
10m pause: Set up your Sanity account!�
04 Setting up Next.js
05 Deploying to production on Vercel
What are we building?
Visit demo site at https://headless-workshop.vercel.app/
What is headless architecture?
What is headless architecture?
In headless architecture, the presentation layer is decoupled from the content layer. The content is served from a CMS via an API, and can be accessed by a wide variety of frontend solutions.
A headless platform can be broken down into two parts:�
Benefits of going headless
Headless architecture - a visual journey
Headless architecture - the CMS workflow
Introducing �platforms & tools
Sanity
Next.js
Vercel
Storybook
Let’s get STARTED!
Hands on: Set up the repo [10min]
Workshop starter repo: https://github.com/rangle/headless-workshop
�*Running Sanity Studio won’t work until after we set it up!
[Screenshot of starter Sanity studio]
Setting up Sanity.io
What is a content model?
Types of Content Models
When working in any headless CMS we’re in charge of building the authoring interface. A content model is a way for us to define and “model” that interface. In Sanity, we achieve this by creating a “schema”, which acts as the blueprint for the content model.
Content Model: Page
A page is a content model that mirrors the information needed to put together a single page. It usually contains two different types of content:
Content Model: Page Module
A page module is what's displayed on the page. This content model mirrors the information needed for a single component we want to display on a page. Content authors add page modules to a page and enter data in input fields within the CMS.
Defining a Schema
Content models are defined by creating schemas using Sanity’s specific syntax.
Schema Types: Document vs. Object
Document
Object
In Sanity, we will be using the document and object schema types to define our Page and Page Module content models.
Defining a field
Schema Documentation: https://www.sanity.io/docs/schema-types
Field Examples
Displayed name in Sanity Interface
Renders the toggle input field
Defines the field validation
Renders the text input field
Matching “name” to component and props name
* We’ll revisit this in the next section to learn why this is necessary
Schema Demo
Setting up the Sanity Studio
⚠️ Do not follow instructions to install Studio in the CLI! ⚠️
Your Account
Click to exit. Do not follow instructions to install Studio in CLI.
Accessing the Studio locally
Hands on: Setting up Sanity studio [10m]
✨ See previous 2 slides for detailed instructions!�
Result: You should be able to navigate to localhost:3000/studio and see the Sanity Studio!
Hands on: Requirements for Page schema [20min]
Result: You should be able to see Pages in Sanity Studio and be able to add content to it
*** Reference Schema Documentation: https://www.sanity.io/docs/schema-types
BONUS: Add a custom validation to the slug field to ensure all slugs start with a leading ‘/’
⭐
Title (DONE)
Slug
Sections
Hands on: Requirements for Media Module schema [20min]
BONUS: Customize the preview to feature heading field as title, ‘Media Module' as subtitle and image as media
⭐
***Remember that the name of the schema must match the name of the component and the name of the fields must match the component props.
Result: You should be able to add the Media Module onto a Page in Sanity Studio. You should see the component properly render in the Preview
Hands on: Add some content! [10min]
We want to populate Sanity with some data so we can test it out in the next section. Don’t worry about filling in everything at this point!
* You will not be able to preview this content on the site – YET!
***Make sure to include the leading slashes for slugs!
Querying Data with GROQ
Anatomy of a GROQ query
Select all documents where “_type” is equal to “page”
Only return the “slug.current” value as “slug” in the document object
Select all “page” documents with “slug.current” equal to “/”
Return all values from the document object
Setting up Next.js
Routing
Catch-all vs. Optional catch-all
Only included in optional catch-all
Creating dynamic routes
Querying data from Sanity
Fetching page content
Page builder
Breaking down the buildComponent function
Import all modules that’s used in the CMS
Null checking “_type” property
Selecting the correct component based on “_type” value
Null checking if no valid component is selected
Returning the selected component with all other JSON value passed into it as props
Hands-on: Setup page template [20min]
Result: You should now be able to navigate to all three of your pages. You should also be able to console.log the page prop and see the Sanity data you inputted
Hands-on: Render components on the page [10min]
Results: You should be able to see your complete page with all the content and sections you have inputted in Sanity!
Deploying to Production
Sign-up and creating a new project in Vercel
Setting up ENV variables
Setting up a webhook
Setting up a webhook
Hands-on: Deploy to production! [10min]
Results: You should now have a deployed link to your project! You should also be able to see updates in your production link when you publish changes on Sanity.
Hands-on: Customize the website [10min]
Using the remainder of the time left in the workshop (or on your own time after the workshop) customize the website to make it your own. Consider doing any of these things:
Thank you!