Skip to content

Commit

Permalink
fix: order of injected scripts
Browse files Browse the repository at this point in the history
fixes #3117
  • Loading branch information
schiller-manuel committed Jan 14, 2025
1 parent 999664e commit a6634f6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 27 deletions.
32 changes: 6 additions & 26 deletions packages/react-router/src/ScriptOnce.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,11 @@
import jsesc from 'jsesc'
import { useRouter } from './useRouter';

export function ScriptOnce({
className,
children,
log,
...rest
}: { children: string; log?: boolean } & React.HTMLProps<HTMLScriptElement>) {
if (typeof document !== 'undefined') {
return null
}

return (
<script
{...rest}
className={`tsr-once ${className || ''}`}
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'),
}}
/>
)
}: { children: string; log?: boolean }) {
const router = useRouter()

router.injectScript(children, {logScript: log})
return null
}
3 changes: 2 additions & 1 deletion packages/react-router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
import { Store, batch } from '@tanstack/react-store'
import invariant from 'tiny-invariant'
import warning from 'tiny-warning'
import jsesc from 'jsesc'
import { rootRouteId } from './root'
import { defaultParseSearch, defaultStringifySearch } from './searchParams'
import {
Expand Down Expand Up @@ -3100,7 +3101,7 @@ export class Router<
`<script class='tsr-once'>${script}${
process.env.NODE_ENV === 'development' && (opts?.logScript ?? true)
? `; console.info(\`Injected From Server:
${script}\`)`
${jsesc(script, { quotes: 'backtick' })}\`)`
: ''
}; if (typeof __TSR__ !== 'undefined') __TSR__.cleanScripts()</script>`,
)
Expand Down

0 comments on commit a6634f6

Please sign in to comment.