Skip to content

Commit 2b08766

Browse files
authored
fix: solve prevUrl on first render issue (#7082)
1 parent 5450c7e commit 2b08766

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

.changeset/dry-jobs-repair.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@builder.io/qwik-city': patch
3+
---
4+
5+
FIX: the previous URL now is undefined on first render.

packages/docs/src/routes/docs/(qwikcity)/api/index.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ export const BackButton = component$(() => {
245245

246246
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.
247247

248+
> 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).
249+
250+
248251
## `routeLoader$()`
249252

250253
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.

packages/qwik-city/src/runtime/src/qwik-city-component.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,10 @@ export const QwikCityProvider = component$<QwikCityProps>((props) => {
375375
}
376376

377377
// Update route location
378-
routeLocation.prevUrl = prevUrl;
378+
if (!isSamePath(trackUrl, prevUrl)) {
379+
routeLocation.prevUrl = prevUrl;
380+
}
381+
379382
routeLocation.url = trackUrl;
380383
routeLocation.params = { ...params };
381384

0 commit comments

Comments
 (0)