Skip to content
Discussion options

You must be logged in to vote

If the response is serializable then you can render it in a script.

// app/root.tsx
export async function loader() {
  let response = await fetch(url)
  let data = validate(await response.json())
  return json({ data })
}

export default function Component() {
  let { data } = useLoaderData<typeof loader>();
  return (
    <>
      <Outlet />
      <script id="entry-data" type="application/json" dangerouslySetInnerHtml={{ __html: data }} />
    </>
  )
}
// app/entry.client.tsx
// imports here

let data = JSON.parse(document.getDocumentById("entry-data").innerText ?? "{}")
// use data

// hydrate the app

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@tschmidt01
Comment options

@sergiodxa
Comment options

Answer selected by tschmidt01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants