What is @remix-run/server-runtime?
@remix-run/server-runtime is a package that provides server-side utilities and abstractions for building web applications with Remix. It allows developers to handle server-side rendering, data loading, and other server-side logic in a structured and efficient manner.
What are @remix-run/server-runtime's main functionalities?
Server-Side Rendering
This feature allows you to handle server-side rendering with Remix. The `createRequestHandler` function is used to create a request handler that can be integrated with an Express server.
const { createRequestHandler } = require('@remix-run/server-runtime');
const express = require('express');
const app = express();
app.all('*', createRequestHandler({
getLoadContext() {
// Provide context for loaders
return {};
}
}));
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Data Loading
This feature allows you to load data on the server side before rendering a page. The `json` function is used to return JSON responses from loaders.
const { json } = require('@remix-run/server-runtime');
async function loader({ request }) {
const data = await fetchDataFromAPI();
return json(data);
}
Error Handling
This feature allows you to handle errors in your server-side logic. The `json` function can be used to return error responses with appropriate status codes.
const { json } = require('@remix-run/server-runtime');
async function loader({ request }) {
try {
const data = await fetchDataFromAPI();
return json(data);
} catch (error) {
return json({ error: error.message }, { status: 500 });
}
}
Other packages similar to @remix-run/server-runtime
next
Next.js is a popular React framework that provides server-side rendering, static site generation, and API routes. It offers a more opinionated and integrated approach compared to @remix-run/server-runtime, which is more focused on providing utilities for server-side logic in Remix applications.
express
Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. While it does not provide server-side rendering out of the box, it can be used in conjunction with other libraries to achieve similar functionality to @remix-run/server-runtime.
koa
Koa is a new web framework designed by the team behind Express, which aims to be a smaller, more expressive, and more robust foundation for web applications and APIs. Like Express, it does not provide server-side rendering out of the box but can be extended with middleware to achieve similar functionality.
@remix-run/server-runtime
Remix supports multiple server runtimes:
Support for each runtime is provided by a corresponding Remix package:
This package defines a "Remix server runtime interface" that each runtime package must conform to.
Each Remix server runtime package MUST:
Each Remix server runtime package MAY:
v2.2.0
Date: 2023-10-31
What's Changed
Vite!
Remix 2.2.0
adds unstable support for Vite for Node-based apps! See our announcement blog post and the Future > Vite page in the Remix docs for more details.
You can try it out today with two new (unstable) templates:
# minimal server
npx create-remix@latest --template remix-run/remix/templates/unstable-vite
# custom server (Express example)
npx create-remix@latest --template remix-run/remix/templates/unstable-vite-express
- New APIs in
@remix-run/dev
unstable_vitePlugin
: The new Remix Vite pluginunstable_createViteServer
: Creates a Vite server in middleware mode for interop with custom serversunstable_loadViteServerBuild
: Allows your custom server to delegate SSR requests to Vite during development
- Changed APIs
createRequestHandler
: Now also allows the build
argument to be a function that will be used to dynamically load new builds for each request during development
- Other Runtimes
- Deno support is untested, but should work through Deno's Node/
npm
interop - CloudFlare support is not yet available
New Fetcher APIs
Per this RFC, we've introduced some new APIs that give you more granular control over your fetcher behaviors:
- You may now specify your own fetcher identifier via
useFetcher({ key: string })
, which allows you to access the same fetcher instance from different components in your application without prop-drilling - Fetcher keys are now exposed on the fetchers returned from
useFetchers
so that they can be looked up by key
Form
and useSubmit
now support optional navigate
/fetcherKey
props/params to allow kicking off a fetcher submission under the hood with an optionally user-specified key
<Form method="post" navigate={false} fetcherKey="my-key">
submit(data, { method: "post", navigate: false, fetcherKey: "my-key" })
- Invoking a fetcher in this way is ephemeral and stateless
- If you need to access the state of one of these fetchers, you will need to leverage
useFetchers()
or useFetcher({ key })
to look it up elsewhere
Persistence Future Flag
Per the same RFC as above, we've introduced a new future.v3_fetcherPersist
flag that allows you to opt-into the new fetcher persistence/cleanup behavior. Instead of being immediately cleaned up on unmount, fetchers will persist until they return to an idle
state. This makes pending/optimistic UI much easier in scenarios where the originating fetcher needs to unmount.
- This is sort of a long-standing bug fix as the
useFetchers()
API was always supposed to only reflect in-flight fetcher information for pending/optimistic UI -- it was not intended to reflect fetcher data or hang onto fetchers after they returned to an idle
state - Keep an eye out for the following specific behavioral changes when opting into this flag and check your app for compatibility:
- Fetchers that complete while still mounted will no longer appear in
useFetchers()
after completion - they served no purpose in there since you can access the data via useFetcher().data
- Fetchers that previously unmounted while in-flight will not be immediately aborted and will instead be cleaned up once they return to an
idle
state
- They will remain exposed via
useFetchers
while in-flight so you can still access pending/optimistic data after unmount - If a fetcher is no longer mounted when it completes, then it's result will not be post processed - e.g., redirects will not be followed and errors will not bubble up in the UI
- However, if a fetcher was re-mounted elsewhere in the tree using the same
key
, then it's result will be processed, even if the originating fetcher was unmounted
Minor Changes
- Unstable
vite
support (#7590) - New fetcher
key
APIs and navigate
/fetcherKey
params for navigational APIs (#10960) - New
future.v3_fetcherPersist
flag (#10962)
Patch Changes
@remix-run/express
: Allow the Express adapter to work behind a proxy when using app.enable('trust proxy')
(#7323)
- Previously, this used
req.get('host')
to construct the Remix Request
, but that does not respect X-Forwarded-Host
- This now uses
req.hostname
which will respect X-Forwarded-Host
@remix-run/react
: Fix warning that could be inadvertently logged when using route files with no default
export (#7745)create-remix
: Support local tarballs with a .tgz
extension which allows direct support for pnpm pack
tarballs (#7649)create-remix
: Default the Remix app version to the version of create-remix
being used (#7670)
- This most notably enables easier usage of tags, e.g.
npm create remix@nightly
Updated Dependencies
Changes by Package
Full Changelog: v2.1.0...v2.2.0