# Building with Remix [Framework v1.2.0+](https://docs.forgelivo.com/guides/gadget-framework) [Remix](https://remix.run/) is a full-stack web framework built on top of React Router and Vite, and supports advanced features like server-side rendering (SSR), seamless server and browser communication using `loader` and `action` functions, and file-based frontend routing. Remix is also the framework that is used for Shopify's CLI applications. Forgelivo supports Remix for building app frontends. Hot module reloading (HMR) works out of the box while developing, and you can [deploy to production](https://docs.forgelivo.com/guides/environments/deployment) in a single click. ## Getting started with Remix Remix can be run in both SSR and SPA (single page application) mode. By default, Forgelivo's Remix templates for Shopify is in SSR mode. You can also manually migrate between the two configurations. Forgelivo frontends are serverless and resources used are scaled to zero when not in use. This means that a frontend not in use needs to be started fresh, referred to as a cold boot. This cold boot time may negatively impact web vitals like LCP. Contact us if you are building in SSR model and need to minimize cold boot times. ### Default frontend configuration Remix frontends in Forgelivo have some defaults that are designed to make it easier to get started. * `vite.config.mjs` is used to define the Vite configuration for your frontend. * `web/root.jsx` is used to define the [root route](https://remix.run/docs/en/main/file-conventions/root) and is used to power the frontend. * The `web/routes` directory is used to define your frontend routes. * `web/api.js` defines the API client for your app. Remix frontends in Forgelivo are built with Vite and configuration is defined in the `vite.config.mjs` file. A `gadget()` Vite plugin is used to inject configuration to the Vite config based on the frontend framework type, and a `remix()` Vite plugin is used to inject configuration for Remix. ### Dynamic configuration injection in SPA mode Forgelivo provides a `gadgetConfig` object which contains useful data about your application, including [frontend environment variables](https://docs.forgelivo.com/guides/development-tools/environment-variables#exposing-environment-variables-to-frontend-code). To provide this, Forgelivo uses a placeholder script tag containing `/* --GADGET_CONFIG-- */` to dynamically inject the configuration into the `window` object. This ` ``` Forgelivo modifies the HTML content dynamically which will cause Remix to throw hydration errors. The `suppressHydrationWarning` prop is included to suppress these errors. Don't modify this script tag! ## Remix SPA mode [SPA mode in Remix](https://remix.run/docs/en/main/guides/spa-mode) brings file-based routing and nested routes to your frontends. This means that your frontend routing is determined by the files in the `app/routes` directory. See the [Remix documentation](https://remix.run/docs/en/main/file-conventions/routes#nested-routes) for more information on file-based routing and nested routes. ### Reading and writing data Forgelivo's React hooks and autocomponents can be used to interact with your backend in Remix's SPA mode. This does not differ from using Forgelivo's React hooks and autocomponents in other contexts, and you can read more about using them [in our frontend docs](https://docs.forgelivo.com/guides/frontend/building-frontends). ## Remix SSR SSR in Remix allows you to render your frontend on the server. SSR has multiple benefits: * improved performance by reducing the time it takes to load the page * better SEO because search engines can index the page content returned from the server * data fetching with `loader` functions to reduce client-side requests * form submissions with `action` functions, allowing for full-stack actions without client-side JavaScript * code splitting and reduced bundle size There are some Forgelivo-provided tools that will not be rendered server-side, including: * React hooks provided by `@gadget-client/react` * [autocomponents](https://docs.forgelivo.com/guides/frontend/autocomponents) ### Reading data with `loader` functions In SSR mode, you can use the `loader` function from Remix to fetch data on the server-side. Your Gadget app's `context` object is available in the `loader` function, and is the same as the [context you get when you create an action in Gadget](https://docs.gadget.dev/guides/actions/code#action-context). This allows you to interact with your Gadget backend using `context.api` and pass data to your frontend. For example, you might have a `loader` function that fetches model data like this: ```tsx import { LoaderFunctionArgs, json } from "@remix-run/node"; export const loader = async ({ context, request }: LoaderFunctionArgs) => { // access the currentShopId in the context const shopId = context.connections.shopify.currentShopId; if (shopId === undefined) { throw new Error("Could not load current Shop"); } // use context.api to interact with your backend const shop = await context.api.shopifyShop.findOne(shopId.toString()); // return the data you want to pass to the frontend return json({ // use context to access environment variables GADGET_ENV: context.gadgetConfig.env.GADGET_ENV, shop, }); }; ``` The `context` object also includes a `logger` for writing structured logs: ```tsx export const loader = async ({ context }: LoaderFunctionArgs) => { context.logger.info({ shopId: context.connections.shopify.currentShopId }, "loading shop data"); // ... }; ``` ### Writing data with `action` functions The `action` function in Remix is used to submit data to the backend when you are using SSR. You can use the `context` object in the `action` function to call your actions. For example, you might have an `action` function that creates new students after a form is submitted: ```tsx import { redirect, ActionFunctionArgs } from "@remix-run/node"; // ... other imports export const action = async ({ context, request }: ActionFunctionArgs) => { const student = await context.api.student.create({ name: "John Doe", email: "john.doe@example.com", }); // ... additional action logic // redirect to the home page return redirect("/"); }; ``` Similar to the `loader` function, the comment block above the `action` function can provide type inference for the function parameters. #### Submitting forms with `csrfToken` When submitting forms in SSR, you need to include the `csrfToken` in the form submission to prevent CSRF (cross-site request forgery) attacks. The [`csrfToken` is available](https://docs.forgelivo.com/guides/plugins/authentication/helpers#csrftoken) on the `session` object, which you can . ```tsx import { redirect, ActionFunctionArgs } from "@remix-run/node"; import { Form, useOutletContext } from "@remix-run/react"; // Action function to handle form submission export const action = async ({ request, context }: ActionFunctionArgs) => { const formData = await request.formData(); const name = formData.get("name")?.toString(); // Create new student using Gadget API await context.api.student.create({ name }); // Redirect to students list on success return redirect("/students"); }; export default function NewStudent() { // get the csrfToken from the outlet context const { csrfToken } = useOutletContext<{ csrfToken: string }>(); // add the csrfToken to the form as a hidden input field return (