Skip to content

[🐞] Cannot access full url in useLocation hook #292

@CukierPo2zl

Description

@CukierPo2zl

Which component is affected?

Qwik City (routing)

Describe the bug

I am using client-side routing and navigating the page using id's and scrolling to the view.
I navigate using the useNavigate hook and trying to access the id (or full url) value in useLocation. Unfortunatelly useLocation doesn't include information that starts with a '#'.
eg.

const nav = useNavigate();
const handleClick = $(() => {
    nav(`#section`);
  });

After that url looks correctly

https://localhost:4200/#section

But cannot find any hash information

const loc = useLocation();
console.log(loc)
{
  hash: "",
  host: "localhost:4200",
  hostname: "localhost",
  href: "http://localhost:4200/",
  origin: "http://localhost:4200",
  password: "",
  pathname: "/",
  port: "4200",
  protocol:"http:",
  search: "",
  ...
}

any thoughts?

Reproduction

https://stackblitz.com/edit/qwik-starter-casjao?file=src%2Froutes%2Findex.tsx

Steps to reproduce

  • Navigate programmatically using useNavigate hook and direct to the html element's id (eg. nav('#hash'))
  • Try to access hash value or full url using useLocation hook

System Info

System:
    OS: macOS 13.0.1
    CPU: (8) arm64 Apple M1 Pro
    Memory: 77.41 MB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 19.3.0 - ~/.nvm/versions/node/v19.3.0/bin/node
    Yarn: 1.22.19 - /opt/homebrew/bin/yarn
    npm: 9.2.0 - ~/.nvm/versions/node/v19.3.0/bin/npm
  Browsers:
    Chrome: 114.0.5735.198
    Edge: 113.0.1774.57
    Safari: 16.1
  npmPackages:
    @builder.io/qwik: ~1.2.6 => 1.2.6 
    @builder.io/qwik-city: ~1.2.6 => 1.2.6 
    undici: ^5.22.0 => 5.22.1 
    vite: ^4.3.9 => 4.3.9

Additional Information

I think it might be affected by this PR QwikDev/qwik#4550

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    In Progress (STAGE 2)

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions