Metronome logo

Documentation

Setting up Metronome in a Cloudfare Pages project

Metronome is unable to track duration of the requests, loaders, and actions because they're intentionally locked in Cloudflare in order to prevent attacks.

Learn more.

0. Install Remix with the Cloudflare Pages adapter

If you have already installed Remix, you can skip this section.

Terminal
npx create-remix@latest
  1. Name your project (e.g. "my-remix-app")
  2. After you name your project, select Just the basics and press enter.
  3. Select Cloudflare Pages and press enter.

1. Install Metronome

Install Metronome React and Cloudflare Pages packages

Terminal
npm install @metronome-sh/react @metronome-sh/cloudflare-pages

2. Initialize Metronome

To create Metronome's metronome.config.js file, you need to run the init command.

Terminal
npx metronome init

3. Configure Metronome in your server.js file

  1. Import combineGetLoadContexts, createMetronomeGetLoadContext and registerMetronome from the @metronome-sh/cloudflare-pages module in your server.js file.
server.js
import {
combineGetLoadContexts,
createMetronomeGetLoadContext,
registerMetronome,
} from "@metronome-sh/cloudflare-pages";
  1. Register Metronome in the build by using the registerMetronome function in your server.js file.
server.js
const buildWithMetronome = registerMetronome(build);
  1. create the getLoadContext function using createMetronomeGetLoadContext with buildWithMetronome as first argument and also the config file import as an option.
server.js
const metronomeGetLoadContext = createMetronomeGetLoadContext(
buildWithMetronome,
{ config: require("./metronome.config.js") }
);
  1. In the createPagesFunctionHandler function, replace the build prop with buildWithMetronome and combine the existing getLoadContext with the metronomeGetLoadContext using combineGetLoadContexts:
server.js
const handleRequest = createPagesFunctionHandler({
build: buildWithMetronome,
mode: process.env.NODE_ENV,
getLoadContext: combineGetLoadContexts(
(context) => context.env,
metronomeGetLoadContext
),
});

Your server.js file should now look like this:

server.js
import {
combineGetLoadContexts,
createMetronomeGetLoadContext,
registerMetronome,
} from "@metronome-sh/cloudflare-pages";
import { createPagesFunctionHandler } from "@remix-run/cloudflare-pages";
import * as build from "@remix-run/dev/server-build";
const buildWithMetronome = registerMetronome(build);
const metronomeGetLoadContext = createMetronomeGetLoadContext(
buildWithMetronome,
{ config: require("./metronome.config.js") }
);
const handleRequest = createPagesFunctionHandler({
build: buildWithMetronome,
mode: process.env.NODE_ENV,
getLoadContext: combineGetLoadContexts(
(context) => context.env,
metronomeGetLoadContext
),
});
export function onRequest(context) {
return handleRequest(context);
}

4. Configure Metronome in your root.tsx file

After you have configured the server side of Metronome in your app, you need to set up the client side with @metronome-sh/react to be able to track your app's Web Vitals.

Simply import MetronomeLinks from @metronome-sh/react:

root.tsx
import { MetronomeLinks } from "@metronome-sh/react";

You should render the <MetronomeLinks /> component between your <head> tags.

root.tsx
export default function App() {
return (
<html lang="en">
<head>
<Meta />
<Links />
<MetronomeLinks />
</head>
<body>
<Outlet />
<ScrollRestoration />
<Scripts />
<LiveReload />
</body>
</html>
);
}

5. Create a new project in Metronome

To create a new project, please follow the instructions in the Creating a new project section.

6. Set your API key in your production environment

After you created you new project in https://metronome.sh, you need to add the METRONOME_API_KEY environment variable in cloudflare. For more information visit the Cloudflare Pages documentation

Deploy your project

Once you have your METRONOME_API_KEY set up, deploy your project to production, after your first couple of visits, you should be able to see metrics.