Skip to content

Commit 94ffcee

Browse files
committed
Fix full page reload when clicking internal links in SPA apps
1 parent e7c10f6 commit 94ffcee

File tree

2 files changed

+28
-42
lines changed

2 files changed

+28
-42
lines changed

src-tauri/Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ crate-type = ["staticlib", "cdylib", "lib"]
1515
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1616

1717
[build-dependencies]
18-
tauri-build = { version = "2.4.0", features = [] }
18+
tauri-build = { version = "2.4.1", features = [] }
1919

2020
[dependencies]
21-
serde_json = "1.0.143"
22-
serde = { version = "1.0.219", features = ["derive"] }
21+
serde_json = "1.0.145"
22+
serde = { version = "1.0.228", features = ["derive"] }
2323
tokio = { version = "1.47.1", features = ["full"] }
24-
tauri = { version = "2.8.4", features = ["tray-icon", "image-ico", "image-png", "macos-proxy"] }
24+
tauri = { version = "2.8.5", features = ["tray-icon", "image-ico", "image-png", "macos-proxy"] }
2525
tauri-plugin-window-state = "2.4.0"
2626
tauri-plugin-oauth = "2.0.0"
2727
tauri-plugin-http = "2.5.2"
2828
tauri-plugin-global-shortcut = { version = "2.3.0" }
2929
tauri-plugin-shell = "2.3.1"
30-
tauri-plugin-single-instance = "2.3.3"
30+
tauri-plugin-single-instance = "2.3.4"
3131
tauri-plugin-notification = "2.3.1"
3232

3333
[features]

src-tauri/src/inject/event.js

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -385,20 +385,21 @@ document.addEventListener("DOMContentLoaded", () => {
385385

386386
// Handle _blank links: same domain navigates in-app, cross-domain opens new window
387387
if (target === "_blank") {
388-
e.preventDefault();
389-
e.stopImmediatePropagation();
390-
391388
if (isSameDomain(absoluteUrl)) {
392-
window.location.href = absoluteUrl;
393-
} else {
394-
const newWindow = originalWindowOpen.call(
395-
window,
396-
absoluteUrl,
397-
"_blank",
398-
"width=1200,height=800,scrollbars=yes,resizable=yes",
399-
);
400-
if (!newWindow) handleExternalLink(absoluteUrl);
389+
// For same-domain links, let the browser/SPA handle it naturally
390+
// This prevents full page reload in SPA apps like Discord
391+
return;
401392
}
393+
394+
e.preventDefault();
395+
e.stopImmediatePropagation();
396+
const newWindow = originalWindowOpen.call(
397+
window,
398+
absoluteUrl,
399+
"_blank",
400+
"width=1200,height=800,scrollbars=yes,resizable=yes",
401+
);
402+
if (!newWindow) handleExternalLink(absoluteUrl);
402403
return;
403404
}
404405

@@ -448,39 +449,24 @@ document.addEventListener("DOMContentLoaded", () => {
448449
// Rewrite the window.open function.
449450
const originalWindowOpen = window.open;
450451
window.open = function (url, name, specs) {
451-
// Apple login and google login
452452
if (name === "AppleAuthentication") {
453-
//do nothing
454-
} else if (
455-
specs &&
456-
(specs.includes("height=") || specs.includes("width="))
457-
) {
458-
location.href = url;
459-
} else {
453+
return originalWindowOpen.call(window, url, name, specs);
454+
}
455+
456+
try {
460457
const baseUrl = window.location.origin + window.location.pathname;
461458
const hrefUrl = new URL(url, baseUrl);
462459
const absoluteUrl = hrefUrl.href;
463460

464-
// Apply same domain logic as anchor links
465-
if (isSameDomain(absoluteUrl)) {
466-
// Same domain: navigate in app or open new window based on specs
467-
if (name === "_blank" || !name) {
468-
return originalWindowOpen.call(
469-
window,
470-
absoluteUrl,
471-
"_blank",
472-
"width=1200,height=800,scrollbars=yes,resizable=yes",
473-
);
474-
} else {
475-
location.href = absoluteUrl;
476-
}
477-
} else {
478-
// Cross domain: open in external browser
461+
if (!isSameDomain(absoluteUrl)) {
479462
handleExternalLink(absoluteUrl);
463+
return null;
480464
}
465+
466+
return originalWindowOpen.call(window, absoluteUrl, name, specs);
467+
} catch (error) {
468+
return originalWindowOpen.call(window, url, name, specs);
481469
}
482-
// Call the original window.open function to maintain its normal functionality.
483-
return originalWindowOpen.call(window, url, name, specs);
484470
};
485471

486472
// Set the default zoom, There are problems with Loop without using try-catch.

0 commit comments

Comments
 (0)