[!WARNING]
This template has been updated to React Router 7, but will not currently deploy to Vercel, as their build plugin has not yet been updated. Update it yourself to adapt to the server of your choice. It will be updated when Vercel updates the build plugin.
--
Batteries-included React Router 7 website using Vite with Vercel's deployment preset with Visual Editing for interactive live-preview inside Presentation powered by an embedded Sanity Studio.
Sanity Docs | React Router Docs
From the command line, install this project as a React Router template:
npx create-react-router@latest --template SimeonGriggs/sanity-react-router-template
This template comes with an init script, if you choose not to run it, you can run it later with:
npx react-router init
This script should populate your .env
file with the details of a new or existing Sanity project's ID and dataset name, as well as create a session secret and an API Token with Viewer permissions. If the init script has already been deleted, you can easily reset your .env
file by running the following command:
npx sanity@latest init --env
Then, start the development server:
npm run dev
Now open the Studio at localhost:5173/studio and start authoring content!
Your new Sanity project starts with no content. A script is included in this repo using Faker to generate content to start with.
[!WARNING]
This script will delete existing content in the dataset every time it is run. It is intended for use in a new dataset only.
npx sanity@latest exec ./scripts/createData.ts --with-user-token
Useful Sanity examples with a light sprinkling of opinionated patterns
Access to Sanity Studio v3 from within the React Router application itself. Now the whole Studio is a component, it lives at the /studio
route.
Powered by Presentation and @sanity/react-loader
Authenticated users inside Sanity Studio can use the Presentation tool to browse through the website and target specific fields to edit - and see changes in real time.
The resource route (/resource/preview
) used by Presentation will set a session token so that overlays will be visible even outside of Presentation.
The Studio has been pre-configured with a basic "Record Collection" schema, as well as a basic Desk Structure to include live preview within a view pane.
Powered by @portabletext/react
This component allows for the customization and extension of how Portable Text is rendered within your app. The Rich Text field in Sanity is an array of objects. Content as data!
Powered by @sanity/image-url
The <RecordCover />
component has an example of how to generate a full Sanity CDN asset URL based on just the asset's ID. This is useful as we do not need to resolve the full image document in the query.
Powered by @sanity/ui
Creating "decorated" inputs has never been easier than in Sanity Studio v3. ./app/sanity/components/Duration.tsx
is a 13-line example where a number
field type to store duration as seconds can also be visualized in minutes. Minimal developer effort, maximum author impact.
Powered by @sanity/client
Requires a token with editor permissions in .env
. Clicking the like/dislike buttons on a Record's page will trigger a React Router action which will send a mutation to the document. This is an example of how user actions on the site may trigger changes on your Sanity dataset.
Powered by Zod
The results of the Sanity query in ./app/routes/$slug.tsx
are parsed with a Zod validator. This ensures that content is always returned in the expected shape at run time, and auto-generates Types which are fed from the loader
into useLoaderData()
.
The record
type generates dynamic open graph images using the ~/routes/resource.og
resource route.
Visit https://localhost:5173/studio
in your React Router app. You will need to:
.env.template
to .env
http://localhost:5173
to the CORS settings on that project, with Allow Credentials privilegesThis React Router template is specifically configured for hosting on Vercel.