qwik icon indicating copy to clipboard operation
qwik copied to clipboard

SSR | OnGet | UseEndpoint.resolved

Open devagja opened this issue 2 years ago • 3 comments

Qwik Version

"@builder.io/qwik": "0.0.105",

Operating System (or Browser)

all

Node Version (if applicable)

No response

Which component is affected?

Qwik City

Expected Behaviour

Previously this problem has not been solved

ES: Anteriormente este problema no ha podido ser resuelto

Actual Behaviour

We want to render according to the header of the request two different components (for example), one for desktop and one for mobile, by default in the store we mark to render the mobile, this causes a choice issue on desktop devices due to the order of execution. Given the:

  1. onGet -> store default value mobile -> render mobile component
  2. useEndpoint.resolved -> store changes value to desktop -> render desktop component

ES: Queremos renderizar según el encabezado de la solicitud dos componentes diferentes (por ejemplo), uno para escritorio y otro para móvil, por defecto en la store marcamos para renderizar el móvil, esto provoca un problema de elección en los dispositivos de escritorio debido al orden de ejecución. Dado que:

  1. onGet -> se define en la store el valor móvil como predeterminado -> renderizar componente móvil
  2. useEndpoint.resolved -> la store altera el valor a escritorio -> renderizar componente de escritorio

Additional Information

example repo:

https://github.com/devagja/qwik-ssr-headers

devagja avatar Aug 29 '22 09:08 devagja

Is it possible to execute the promise in the store declaration?

In this way we would set the default value of the store with the result of the execution of the server directly

ES: ¿es posible de alguna forma ejecutar la promesa en la declaracion del store ?

De esta forma el default value de la store sería el resultado de la ejecución del servidor directamente

devagja avatar Aug 29 '22 09:08 devagja

I think what you want can be achieved by setting up a store with an undefined value, and then writing to the store in the <Resource>'s onResolved handler?

petebacondarwin avatar Oct 04 '22 08:10 petebacondarwin

How about ?

const resource = useEndpoint<typeof onGet>();
const store = useStore({
  value: null,
});
useServerMount$(async () => {
  store.value = await resource.promise;
});

utherpally avatar Oct 04 '22 09:10 utherpally

Yes I think @petebacondarwin suggestion would work: https://github.com/BuilderIO/qwik/issues/1164#issuecomment-1266559329

adamdbradley avatar Oct 31 '22 19:10 adamdbradley