Skip to content

Commit

Permalink
render initial tags always, no weird empty div
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanflorence committed Feb 11, 2021
1 parent 5f911dc commit f72ff8c
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions packages/react/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -548,11 +548,10 @@ export function Scripts() {
serverHandoffString
} = useRemixEntryContext();

// browser renders nothing for this, server renders script tags
let serverOnlyTags = null;

if (serverHandoffString) {
let contextScript = `window.__remixContext = ${serverHandoffString};`;
let initialScripts = React.useMemo(() => {
let contextScript = serverHandoffString
? `window.__remixContext = ${serverHandoffString};`
: "";

let routeModulesScript = `${matches
.map(
Expand All @@ -566,9 +565,12 @@ export function Scripts() {
.map((match, index) => `${JSON.stringify(match.route.id)}:route${index}`)
.join(",")}};`;

serverOnlyTags = (
return (
<>
<script dangerouslySetInnerHTML={createHtml(contextScript)} />
<script
suppressHydrationWarning
dangerouslySetInnerHTML={createHtml(contextScript)}
/>
<script
dangerouslySetInnerHTML={createHtml(routeModulesScript)}
type="module"
Expand All @@ -577,7 +579,11 @@ export function Scripts() {
<script src={manifest.entryModuleUrl} type="module" />
</>
);
}
// disabled deps array because we are purposefully only rendering this once
// for hydration, after that we want to just continue rendering the initial
// scripts as they were when the page first loaded
// eslint-disable-next-line
}, []);

// avoid waterfall when importing the next route module
let nextMatches = React.useMemo(
Expand All @@ -601,7 +607,7 @@ export function Scripts() {
{dedupe(preloads).map(path => (
<link key={path} rel="modulepreload" href={path} />
))}
<div suppressHydrationWarning>{serverOnlyTags}</div>
{initialScripts}
</>
);
}
Expand Down

0 comments on commit f72ff8c

Please sign in to comment.