QwikDev / qwik

Instant-loading web apps, without effort
https://qwik.dev
MIT License
20.64k stars 1.28k forks source link

[🐞] `useServerData(key)` doesn't return anything after qwik-city navigation #4506

Open riencoertjens opened 1 year ago

riencoertjens commented 1 year ago

Which component is affected?

Qwik Runtime

Describe the bug

useServerData(key) doesn't return the data after a qwik-city navigation, both in dev and preview

Reproduction

https://github.com/riencoertjens/qwik-redirect-issue-reproduction/tree/server-data-issue

Steps to reproduce

expectation: shows serverData added in entry.ssr.ts

what happens: no data is shown

it does work on initial load or when using an <a /> to do the navigation,

System Info

System:
    OS: macOS 13.4
    CPU: (10) arm64 Apple M1 Pro
    Memory: 130.25 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.16.0 - ~/.nvm/versions/node/v18.16.0/bin/node
    npm: 9.5.1 - ~/.nvm/versions/node/v18.16.0/bin/npm
  npmPackages:
    @builder.io/qwik: ^1.1.4 => 1.1.4 
    @builder.io/qwik-city: ^1.1.4 => 1.1.4 
    undici: 5.22.1 => 5.22.1 
    vite: 4.3.5 => 4.3.5

Additional Information

No response

manucorporat commented 1 year ago

It's by design that useServerData only works in server, in fact I would say, it's a very low level API, that most developers should never have to use. Ie, if you have to use it... something else might be wrong with qwik.

Kasheftin commented 2 months ago

How to manage nonce then? I did it according to this example, https://qwik.dev/docs/advanced/content-security-policy/. There's a script that is loaded dynamically (google auth). const nonce = useServerData<string | undefined>('nonce') is undefined there. By dynamic I mean a user clicks on a button, and then a client component is being loaded. It loads an external script with nonce.

PatrickJS commented 2 months ago

for now, in the ssr.entry you can add it as a prop to Root then query for that value