Skip to content

Commit 9f7774c

Browse files
committed
Upgrade to .NET 10 and add reconnect modal support
Upgraded the target framework to .NET 10 in `BlazorBootstrap.Demo.Server.csproj` and added the `BlazorDisableThrowNavigationException` property. Enhanced `App.razor` with `<ResourcePreloader />`, `<ImportMap />`, and a new `<ReconnectModal />` component for handling reconnection scenarios. Updated script references to use dynamic asset management. Added a `ReconnectModal.razor` component with a `<dialog>` element, user interaction buttons, and messages for reconnecting, retrying, and resuming sessions. Styled the modal with animations in `ReconnectModal.razor.css` and implemented reconnect logic in `ReconnectModal.razor.js`. Modified `_Imports.razor` to include layout components. Updated `Program.cs` to use `app.MapStaticAssets()` and `app.MapRazorComponents` for interactive server rendering and added custom status code handling. These changes improve user experience, modularity, and resilience to connectivity issues.
1 parent b15c7bd commit 9f7774c

7 files changed

Lines changed: 260 additions & 4 deletions

File tree

BlazorBootstrap.Demo.Server/BlazorBootstrap.Demo.Server.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net9.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
7+
<BlazorDisableThrowNavigationException>true</BlazorDisableThrowNavigationException>
78
<UserSecretsId>17d839f8-f71e-4856-829b-ccf23589bb77</UserSecretsId>
89
</PropertyGroup>
910

BlazorBootstrap.Demo.Server/Components/App.razor

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
77
<title>Blazor Bootstrap Demos & Examples | Enterprise-class Blazor Bootstrap Component library built on the Blazor and Bootstrap CSS frameworks | Blazor Bootstrap</title>
88
<base href="/" />
9+
<ResourcePreloader />
910
<link href="images/logo/128X128.png" rel="icon" type="image/png" />
1011
<!-- CSS only -->
1112
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-LN+7fdVzj6u52u30Kp6M/trliBMCMKTyK833zpbD+pXdCLuTusPj697FH4R/5mcr" crossorigin="anonymous">
@@ -14,6 +15,7 @@
1415
<link href="_content/BlazorBootstrap.Demo.RCL/css/blazorbootstrap.demo.rcl.css" rel="stylesheet" asp-append-version="true" />
1516
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.13.1/font/bootstrap-icons.min.css">
1617
<link href="BlazorBootstrap.Demo.Server.styles.css" rel="stylesheet" asp-append-version="true" />
18+
<ImportMap />
1719
<!-- Font Awesome -->
1820
<script src="https://kit.fontawesome.com/42379982c2.js" crossorigin="anonymous"></script>
1921

@@ -41,7 +43,8 @@
4143

4244
<body>
4345
<Routes @rendermode="InteractiveServer" />
44-
<script src="_framework/blazor.web.js"></script>
46+
<ReconnectModal />
47+
<script src="@Assets["_framework/blazor.web.js"]"></script>
4548
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/js/bootstrap.bundle.min.js" integrity="sha384-ndDqU0Gzau9qJ1lfW4pNLlhNTkCfHzAVBReH9diLvGRem5+R9g2FzA8ZGN954O5Q" crossorigin="anonymous"></script>
4649
<!-- Add chart.js reference if chart components are used in your application. -->
4750
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/4.4.1/chart.umd.js" integrity="sha512-ZwR1/gSZM3ai6vCdI+LVF1zSq/5HznD3ZSTk7kajkaj4D292NLuduDCO1c/NT8Id+jE58KYLKT7hXnbtryGmMg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<script type="module" src="@Assets["Components/Layout/ReconnectModal.razor.js"]"></script>
2+
3+
<dialog id="components-reconnect-modal" data-nosnippet>
4+
<div class="components-reconnect-container">
5+
<div class="components-rejoining-animation" aria-hidden="true">
6+
<div></div>
7+
<div></div>
8+
</div>
9+
<p class="components-reconnect-first-attempt-visible">
10+
Rejoining the server...
11+
</p>
12+
<p class="components-reconnect-repeated-attempt-visible">
13+
Rejoin failed... trying again in <span id="components-seconds-to-next-attempt"></span> seconds.
14+
</p>
15+
<p class="components-reconnect-failed-visible">
16+
Failed to rejoin.<br />Please retry or reload the page.
17+
</p>
18+
<button id="components-reconnect-button" class="components-reconnect-failed-visible">
19+
Retry
20+
</button>
21+
<p class="components-pause-visible">
22+
The session has been paused by the server.
23+
</p>
24+
<button id="components-resume-button" class="components-pause-visible">
25+
Resume
26+
</button>
27+
<p class="components-resume-failed-visible">
28+
Failed to resume the session.<br />Please reload the page.
29+
</p>
30+
</div>
31+
</dialog>
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
.components-reconnect-first-attempt-visible,
2+
.components-reconnect-repeated-attempt-visible,
3+
.components-reconnect-failed-visible,
4+
.components-pause-visible,
5+
.components-resume-failed-visible,
6+
.components-rejoining-animation {
7+
display: none;
8+
}
9+
10+
#components-reconnect-modal.components-reconnect-show .components-reconnect-first-attempt-visible,
11+
#components-reconnect-modal.components-reconnect-show .components-rejoining-animation,
12+
#components-reconnect-modal.components-reconnect-paused .components-pause-visible,
13+
#components-reconnect-modal.components-reconnect-resume-failed .components-resume-failed-visible,
14+
#components-reconnect-modal.components-reconnect-retrying,
15+
#components-reconnect-modal.components-reconnect-retrying .components-reconnect-repeated-attempt-visible,
16+
#components-reconnect-modal.components-reconnect-retrying .components-rejoining-animation,
17+
#components-reconnect-modal.components-reconnect-failed,
18+
#components-reconnect-modal.components-reconnect-failed .components-reconnect-failed-visible {
19+
display: block;
20+
}
21+
22+
23+
#components-reconnect-modal {
24+
background-color: white;
25+
width: 20rem;
26+
margin: 20vh auto;
27+
padding: 2rem;
28+
border: 0;
29+
border-radius: 0.5rem;
30+
box-shadow: 0 3px 6px 2px rgba(0, 0, 0, 0.3);
31+
opacity: 0;
32+
transition: display 0.5s allow-discrete, overlay 0.5s allow-discrete;
33+
animation: components-reconnect-modal-fadeOutOpacity 0.5s both;
34+
&[open]
35+
36+
{
37+
animation: components-reconnect-modal-slideUp 1.5s cubic-bezier(.05, .89, .25, 1.02) 0.3s, components-reconnect-modal-fadeInOpacity 0.5s ease-in-out 0.3s;
38+
animation-fill-mode: both;
39+
}
40+
41+
}
42+
43+
#components-reconnect-modal::backdrop {
44+
background-color: rgba(0, 0, 0, 0.4);
45+
animation: components-reconnect-modal-fadeInOpacity 0.5s ease-in-out;
46+
opacity: 1;
47+
}
48+
49+
@keyframes components-reconnect-modal-slideUp {
50+
0% {
51+
transform: translateY(30px) scale(0.95);
52+
}
53+
54+
100% {
55+
transform: translateY(0);
56+
}
57+
}
58+
59+
@keyframes components-reconnect-modal-fadeInOpacity {
60+
0% {
61+
opacity: 0;
62+
}
63+
64+
100% {
65+
opacity: 1;
66+
}
67+
}
68+
69+
@keyframes components-reconnect-modal-fadeOutOpacity {
70+
0% {
71+
opacity: 1;
72+
}
73+
74+
100% {
75+
opacity: 0;
76+
}
77+
}
78+
79+
.components-reconnect-container {
80+
display: flex;
81+
flex-direction: column;
82+
align-items: center;
83+
gap: 1rem;
84+
}
85+
86+
#components-reconnect-modal p {
87+
margin: 0;
88+
text-align: center;
89+
}
90+
91+
#components-reconnect-modal button {
92+
border: 0;
93+
background-color: #6b9ed2;
94+
color: white;
95+
padding: 4px 24px;
96+
border-radius: 4px;
97+
}
98+
99+
#components-reconnect-modal button:hover {
100+
background-color: #3b6ea2;
101+
}
102+
103+
#components-reconnect-modal button:active {
104+
background-color: #6b9ed2;
105+
}
106+
107+
.components-rejoining-animation {
108+
position: relative;
109+
width: 80px;
110+
height: 80px;
111+
}
112+
113+
.components-rejoining-animation div {
114+
position: absolute;
115+
border: 3px solid #0087ff;
116+
opacity: 1;
117+
border-radius: 50%;
118+
animation: components-rejoining-animation 1.5s cubic-bezier(0, 0.2, 0.8, 1) infinite;
119+
}
120+
121+
.components-rejoining-animation div:nth-child(2) {
122+
animation-delay: -0.5s;
123+
}
124+
125+
@keyframes components-rejoining-animation {
126+
0% {
127+
top: 40px;
128+
left: 40px;
129+
width: 0;
130+
height: 0;
131+
opacity: 0;
132+
}
133+
134+
4.9% {
135+
top: 40px;
136+
left: 40px;
137+
width: 0;
138+
height: 0;
139+
opacity: 0;
140+
}
141+
142+
5% {
143+
top: 40px;
144+
left: 40px;
145+
width: 0;
146+
height: 0;
147+
opacity: 1;
148+
}
149+
150+
100% {
151+
top: 0px;
152+
left: 0px;
153+
width: 80px;
154+
height: 80px;
155+
opacity: 0;
156+
}
157+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Set up event handlers
2+
const reconnectModal = document.getElementById("components-reconnect-modal");
3+
reconnectModal.addEventListener("components-reconnect-state-changed", handleReconnectStateChanged);
4+
5+
const retryButton = document.getElementById("components-reconnect-button");
6+
retryButton.addEventListener("click", retry);
7+
8+
const resumeButton = document.getElementById("components-resume-button");
9+
resumeButton.addEventListener("click", resume);
10+
11+
function handleReconnectStateChanged(event) {
12+
if (event.detail.state === "show") {
13+
reconnectModal.showModal();
14+
} else if (event.detail.state === "hide") {
15+
reconnectModal.close();
16+
} else if (event.detail.state === "failed") {
17+
document.addEventListener("visibilitychange", retryWhenDocumentBecomesVisible);
18+
} else if (event.detail.state === "rejected") {
19+
location.reload();
20+
}
21+
}
22+
23+
async function retry() {
24+
document.removeEventListener("visibilitychange", retryWhenDocumentBecomesVisible);
25+
26+
try {
27+
// Reconnect will asynchronously return:
28+
// - true to mean success
29+
// - false to mean we reached the server, but it rejected the connection (e.g., unknown circuit ID)
30+
// - exception to mean we didn't reach the server (this can be sync or async)
31+
const successful = await Blazor.reconnect();
32+
if (!successful) {
33+
// We have been able to reach the server, but the circuit is no longer available.
34+
// We'll reload the page so the user can continue using the app as quickly as possible.
35+
const resumeSuccessful = await Blazor.resumeCircuit();
36+
if (!resumeSuccessful) {
37+
location.reload();
38+
} else {
39+
reconnectModal.close();
40+
}
41+
}
42+
} catch (err) {
43+
// We got an exception, server is currently unavailable
44+
document.addEventListener("visibilitychange", retryWhenDocumentBecomesVisible);
45+
}
46+
}
47+
48+
async function resume() {
49+
try {
50+
const successful = await Blazor.resumeCircuit();
51+
if (!successful) {
52+
location.reload();
53+
}
54+
} catch {
55+
location.reload();
56+
}
57+
}
58+
59+
async function retryWhenDocumentBecomesVisible() {
60+
if (document.visibilityState === "visible") {
61+
await retry();
62+
}
63+
}

BlazorBootstrap.Demo.Server/Components/_Imports.razor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
@using Microsoft.JSInterop
99
@using BlazorBootstrap.Demo.Server
1010
@using BlazorBootstrap.Demo.Server.Components
11+
@using BlazorBootstrap.Demo.Server.Components.Layout

BlazorBootstrap.Demo.Server/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
2222
app.UseHsts();
2323
}
24-
24+
app.UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages: true);
2525
app.UseHttpsRedirection();
2626

27-
app.UseStaticFiles();
2827
app.UseAntiforgery();
2928

29+
app.MapStaticAssets();
3030
app.MapRazorComponents<BlazorBootstrap.Demo.Server.Components.App>()
3131
.AddInteractiveServerRenderMode()
3232
.AddAdditionalAssemblies(typeof(BlazorBootstrap.Demo.RCL.App).Assembly); ;

0 commit comments

Comments
 (0)