Skip to content

Commit

Permalink
fix: solve prevUrl on first render issue (#7082)
Browse files Browse the repository at this point in the history
  • Loading branch information
damianpumar authored Nov 22, 2024
1 parent 5450c7e commit 2b08766
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/dry-jobs-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@builder.io/qwik-city': patch
---

FIX: the previous URL now is undefined on first render.
3 changes: 3 additions & 0 deletions packages/docs/src/routes/docs/(qwikcity)/api/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ export const BackButton = component$(() => {

The fallback in the nav function ensures that if the previous URL (loc.prevUrl) is not available, the user is redirected to a default path (eg. the root path /). This is useful in scenarios where the navigation history might not include a previous URL, such as when the user directly lands on a specific page without navigating from another page within the app.

> Notice that the **loc.prevUrl** is only available when the user navigates to a page using the [`useNavigate()`](#usenavigate) or [`<Link>`](/docs/cookbook/nav-link). It is not available when the user navigates to a page using [`the anchor element`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a).

## `routeLoader$()`

The `routeLoader$()` function is used to declare a new Server Loader in the given page/middleware or layout. Qwik City will execute all the declared loaders for the given route match. Qwik Components can later reference the loaders by importing them and calling the returned custom hook function in order to retrieve the data.
Expand Down
5 changes: 4 additions & 1 deletion packages/qwik-city/src/runtime/src/qwik-city-component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,10 @@ export const QwikCityProvider = component$<QwikCityProps>((props) => {
}

// Update route location
routeLocation.prevUrl = prevUrl;
if (!isSamePath(trackUrl, prevUrl)) {
routeLocation.prevUrl = prevUrl;
}

routeLocation.url = trackUrl;
routeLocation.params = { ...params };

Expand Down

0 comments on commit 2b08766

Please sign in to comment.