Skip to content

Commit

Permalink
fix(start): fix script initialization order (#3215)
Browse files Browse the repository at this point in the history
  • Loading branch information
schiller-manuel authored Jan 22, 2025
1 parent b883fdd commit da84bbb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
30 changes: 28 additions & 2 deletions packages/react-router/src/ScriptOnce.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,40 @@
import jsesc from 'jsesc'
import { useRouter } from './useRouter'

export function ScriptOnce({
children,
log,
sync,
}: {
children: string
log?: boolean
sync?: boolean
}) {
const router = useRouter()
if (typeof document !== 'undefined') {
return null
}

router.injectScript(children, { logScript: log })
return null
if (!sync) {
router.injectScript(children, { logScript: log })
return null
}

return (
<script
className="tsr-once"
dangerouslySetInnerHTML={{
__html: [
children,
(log ?? true) && process.env.NODE_ENV === 'development'
? `console.info(\`Injected From Server:
${jsesc(children.toString(), { quotes: 'backtick' })}\`)`
: '',
'if (typeof __TSR__ !== "undefined") __TSR__.cleanScripts()',
]
.filter(Boolean)
.join('\n'),
}}
/>
)
}
3 changes: 2 additions & 1 deletion packages/start-client/src/Meta.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ export const useMetaElements = () => {
<Asset {...asset} key={`tsr-meta-${JSON.stringify(asset)}`} />
))}
<>
<ScriptOnce log={false} children={minifiedScript} />
<ScriptOnce log={false} children={minifiedScript} sync={true} />
<ScriptOnce
sync={true}
children={`__TSR__.dehydrated = ${jsesc(
router.options.transformer.stringify(dehydratedCtx),
{
Expand Down
1 change: 0 additions & 1 deletion packages/start-router-manifest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export function getFullRouterManifest() {
attrs: {
src: vinxiClientManifest.inputs[vinxiClientManifest.handler]?.output.path,
type: 'module',
async: true,
suppressHydrationWarning: true,
},
})
Expand Down

0 comments on commit da84bbb

Please sign in to comment.