Edgio
Edgio

Sanity Studio

This guide shows you how to deploy a Sanity Studio application to Edgio.

Sanity Studio is a single page app (SPA) written in React, where you can configure the document types and input fields, with simple JavaScript objects. This guide will walk you through how to deploy Sanity Studio with Edgio in four simple steps.

Example

System Requirements

Sign up for Edgio

Deploying requires an account on Edgio. Sign up here for free.

Install the Edgio CLI

If you have not already done so, install the Edgio CLI.

With npm:

Bash
1npm i -g @layer0/cli

With yarn:

Bash
1yarn global add @layer0/cli

Create a new Sanity Studio app

First, install the Sanity CLI:

Bash
1npm i -g @sanity/cli

To initiate a new project and download the Studio code to your computer, run the following in the command line:

Bash
1sanity init

The Sanity CLI will walk you through the necessary steps to set up a project, letting you choose a schema template. When you’re done with these steps, the CLI will download the source code and configuration to get you started. To start a local development server, cd into the project folder and run the following command:

Bash
1sanity start

Configuring your Sanity Studio app for Edgio

Initialize your project

In the root directory of your project run 0 init:

Bash
10 init

This will automatically update your package.json and add all of the required Edgio dependencies and files to your project. These include:

  • The @layer0/core package - Allows you to declare routes and deploy your application on Edgio
  • The @layer0/prefetch package - Allows you to configure a service worker to prefetch and cache pages to improve browsing speed
  • layer0.config.js - A configuration file for Edgio
  • routes.js - A default routes file that sends all requests to Sanity Studio.

Configure the routes

Update routes.js at the root of your project to the following:

JavaScript
1// This file was added by layer0 init.
2// You should commit this file to source control.
3
4import { Router } from '@layer0/core/router'
5
6export default new Router()
7 // Prevent search engine bot(s) from indexing
8 // Read more on: https://docs.layer0.co/guides/cookbook#blocking-search-engine-crawlers
9 .noIndexPermalink()
10 .static('dist', ({ cache }) => {
11 cache({
12 edge: {
13 maxAgeSeconds: 60 * 60 * 60 * 365,
14 forcePrivateCaching: true,
15 },
16 browser: {
17 maxAgeSeconds: 0,
18 serviceWorkerSeconds: 60 * 60 * 24,
19 },
20 })
21 })
22 .fallback(({ appShell }) => {
23 appShell('dist/index.html')
24 })

Refer to the Routing guide for the full syntax of the routes.js file and how to configure it for your use case.

Run the Sanity Studio app locally on Edgio

Create a production build of your app by running the following in your project’s root directory:

Bash
1npm run build

Set default port number for the app to run on 3333:

Bash
1set PORT=3333 # windows
2export PORT=3333 # linux

Test your app with the App Platform on your local machine by running the following command in your project’s root directory:

Bash
10 dev

Load the site http://127.0.0.1:3333

Deploying

Create a production build of your app by running the following in your project’s root directory:

Bash
1npm run build

Deploy your app to the App Platform by running the following command in your project’s root directory:

Bash
10 deploy

Refer to the Deploying guide for more information on the deploy command and its options.

Post Deployment Whitelisting

Once Sanity Studio is deployed, you will need to add it’s URL to Sanity’s CORS origins settings. You can do this from the command line:

Bash
1sanity cors add https://your-url.layer0-limelight.link --credentials

Alternatively, you can navigate to manage.sanity.io, find your project and under Settings > API, add the Studio URL to the CORS origins list. You should allow credentials as the Studio requires authentication for added security.