Developing
For improving performance of client hydration on the web, you can pass data from the server to the client. Data must be serializable to JSON.
Here’s an example of a simple useFetch:
import { setServerData, getServerData } from 'one'
type SafeURLs = `${`https://tamagui.dev` | `http://localhost`}${string}
const useFetch = async (url: SafeURLs) => { if (process.env.VITE_ENVIRONMENT === 'ssr') { // on the server, fetch and store during render, then return directly: // getServerData throws on the server const data = await fetch(url).then(res => res.json()) setServerData(url, data) return data }
// on the client, read the data the server stored return getServerData(url)}
export default async (props) => { const serverData = await useFetch(props.url) return <div />}
// can use it in loaders, too
export const loader = async ({ params }) => { await doSomething() setServerData(params.id, 'data') return {}}setServerData only runs on the server and throws elsewhere. getServerData only runs on the client and throws on the server. Keys can be any string; override the One.ClientData type with declare module 'one' to type them.
Edit this page on GitHub.