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

chore: clean up chakra example #35

Merged
merged 2 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions chakra-ui/app/entry.client.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
import { RemixBrowser } from "@remix-run/react";
import { hydrate } from "react-dom";
import { startTransition, StrictMode } from "react";
import { hydrateRoot } from "react-dom/client";
import createEmotionCache from "@emotion/cache";
import { CacheProvider } from "@emotion/react";

hydrate(<RemixBrowser />, document);
function hydrate() {
const emotionCache = createEmotionCache({ key: "css" });

startTransition(() => {
hydrateRoot(
document,
<StrictMode>
<CacheProvider value={emotionCache}>
<RemixBrowser />
</CacheProvider>
</StrictMode>
);
});
}

if (window.requestIdleCallback) {
window.requestIdleCallback(hydrate);
} else {
// Safari doesn't support requestIdleCallback
// https://caniuse.com/requestidlecallback
window.setTimeout(hydrate, 1);
}
60 changes: 52 additions & 8 deletions chakra-ui/app/entry.server.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,65 @@
import { PassThrough } from "stream";

import type { EntryContext } from "@remix-run/node";
import { Response } from "@remix-run/node";
import { RemixServer } from "@remix-run/react";
import { renderToString } from "react-dom/server";
import isbot from "isbot";
import { renderToPipeableStream } from "react-dom/server";
import createEmotionCache from "@emotion/cache";
import { CacheProvider as EmotionCacheProvider } from "@emotion/react";
import createEmotionServer from "@emotion/server/create-instance";

const ABORT_DELAY = 5000;

export default function handleRequest(
request: Request,
responseStatusCode: number,
responseHeaders: Headers,
remixContext: EntryContext
) {
const markup = renderToString(
<RemixServer context={remixContext} url={request.url} />
);
const callbackMethod = isbot(request.headers.get("user-agent"))
? "onAllReady"
: "onShellReady";

return new Promise((resolve, reject) => {
let didError = false;

const emotionCache = createEmotionCache({ key: "css" });

const { pipe, abort } = renderToPipeableStream(
<EmotionCacheProvider value={emotionCache}>
<RemixServer context={remixContext} url={request.url} />
</EmotionCacheProvider>,
{
[callbackMethod]() {
const reactBody = new PassThrough();
const emotionServer = createEmotionServer(emotionCache);

const bodyWithStyles = emotionServer.renderStylesToNodeStream();
reactBody.pipe(bodyWithStyles);

responseHeaders.set("Content-Type", "text/html");

resolve(
new Response(bodyWithStyles, {
headers: responseHeaders,
status: didError ? 500 : responseStatusCode,
})
);

pipe(reactBody);
},
onShellError(err: unknown) {
reject(err);
},
onError(error: unknown) {
didError = true;

responseHeaders.set("Content-Type", "text/html");
console.error(error);
},
}
);

return new Response("<!DOCTYPE html>" + markup, {
status: responseStatusCode,
headers: responseHeaders,
setTimeout(abort, ABORT_DELAY);
});
}
14 changes: 8 additions & 6 deletions chakra-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@
"start": "remix-serve build"
},
"dependencies": {
"@chakra-ui/react": "^1.8.6",
"@emotion/react": "^11.8.1",
"@emotion/styled": "^11.8.1",
"@chakra-ui/react": "^2.3.6",
"@emotion/react": "^11.10.4",
"@emotion/server": "^11.10.0",
"@emotion/styled": "^11.10.4",
"@remix-run/node": "*",
"@remix-run/react": "*",
"@remix-run/serve": "*",
"framer-motion": "^5.6.0",
"react": "^17.0.2",
"react-dom": "^17.0.2"
"framer-motion": "^6.5.1",
"isbot": "^3.5.4",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@remix-run/dev": "*",
Expand Down