QwikDev / qwik

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

[🐞] useLocation URL searchParams is null #4956

Open lbensaad opened 1 year ago

lbensaad commented 1 year ago

Which component is affected?

Qwik City (routing)

Describe the bug

Please note first that this bug appears only on SSG pages. It does not appear in dev mode. using qwik v1.2.7. I have a page with parameters like ?id=7, when I use a Link to navigate to that page then const id = loc.url.searchParams.get('id') will correctly return the id '7' but when I reload/refresh the page or directly navigate to the page then It will return a null. While const id = new URLSearchParams(document.location.search).get("id") will correctly get the id '7' This issue does not appear on pnpm dev mode, it works correctly. It appears only in SSG using pnpm build then serve the dist folder using http-server or nginx.

Add this code to file src/routes/index.tsx:

  const loc = useLocation();
  useVisibleTask$(()=>{
    const id1 = new URLSearchParams(document.location.search).get("id");
    const id2 = loc.url.searchParams.get("id");

    alert(`id1 = ${id1} vs id2 = ${id2}`);
  });

Add the adapter SSG: pnpm qwik add then select: Adapter: Static site (.html files) Build the static html files with pnpm build then serve the dist folder with http-server ./dist open the page giving it a parameter in the url like this http://localhost:8080/?id=7 you should get and alert with message: id1 = 7 vs id2 = 7 but you will get this alert message: id1 = 7 vs id2 = null

Reproduction

https://stackblitz.com/edit/qwik-starter-9umyqd

Steps to reproduce

run: pnpm i && pnpm build then http-server ./dist. After that open /?id=7

System Info

System:
    OS: Linux 5.15 Ubuntu 22.04.3 LTS 22.04.3 LTS (Jammy Jellyfish)
    CPU: (8) x64 Intel(R) Core(TM) i7-2670QM CPU @ 2.20GHz
    Memory: 4.31 GB / 15.59 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 19.7.0 - ~/.nvm/versions/node/v19.7.0/bin/node
    npm: 9.5.0 - ~/.nvm/versions/node/v19.7.0/bin/npm
  Browsers:
    Chrome: 115.0.5790.170
    Firefox: 116.0.2
  npmPackages:
    @builder.io/partytown: latest => 0.8.0 
    @builder.io/qwik: latest => 1.2.7 
    @builder.io/qwik-city: latest => 1.2.7 
    vite: latest => 4.4.9

Additional Information

No response

stackblitz[bot] commented 1 year ago

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

haitaojarvis commented 9 months ago

Met the exact same bug, hopefully some future minor release will fix this.

JerryWu1234 commented 1 month ago

I can try this

wmertens commented 1 month ago

Possibly related: #6753

JerryWu1234 commented 1 month ago

thank you for your tips