dotnet / dotnet/aspnetcore

NavigationManager.NavigateTo with ReplaceHistoryEntry and HistoryEntryState doesn't update browser history state without Task.Yield

Open
#66,193 2 comments 0 reactions 0 assignees View on GitHub
area-blazor bug Docs
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

When calling `NavigationManager.NavigateTo` with `ReplaceHistoryEntry = true` and `HistoryEntryState` set, the browser history state is not actually updated unless a `Task.Yield()` is inserted before the next navigation call.

This means the `ReplaceHistoryEntry` + `HistoryEntryState` combination silently fails to persist the state into the browser's history entry.

### Expected Behavior

`NavigationManager.NavigateTo` with `ReplaceHistoryEntry = true` and `HistoryEntryState` should synchronously (or at least reliably) update the browser history state without requiring a `Task.Yield()` workaround.

### Steps To Reproduce

```csharp
NavigationManager.NavigateTo(
NavigationManager.Uri,
new NavigationOptions
{
ReplaceHistoryEntry = true,
HistoryEntryState = state
}
);

// Without this Task.Yield(), the history state update above is lost
await Task.Yield();

Navigator.OpenEntryDetail(newUrl);
```

Without the `await Task.Yield()` between the two calls, the `HistoryEntryState` set in the first `NavigateTo` call is not applied to the browser history. Adding the `Task.Yield()` gives the framework enough time to flush the state update to the browser before the subsequent navigation occurs.

### Exceptions (if any)

_No response_

### .NET Version

10.0 (and all other versions)

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.