This guide shows you how to deploy Frontity apps on Layer0.

This framework has a connector developed for Layer0. See Connectors for more information.

If you don't already have a Frontity app, use the terminal (or command prompt on Windows) to create one using the commands below:

npx frontity create my-app

To prepare your Frontity app for deployment on Layer0, run the following in the root folder of your project:

npm install -g @layer0/cli
0 init

This will automatically add all of the required 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/frontity package - Provides router middleware that automatically adds Frontity routes to the Layer0 router.
  • The @layer0/prefetch package - Allows you to configure a service worker to prefetch and cache pages to improve browsing speed
  • The @layer0/react package - Provides a Prefetch component for prefetching pages
  • routes.js - A default routes file that sends all requests to Frontity. Update this file to add caching or proxy some URLs to a different origin.
  • sw/service-worker.js - The source code for your service worker, which enables prefetching when running on Layer0.
  • layer0.config.js - Contains configuration options for deploying on Layer0.

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:

// mars-theme/src/components/header.js
import { useEffect } from 'react'
import { install } from '0/prefetch/window'

const Header = ({ state }) => {
  useEffect(() => {
    if (process.env.NODE_ENV === 'production') {
      install()
    }
  }, [])

  /* ... */
}

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:

import { Prefetch } from '@layer0/react'

function MyComponent() {
  return (
    <Prefetch url="/some/data/url.json">
      {/* 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 */}
      <a href="/link/to/page">My Page</a>
    </Prefetch>
  )
}

To simulate your app within Layer0 locally, run:

0 dev

To simulate edge caching locally, run:

0 dev --cache

Deploying requires an account on Layer0. Sign up here for free. Once you have an account, you can deploy to Layer0 by running the following in the root folder of your project

0 deploy

See deploying for more information.

Edit this guide on GitHub