Giter Site home page Giter Site logo

Comments (1)

wmertens avatar wmertens commented on May 19, 2024

You can make this as follows. It would be great if you contribute a cookbook entry that explains this in a way that others at your company and elsewhere understand it easily.

The idea is that during SSR, you store certain data that never changes at the root level, and since root never rerenders, it will never need to be loaded again on the client.

  • Define a getClient() function that reads the data from disk
  • Make something like this
export const clientCtx = createContextId<Signal<ClientConfig>>('c')

export const useClientProvider = () => {
	const loc = useLocation()
	const clientS = useSignal<ClientConfig>({theme: {}} as ClientConfig)
	useContextProvider(clientCtx, clientS)

	useTask$(async () => {
		console.log('get client')
		clientS.value = await getClient(loc.url.hostname)
		console.log('got client')
	})
}
const o = c => console.log(c) || c
// Note, this removes reactivity but the value is static so it's fine
export const useClient = () => o(useContext(clientCtx).value)
  • In your root layout, add useClientProvider()
  • Anywhere you need the data, add const client = useClient()

If you need to use the client info in some server function, use the getClient function. You can also cache the result in module scope.

from qwik.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.