Remix 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.
Available in Gadget framework version 1.2+
Remix support in Gadget was added with Gadget framework version 1.2. If you're using an older version of Gadget, you'll need to
upgrade your framework version to run Remix apps in Gadget.
Gadget supports Remix for building app frontends. Hot module reloading (HMR) works out of the box while developing, and you can deploy to production in a single click.
Getting started with Remix
Remix can be run in both SSR and SPA (single page application) mode.
By default, Gadget's Remix template for Shopify is in SPA mode, and the Remix template for web apps uses SSR. You can also manually migrate between the two configurations.
Cold boot times for SSR
Gadget 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 Gadget 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 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 Gadget 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
Gadget provides a gadgetConfig object which contains useful data about your application, including frontend environment variables. To provide this, Gadget uses a placeholder script tag containing /* --GADGET_CONFIG-- */ to dynamically inject the configuration into the window object. This <script> tag is included in the web/root.jsx file:
Gadget 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 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 for more information on file-based routing and nested routes.
Reading and writing data
Gadget's React hooks and autocomponents can be used to interact with your backend in Remix's SPA mode.
This does not differ from using Gadget's React hooks and autocomponents in other contexts, and you can read more about using them in our frontend docs.
Migrating from SPA to SSR
To migrate a Remix frontend from SPA mode to SSR, you need to make a few changes to your app.
Install the isbot package if you are not planning to customize your entry.server file.
Remove the ssr: false flag from the remix plugin in the vite.config.mjs file.
Remove the /* --GADGET_CONFIG-- */ script tag from web/root.jsx.
Update your routes to use the loader function to fetch data and the action function to submit data.
Updating your routes is not required, but it is recommended to do so to take advantage of the benefits of SSR.
Additional steps for migrating Shopify SPA apps to SSR
If you are migrating a Shopify Remix SPA app to SSR, you need to update the following files so the Shopify App Bridge works correctly.
Update the web/components/App.jsx file:
Remove the use of the window object.
Export the existing Unauthenticated function. This component will be used in the next step.
Remove the use of the isAuthenticated value returned from the useGadget hook.
Below is an example of what the updated web/components/App.jsx file could look like if you are migrating the default Gadget Shopify Remix SPA app to SSR mode:
In SSR mode, you could use the loader function from Remix to fetch the data on 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. This allows you to interact with your Gadget backend and pass data to your frontend.
For example, you might have a loader function that fetches the model data like this:
The comment block above the loader function is used to provide the types for the function parameters like context. This is optional but can help with type inference in the editor.
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:
Check out the Remix reference on outlet context for more information.
actAsAdmin for bypassing tenancy
In SSR mode, you can access your Gadget app's API client in your loader and action functions using context.api.
By default, the API client will have the role and permissions granted by the current session, which means that any requests made will have tenancy applied by default. This is different from using the API client in Gadget actions or HTTP routes, where tenancy is not applied by default.
If you need to make requests outside of the current tenancy you can use actAsAdmin in loader and action functions. For example:
SPA mode does not support the loader and action functions. You need to use Gadget's React hooks and autocomponents to read and write data in your frontend.
Migrate from React Router to Remix
If you would like to migrate your existing Gadget frontend app to Remix SPA mode, you can follow these steps.
Note that this sample migration is for a Shopify app.
Step 1: Install required packages
To run Remix apps, you need the following packages in package.json:
add the following Remix packages
yarnadd @remix-run/node @remix-run/react
And then install the following dev dependency:
add @remix-run/dev as a dev dependency
yarnadd -D @remix-run/dev
Step 2: Update your Vite configuration
You need to include the remix Vite plugin from @remix-run/dev as well as our Gadget Vite plugin with pre-defined options.
You can copy the following example into your vite.config.mjs file:
4import{ vitePlugin as remix }from"@remix-run/dev";
5
6exportdefaultdefineConfig({
7plugins:[
8gadget(),
9remix({
10...remixViteOptions,
11ssr:false,
12}),
13],
14});
The remixViteOptions object provides some pre-defined options to make it easier to set up your Vite app to work with Remix.
The gadget Vite plugin dynamically injects Vite configuration for you Gadget apps. Different configuration is injected depending on the type of frontend you are building.
Step 3: Update the build script in package.json
We run the build script from the package.json file when deploying to production. You need to update this script to use the remix Vite plugin.
package.json
json
{
"scripts":{
"build":"NODE_ENV=production remix vite:build"
}
}
Step 4: Rename files that use the window object
By default, your Gadget frontend has a web/api.js that uses window to get the Gadget config when setting up the API client:
You can remove the following files as they are no longer needed:
index.html
web/main.jsx
Step 7: Rewrite App component
Now that app setup is handled in web/root.jsx, you can simplify the web/components/App.jsx file to provide your app's UI only. This may include an AuthenticatedApp component that conditionally renders your app's UI based on the user's login state.
Here's what a web/components/App.jsx file might look like for a Shopify app: