-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We need to handle styled components at the server level for remix.
- Loading branch information
1 parent
18fb3c3
commit c422ed1
Showing
2 changed files
with
35 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// c.f. https://github.com/remix-run/examples/blob/main/styled-components/app/entry.server.tsx | ||
import type { AppLoadContext, EntryContext } from "@remix-run/node"; | ||
import { RemixServer } from "@remix-run/react"; | ||
import { renderToString } from "react-dom/server"; | ||
import { ServerStyleSheet } from "styled-components"; | ||
|
||
export default function handleRequest( | ||
request: Request, | ||
responseStatusCode: number, | ||
responseHeaders: Headers, | ||
remixContext: EntryContext, | ||
loadContext: AppLoadContext, | ||
) { | ||
const sheet = new ServerStyleSheet(); | ||
|
||
let markup = renderToString( | ||
sheet.collectStyles( | ||
<RemixServer context={remixContext} url={request.url} />, | ||
), | ||
); | ||
const styles = sheet.getStyleTags(); | ||
|
||
markup = markup.replace("__STYLES__", styles); | ||
|
||
responseHeaders.set("Content-Type", "text/html"); | ||
|
||
return new Response("<!DOCTYPE html>" + markup, { | ||
status: responseStatusCode, | ||
headers: responseHeaders, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters