Skip to content

Commit 2345cb4

Browse files
committed
fix: prevent stale dashboard chunks during startup
1 parent 235c111 commit 2345cb4

3 files changed

Lines changed: 69 additions & 1 deletion

File tree

dashboard/src/app/AppRouter.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { NotFoundPage } from '@/routes/NotFoundPage';
77
import { BlankLayout } from '@/layouts/blank/BlankLayout';
88
import { FullLayout } from '@/layouts/full/FullLayout';
99
import { coreRouteModuleLoaders } from '@/app/coreRouteModules';
10+
import { RouteErrorPage } from '@/app/RouteErrorPage';
1011

1112
const WelcomePage = lazy(() => import('@/routes/welcome/WelcomePage'));
1213
const AboutPage = lazy(() => import('@/routes/about/AboutPage'));
@@ -94,10 +95,12 @@ function routesForLayout(layout: RouteLayout) {
9495
const router = createHashRouter([
9596
{
9697
element: <BlankLayout />,
98+
errorElement: <RouteErrorPage />,
9799
children: routesForLayout('protected-blank'),
98100
},
99101
{
100102
element: <FullLayout />,
103+
errorElement: <RouteErrorPage />,
101104
children: [...routesForLayout('protected-full'), { path: '*', element: <NotFoundPage /> }],
102105
},
103106
]);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { useRouteError } from 'react-router-dom';
2+
3+
function errorMessage(error: unknown) {
4+
if (error instanceof Error) return error.message;
5+
if (typeof error === 'string') return error;
6+
return 'An unexpected route error occurred.';
7+
}
8+
9+
export function RouteErrorPage() {
10+
const error = useRouteError();
11+
12+
const reloadFreshDocument = () => {
13+
const url = new URL(window.location.href);
14+
url.searchParams.set('_desktop_reload', Date.now().toString());
15+
window.location.replace(url.toString());
16+
};
17+
18+
return (
19+
<main className="app-error" role="alert">
20+
<h1>Dashboard failed to load</h1>
21+
<p>{errorMessage(error)}</p>
22+
<button type="button" onClick={reloadFreshDocument}>
23+
Reload
24+
</button>
25+
</main>
26+
);
27+
}

src-tauri/src/window/main_window.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
use std::time::{SystemTime, UNIX_EPOCH};
2+
13
use tauri::{AppHandle, Manager};
4+
use url::Url;
25

36
pub fn show_main_window<F>(app_handle: &AppHandle, log: F)
47
where
@@ -50,8 +53,13 @@ pub fn navigate_main_window_to_backend(
5053
app_handle: &AppHandle,
5154
backend_url: &str,
5255
) -> Result<(), String> {
56+
let cache_buster = SystemTime::now()
57+
.duration_since(UNIX_EPOCH)
58+
.map(|duration| duration.as_millis())
59+
.unwrap_or_default();
60+
let navigation_url = backend_navigation_url(backend_url, cache_buster);
5361
let backend_url_json =
54-
serde_json::to_string(backend_url).unwrap_or_else(|_| "\"/\"".to_string());
62+
serde_json::to_string(&navigation_url).unwrap_or_else(|_| "\"/\"".to_string());
5563
let Some(window) = app_handle.get_webview_window("main") else {
5664
return Err("Main window is unavailable after backend startup.".to_string());
5765
};
@@ -61,3 +69,33 @@ pub fn navigate_main_window_to_backend(
6169
.eval(&js)
6270
.map_err(|error| format!("Failed to navigate to backend dashboard: {error}"))
6371
}
72+
73+
fn backend_navigation_url(backend_url: &str, cache_buster: u128) -> String {
74+
let Ok(mut url) = Url::parse(backend_url) else {
75+
return backend_url.to_string();
76+
};
77+
url.query_pairs_mut()
78+
.append_pair("_desktop_boot", &cache_buster.to_string());
79+
url.into()
80+
}
81+
82+
#[cfg(test)]
83+
mod tests {
84+
use super::backend_navigation_url;
85+
86+
#[test]
87+
fn backend_navigation_adds_a_document_cache_buster() {
88+
assert_eq!(
89+
backend_navigation_url("http://127.0.0.1:6185", 1234),
90+
"http://127.0.0.1:6185/?_desktop_boot=1234"
91+
);
92+
}
93+
94+
#[test]
95+
fn backend_navigation_preserves_existing_query_parameters() {
96+
assert_eq!(
97+
backend_navigation_url("http://127.0.0.1:6185/?channel=stable", 1234),
98+
"http://127.0.0.1:6185/?channel=stable&_desktop_boot=1234"
99+
);
100+
}
101+
}

0 commit comments

Comments
 (0)