Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Pass nonce to children
  • Loading branch information
regalijan committed Oct 19, 2023
1 parent 70b4341 commit 5e7779a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
10 changes: 7 additions & 3 deletions app/entry.client.tsx
@@ -1,6 +1,6 @@
import { CacheProvider } from "@emotion/react";
import { ClientStyleContext } from "./context.js";
import createEmotionCache, { defaultCache } from "./createEmotionCache.js";
import createEmotionCache from "./createEmotionCache.js";
import { hydrateRoot } from "react-dom/client";
import { Integrations } from "@sentry/tracing";
import { RemixBrowser } from "@remix-run/react";
Expand All @@ -16,10 +16,14 @@ Sentry.init({
});

function ClientCacheProvider({ children }: { children: ReactNode }) {
const [cache, setCache] = useState(defaultCache);
const nonce =
document
.querySelector("meta[name='style-nonce']")
?.getAttribute("content") || undefined;
const [cache, setCache] = useState(createEmotionCache(nonce));

function reset() {
setCache(createEmotionCache());
setCache(createEmotionCache(nonce));
}

return (
Expand Down
4 changes: 2 additions & 2 deletions app/entry.server.tsx
Expand Up @@ -10,9 +10,9 @@ export default function handleRequest(
request: Request,
responseStatusCode: number,
responseHeaders: Headers,
remixContext: EntryContext
remixContext: EntryContext & RequestContext
) {
const cache = createEmotionCache();
const cache = createEmotionCache(remixContext.data.nonce);
const { extractCriticalToChunks } = createEmotionServer(cache);
const html = renderToString(
<ServerStyleContext.Provider value={null}>
Expand Down
1 change: 1 addition & 0 deletions app/root.tsx
Expand Up @@ -157,6 +157,7 @@ function getMarkup(
{loaderData.dsn ? (
<meta name="dsn" content={loaderData.dsn} />
) : null}
<meta name="style-nonce" content={loaderData.nonce} />
<meta name="theme-color" content="#00a8f8" />
<meta
name="viewport"
Expand Down

0 comments on commit 5e7779a

Please sign in to comment.