Replies: 10 comments
-
Ohh, that is an interesting idea. So we have this in const token = useResource$(function(this: nul | RequestEvent, ctx, req) {
console.log(this ? 'Server': 'Client');
return req?.cookie.get('token') || jsCookie.get('token');
}); Would you be interested in creating a PR fix for this? See |
Beta Was this translation helpful? Give feedback.
-
Thanks for the hint about Use Case 1: // Top-level function
const getApiUrlSsr = server$(async function() {
return this.env.get('API_URL');
});
// Component-level hook
const user = useResource$(async ctx => {
const apiUrl = isServer ? await getApiUrlSsr() : import.meta.env.PUBLIC_API_URL;
return fetch(`${apiUrl}/v1/user`).then(res => res.json());
}); Use Case 2: // Top-level function
const getTokenSsr = server$(async function() {
return this.cookie.get('token');
});
// Component-level hook
const token = useResource$(async ctx => {
return isServer ? await getTokenSsr() : jsCookie.get('token');
}); Does this work? I'll try soon to test it out. If this solution works, then I think we don't need to necessarily change the Thanks. |
Beta Was this translation helpful? Give feedback.
-
Also, I think just passing For example, those who need access to What's your opinion? |
Beta Was this translation helpful? Give feedback.
-
Qwik AI is awesome for clarifying simple questions. |
Beta Was this translation helpful? Give feedback.
-
I like your idea. I think all tasks functions could have Let me know if you are up for doing the work. Happy to guide you along. |
Beta Was this translation helpful? Give feedback.
-
I would be happy to contribute, but will be able to start next month. |
Beta Was this translation helpful? Give feedback.
-
Wondering one thing, how can we make the import the same, as currently |
Beta Was this translation helpful? Give feedback.
-
Hmm! That is a good point. Qwik cant depend on qwikcity. Maybe there is a simpler request interface which is not Qwik city specific but only platform specific? |
Beta Was this translation helpful? Give feedback.
-
We moved this issue to |
Beta Was this translation helpful? Give feedback.
-
Is your feature request related to a problem?
Yes, I'm facing a problem where I've two different API URLs for the same API Service, one URL is public-network facing and the other one is local-network facing.
And now when I use the
useResource$
hook which runs on bothserver (within local-network)
andclient (within public-network)
If the hook runs on the server then I need to access the following to do conditional fetch request:req.env.get('API_URL')
.SSR Mode:
req.env.get('API_URL')
CSR Mode:
import.meta.env.PUBLIC_API_URL
But because I can't RequestEvent object in the
useResource$
hook I can't do those conditionals API URL choosing based on SSR and CSR mode.Describe the solution you'd like
I would like to have access to the
RequestEvent
orRequestEventBase
object instance optionally as useResource$'s callback parameter.Use Case 1:
Use Case 2:
Describe alternatives you've considered
routeLoader
might be an option but that is currently inefficient for this kind of task in my opinion.For example for
Use Case 1
:I might get the
LOCAL API URL (from req.env.get('API_URL')
using routeLoader which is kinda weird because in SPA navigation the routeLoader will called but that value no use in CSR Mode / SPA mode. and also kinda exposes theLOCAL API URL
to the public source code.For example for
Use Case 2
:I might get the
token (from req.cookie)
using routeLoader which is okay, but when navigation is happening in SPA mode with CSR rendering I already have access todocument.cookie
which is enough for me to have cookie in place for me to use. So in SPA navigation, the routeLoader network call is wasted.Additional context
No response
Beta Was this translation helpful? Give feedback.
All reactions