Comments (1)
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)
- [🐞] Sitemap is not generating / finding routes when using localized routing HOT 1
- [✅] The Edge Function "_qwik-city" is referencing unsupported modules HOT 1
- [🐞] useStylesScoped$ doesn't support nested CSS HOT 8
- [🐞] Qwik Insights returns 500 error for fetching data/strategy HOT 3
- [🐞] Invalid module "@qwik-city-plan" is not a valid package name imported from HOT 12
- Code split loading[🐞] HOT 2
- [🐞] Error Integrating React Libraries (Material ui) in Qwik Framework HOT 1
- [🐞] ERR_TOO_MANY_REDIRECTS – Redirect loop on qwik.dev HOT 2
- [🐞] Qwik Form resets translated strings on submit to original language (qwik-speak) HOT 2
- [🐞] Cannot use node.js module inside Endpoint because of rollup compilation error HOT 4
- [✨] feat: add support for eslint flat config - eslint-plugin-qwik HOT 2
- [🐞] Missing "export" in some types
- [🐞] In dev mode when saving a change, it sometimes gives an error "aborted"
- [🐞] The CJS build of Vite's Node API is deprecated HOT 1
- [🐞] Bun adapter breaks / hangs build
- [🐞] Excalidraw Integration HOT 1
- Qwik City injects style for `view-transition-name: none`
- [🐞] Build static with custom origin protocol is broken HOT 4
- [📖]Could add a demo like "Learn Next.js"?
- [🐞] View transition and scroll restore conflicts
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from qwik.