Skip to content

Instantly share code, notes, and snippets.

@ryanflorence
Created May 6, 2020 21:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ryanflorence/610613b54e5b34f8122d1ba4a3da21a9 to your computer and use it in GitHub Desktop.
Save ryanflorence/610613b54e5b34f8122d1ba4a3da21a9 to your computer and use it in GitHub Desktop.
import React from "react";
import { renderToNodeStream } from "react-dom/server";
import Remix, { loadRouteData } from "remix/RemixNode";
// same App as entry-web!
import App from "./App";
export default async (req, res, assets, matches) => {
// @remix-run/firebase is calling this function and passing
// the arguments in, @remix-run/aws or @remix-run/azure will
// pass in whatever APIs those platforms use
// you have total control over the server rendering, here we
// start with the doctype
res.write("<!DOCTYPE html>");
// This will be passed to the route `load` exports, you can
// put session information here, auth context, etc.
let loadContext = {};
// This part is rough right now, we have plans to remove it,
// but long story short, remix can't import these modules for you
// because of the way the build works, but it needs them to be
// able to load all the route data before rendering
let routeModules = matches.map(match =>
require(`./${match.route.importPath}`)
);
// Alright, we're ready to load the route data now:
let data = await loadRouteData(matches, loadContext, routeModules);
// Now we can stream the markup down to the browser, just have to tell
// Remix a few things. Remix already told this function about the
// assets and the matched routes (the `assets` and `matches`) parameters
// to this function.
let stream = renderToNodeStream(
<Remix
location={req.url}
data={data}
assets={assets}
loadContext={loadContext}
>
<App />
</Remix>
);
stream.pipe(res);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment