This guide shows you how to deploy a Sanity Studio application on Layer0.
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 Layer0 in four simple steps.
Example
System Requirements
Sign up for Layer0
Deploying requires an account on Layer0. Sign up here for free.
Install the Layer0 CLI
If you have not already done so, install the Layer0 CLI
npm i -g @layer0/cli
Create a new Sanity Studio app
First, install the Sanity CLI:
npm i -g @sanity/cli
To initiate a new project and download the Studio code to your computer, run the following in the command line:
sanity 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:
sanity start
Configuring your Sanity Studio app for Layer0
Initialize your project
In the root directory of your project run 0 init
:
0 init
This will automatically update your package.json
and add all of the required Layer0 dependencies and files to your project. These include:
- The
@layer0/core
package - Allows you to declare routes and deploy your application on Layer0 - 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 Layer0routes.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:
// This file was added by layer0 init.
// You should commit this file to source control.
import { Router } from '@layer0/core/router'
export default new Router()
.static('dist', ({ cache }) => {
cache({
edge: {
maxAgeSeconds: 60 * 60 * 60 * 365,
forcePrivateCaching: true,
},
browser: {
maxAgeSeconds: 0,
serviceWorkerSeconds: 60 * 60 * 24,
},
})
})
.fallback(({ appShell }) => {
appShell('dist/index.html')
})
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 Layer0
Create a production build of your app by running the following in your project’s root directory:
npm run build
Set default port number for the app to run on 3333:
set PORT= 3333 # windows
export PORT= 3333 # linux
Run Layer0 on your local machine:
0 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:
npm run build
Next, deploy the build to Layer0 by running the 0 deploy
command:
0 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:
sanity 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.