Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Pages]: Add binding instructions to remix sites #5120

Merged
merged 13 commits into from
Nov 30, 2022
50 changes: 49 additions & 1 deletion content/pages/framework-guides/remix.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Deploy your site to Pages by logging in to the [Cloudflare dashboard](https://da

{{<Aside type="warning">}}

Currently Cloudflare uses Node `12.18.0` in the Pages build environment, but Remix requires a newer node version >14.0.0 to build on Cloudflare Pages. To set the Node version go to _Settings_ > _Environment Variables_ > _Production_ and add `NODE_VERSION = v16.7.0` in your production option.
Currently Cloudflare uses Node `12.18.0` in the Pages build environment, but Remix requires a newer node version >14.0.0 to build on Cloudflare Pages. To set the Node version go to **Settings** > **Environment variables** > **Production** and add `NODE_VERSION = v16.7.0` in the production option.

{{</Aside>}}
Ekwuno marked this conversation as resolved.
Show resolved Hide resolved

Expand All @@ -74,6 +74,54 @@ For the complete guide to deploying your first site to Cloudflare Pages, refer t
After deploying your site, you will receive a unique subdomain for your project on `*.pages.dev`.
Every time you commit new code to your Remix site, Cloudflare Pages will automatically rebuild your project and deploy it. You will also get access to [preview deployments](/pages/platform/preview-deployments/) on new pull requests, so you can preview how changes look to your site before deploying them to production.

## Creating and adding a binding to your Remix application
deadlypants1973 marked this conversation as resolved.
Show resolved Hide resolved

A [binding](/pages/platform/functions/#adding-bindings) is how your Function (Worker) interacts with external resources. You can add KV, Durable Object, and plain-text bindings to your project. A binding is a runtime variable that the Workers runtime provides to your code. You can also use these bindings in development with [Wrangler](https://developers.cloudflare.com/workers/wrangler/get-started/).
deadlypants1973 marked this conversation as resolved.
Show resolved Hide resolved

To add a KV namespace or Durable Object binding to your Remix application, refer to [Functions](/pages/platform/functions/#adding-bindings).
deadlypants1973 marked this conversation as resolved.
Show resolved Hide resolved

### Using binding in your Remix application
Ekwuno marked this conversation as resolved.
Show resolved Hide resolved

For example, if you create a KV namespace binding called `PRODUCTS_KV`, you will access it in your Remix application by first importing the `LoaderFunction` from `@remix-run/cloudflare-pages`. The `LoaderFunction` allows you to access data from different points in your Remix application.
Ekwuno marked this conversation as resolved.
Show resolved Hide resolved

The following code block shows an example of accessing a KV namespace in Remix.
Ekwuno marked this conversation as resolved.
Show resolved Hide resolved

```typescript
Ekwuno marked this conversation as resolved.
Show resolved Hide resolved
---
filename: app/routes/products/$productId.tsx
highlight: [9,10,11,12,13,18,21]
GregBrimble marked this conversation as resolved.
Show resolved Hide resolved
deadlypants1973 marked this conversation as resolved.
Show resolved Hide resolved
---
import type { LoaderFunction } from "@remix-run/cloudflare";
Ekwuno marked this conversation as resolved.
Show resolved Hide resolved
import { json } from "@remix-run/cloudflare";
import { useLoaderData } from "@remix-run/react";

export const loader: LoaderFunction = async ({
Ekwuno marked this conversation as resolved.
Show resolved Hide resolved
context, params,
params,
Ekwuno marked this conversation as resolved.
Show resolved Hide resolved
}) => {
Ekwuno marked this conversation as resolved.
Show resolved Hide resolved
return json(
await context.PRODUCTS_KV.get(`product-${params.productId}`, {
Ekwuno marked this conversation as resolved.
Show resolved Hide resolved
await PRODUCTS_KV.get(`product-${params.productId}`, {
Ekwuno marked this conversation as resolved.
Show resolved Hide resolved
type: "json",
})
);
};

export default function Product() {
const product = useLoaderData();
Ekwuno marked this conversation as resolved.
Show resolved Hide resolved
return (
<div>
<p>Product</p>
{product.name}
<p>{} Products</p>
Ekwuno marked this conversation as resolved.
Show resolved Hide resolved
Ekwuno marked this conversation as resolved.
Show resolved Hide resolved
{/* ... */}
</div>
);
}

```


Ekwuno marked this conversation as resolved.
Show resolved Hide resolved
## Learn more

By completing this guide, you have successfully deployed your Remix.js site to Cloudflare Pages. To get started with other frameworks, [refer to the list of Framework guides](/pages/framework-guides/).
Ekwuno marked this conversation as resolved.
Show resolved Hide resolved