Deploy Frontity on Layer0
​Layer0 is an all-in-one platform to develop, deploy, preview, experiment on, monitor, and run your headless frontend. It is focused on large, dynamic sites and best-in-class performance through EdgeJS (a JavaScript-based Content Delivery Network), predictive prefetching, and performance monitoring.
Layer0's EdgeJS enables powerful and precise control of Edge based caching and routing that can improve the performance for Frontity sites.
For the full details on deploying Frontity on Layer0 refer to the Frontity on Layer0 guide in the developer documentation.

Getting Started

First start by installing the Layer0 command-line interface (CLI),
1
npm i -g @layer0/cli
Copied!

Project setup

Run the init command in the directory of your Frontity project:
1
cd my-frontity-app
2
layer0 init
Copied!
This will automatically configure your app for deployment on Layer0.

Running locally

You can simulate your app running on Layer0 using the dev command:
1
layer0 dev
Copied!
In particular, the --cache option will emulate Edge caching rules locally so that you can test easily test edge behavior during development without having to do a deploy to the cloud:
1
layer0 dev --cache
Copied!

Deploying

Deploying a Frontity app requires an account on Layer0. Sign up here for free.
Once you have installed the CLI and created an account, you should login from the terminal by running the login command:
1
layer0 login
Copied!
Once you have an account and have logged in to the CLI, you can deploy to the Layer0 by running the following in the root folder of your project:
1
layer0 deploy
Copied!

Enabling Prefetching (optional)

Layer0 improves the performance of your site by bundling an integrated server worker that will predictively prefetch cached pages from the edge. To add the Layer0 service worker to your app, call the install function from @layer0/prefetch/window in a useEffect hook when the app first loads. For example, you can alter the Header component in your theme as follows:
1
// mars-theme/src/components/header.js
2
​
3
import { useEffect } from 'react'
4
​
5
const Header = ({ state }) => {
6
useEffect(() => {
7
if (process.env.NODE_ENV === 'production') {
8
install()
9
}
10
}, [])
11
​
12
/* ... */
13
}
Copied!
To prefetch data into the browser cache using the service worker, use the Prefetch component from @layer0/react. This component prefetches a specific URL from the Layer0 edge when it becomes visible in the viewport. You typically wrap it around links. For example:
1
import { Prefetch } from '@layer0/react'
2
​
3
function MyComponent() {
4
return (
5
<Prefetch url="/some/data/url.json">
6
{/* When this link is scrolled into view, /some/data/url.json in JSON will be fetched in the background and put in the browser cache */}
7
<a href="/link/to/page">My Page</a>
8
</Prefetch>
9
)
10
}
Copied!
Last modified 4mo ago