Preventing Duplicate Tabs in Blazor WebAssembly

I’m working on a Blazor WebAssembly application and encountering an issue where the same page can be opened in multiple tabs if the user either copies the URL or modifies the link in the browser. I want to prevent this behavior and ensure that if a tab with a specific URL is already open, a new tab with the same URL should not be opened.

Here’s what I’ve tried so far:

Using sessionStorage: I tried storing a flag in sessionStorage indicating that a particular URL is open. However, this approach doesn’t work when the user copies the URL into a new tab.

Handling navigation events: I attempted to handle the navigation events (@code OnInitializedAsync or NavigationManager.LocationChanged) to check if the URL is already open in another tab. But I couldn’t figure out how to reliably detect if the URL is open in another tab.

Is there a way to reliably prevent duplicate tabs in a Blazor WebAssembly application? Any help or guidance on approaching this problem would be greatly appreciated.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@page "/"
@inject IJSRuntime JSRuntime
<h3>My Component</h3>
</code>
<code>@page "/" @inject IJSRuntime JSRuntime <h3>My Component</h3> </code>
@page "/"
@inject IJSRuntime JSRuntime

<h3>My Component</h3>
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@code {
@inject NavigationManager nav ;
</code>
<code>@code { @inject NavigationManager nav ; </code>
@code {
    @inject NavigationManager nav ;
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> protected override async Task OnInitializedAsync()
{
var url = nav.Uri;
// Check if URL is already in session storage
var isInSessionStorage = await JSRuntime.InvokeAsync<bool>("checkIfURLInSessionStorage", url);
if (!isInSessionStorage)
{
// URL already visited, show alert
await JSRuntime.InvokeVoidAsync("alert", "This URL is already opened in another tab!");
await JSRuntime.InvokeVoidAsync("closeWindow");
return; // Stop further execution
}
}
}
</code>
<code> protected override async Task OnInitializedAsync() { var url = nav.Uri; // Check if URL is already in session storage var isInSessionStorage = await JSRuntime.InvokeAsync<bool>("checkIfURLInSessionStorage", url); if (!isInSessionStorage) { // URL already visited, show alert await JSRuntime.InvokeVoidAsync("alert", "This URL is already opened in another tab!"); await JSRuntime.InvokeVoidAsync("closeWindow"); return; // Stop further execution } } } </code>
    protected override async Task OnInitializedAsync()
    {
        var url = nav.Uri;

        // Check if URL is already in session storage
        var isInSessionStorage = await JSRuntime.InvokeAsync<bool>("checkIfURLInSessionStorage", url);

        if (!isInSessionStorage)
        {
            // URL already visited, show alert
          await JSRuntime.InvokeVoidAsync("alert", "This URL is already opened in another tab!");
            await JSRuntime.InvokeVoidAsync("closeWindow");
            return; // Stop further execution
        }

    }
}

Duplicate.js

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>window.checkIfURLInSessionStorage = function (url) {
var visitedURLs = JSON.parse(sessionStorage.getItem("visitedURLs")) || [];
// Check if the URL is in visitedURLs
if (visitedURLs.includes(url))
{
// If the URL is already in visitedURLs, return false
return false;
}
// If the URL is not in visitedURLs, add it to sessionStorage
visitedURLs.push(url);
sessionStorage.setItem("visitedURLs", JSON.stringify(visitedURLs));
// Return true since the URL was not found in visitedURLs
return true;
};
window.closeWindow = function closeWindow()
{
window.close();
}
</code>
<code>window.checkIfURLInSessionStorage = function (url) { var visitedURLs = JSON.parse(sessionStorage.getItem("visitedURLs")) || []; // Check if the URL is in visitedURLs if (visitedURLs.includes(url)) { // If the URL is already in visitedURLs, return false return false; } // If the URL is not in visitedURLs, add it to sessionStorage visitedURLs.push(url); sessionStorage.setItem("visitedURLs", JSON.stringify(visitedURLs)); // Return true since the URL was not found in visitedURLs return true; }; window.closeWindow = function closeWindow() { window.close(); } </code>
window.checkIfURLInSessionStorage = function (url) {
    var visitedURLs = JSON.parse(sessionStorage.getItem("visitedURLs")) || [];

    // Check if the URL is in visitedURLs
    if (visitedURLs.includes(url))
    {
        // If the URL is already in visitedURLs, return false
       
        return false;
    }

    // If the URL is not in visitedURLs, add it to sessionStorage
    visitedURLs.push(url);
    sessionStorage.setItem("visitedURLs", JSON.stringify(visitedURLs));

    // Return true since the URL was not found in visitedURLs
    return true;
};
window.closeWindow = function closeWindow()
{
    window.close();
}

Expected Behavior:

When a user tries to open a page that is already open in another tab (either by copying the URL or modifying the link in the browser), the existing tab should be focused instead of opening a new tab.

Environment:

Blazor WebAssembly

.NET 7.0

Visual Studio 2022

Browser: All browsers

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật