Fix full page reload when clicking internal links in SPA apps
This commit is contained in:
@@ -15,19 +15,19 @@ crate-type = ["staticlib", "cdylib", "lib"]
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
tauri-build = { version = "2.4.0", features = [] }
|
tauri-build = { version = "2.4.1", features = [] }
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
serde_json = "1.0.143"
|
serde_json = "1.0.145"
|
||||||
serde = { version = "1.0.219", features = ["derive"] }
|
serde = { version = "1.0.228", features = ["derive"] }
|
||||||
tokio = { version = "1.47.1", features = ["full"] }
|
tokio = { version = "1.47.1", features = ["full"] }
|
||||||
tauri = { version = "2.8.4", features = ["tray-icon", "image-ico", "image-png", "macos-proxy"] }
|
tauri = { version = "2.8.5", features = ["tray-icon", "image-ico", "image-png", "macos-proxy"] }
|
||||||
tauri-plugin-window-state = "2.4.0"
|
tauri-plugin-window-state = "2.4.0"
|
||||||
tauri-plugin-oauth = "2.0.0"
|
tauri-plugin-oauth = "2.0.0"
|
||||||
tauri-plugin-http = "2.5.2"
|
tauri-plugin-http = "2.5.2"
|
||||||
tauri-plugin-global-shortcut = { version = "2.3.0" }
|
tauri-plugin-global-shortcut = { version = "2.3.0" }
|
||||||
tauri-plugin-shell = "2.3.1"
|
tauri-plugin-shell = "2.3.1"
|
||||||
tauri-plugin-single-instance = "2.3.3"
|
tauri-plugin-single-instance = "2.3.4"
|
||||||
tauri-plugin-notification = "2.3.1"
|
tauri-plugin-notification = "2.3.1"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
|
|||||||
60
src-tauri/src/inject/event.js
vendored
60
src-tauri/src/inject/event.js
vendored
@@ -385,20 +385,21 @@ document.addEventListener("DOMContentLoaded", () => {
|
|||||||
|
|
||||||
// Handle _blank links: same domain navigates in-app, cross-domain opens new window
|
// Handle _blank links: same domain navigates in-app, cross-domain opens new window
|
||||||
if (target === "_blank") {
|
if (target === "_blank") {
|
||||||
|
if (isSameDomain(absoluteUrl)) {
|
||||||
|
// For same-domain links, let the browser/SPA handle it naturally
|
||||||
|
// This prevents full page reload in SPA apps like Discord
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
e.stopImmediatePropagation();
|
e.stopImmediatePropagation();
|
||||||
|
const newWindow = originalWindowOpen.call(
|
||||||
if (isSameDomain(absoluteUrl)) {
|
window,
|
||||||
window.location.href = absoluteUrl;
|
absoluteUrl,
|
||||||
} else {
|
"_blank",
|
||||||
const newWindow = originalWindowOpen.call(
|
"width=1200,height=800,scrollbars=yes,resizable=yes",
|
||||||
window,
|
);
|
||||||
absoluteUrl,
|
if (!newWindow) handleExternalLink(absoluteUrl);
|
||||||
"_blank",
|
|
||||||
"width=1200,height=800,scrollbars=yes,resizable=yes",
|
|
||||||
);
|
|
||||||
if (!newWindow) handleExternalLink(absoluteUrl);
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -448,39 +449,24 @@ document.addEventListener("DOMContentLoaded", () => {
|
|||||||
// Rewrite the window.open function.
|
// Rewrite the window.open function.
|
||||||
const originalWindowOpen = window.open;
|
const originalWindowOpen = window.open;
|
||||||
window.open = function (url, name, specs) {
|
window.open = function (url, name, specs) {
|
||||||
// Apple login and google login
|
|
||||||
if (name === "AppleAuthentication") {
|
if (name === "AppleAuthentication") {
|
||||||
//do nothing
|
return originalWindowOpen.call(window, url, name, specs);
|
||||||
} else if (
|
}
|
||||||
specs &&
|
|
||||||
(specs.includes("height=") || specs.includes("width="))
|
try {
|
||||||
) {
|
|
||||||
location.href = url;
|
|
||||||
} else {
|
|
||||||
const baseUrl = window.location.origin + window.location.pathname;
|
const baseUrl = window.location.origin + window.location.pathname;
|
||||||
const hrefUrl = new URL(url, baseUrl);
|
const hrefUrl = new URL(url, baseUrl);
|
||||||
const absoluteUrl = hrefUrl.href;
|
const absoluteUrl = hrefUrl.href;
|
||||||
|
|
||||||
// Apply same domain logic as anchor links
|
if (!isSameDomain(absoluteUrl)) {
|
||||||
if (isSameDomain(absoluteUrl)) {
|
|
||||||
// Same domain: navigate in app or open new window based on specs
|
|
||||||
if (name === "_blank" || !name) {
|
|
||||||
return originalWindowOpen.call(
|
|
||||||
window,
|
|
||||||
absoluteUrl,
|
|
||||||
"_blank",
|
|
||||||
"width=1200,height=800,scrollbars=yes,resizable=yes",
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
location.href = absoluteUrl;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Cross domain: open in external browser
|
|
||||||
handleExternalLink(absoluteUrl);
|
handleExternalLink(absoluteUrl);
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return originalWindowOpen.call(window, absoluteUrl, name, specs);
|
||||||
|
} catch (error) {
|
||||||
|
return originalWindowOpen.call(window, url, name, specs);
|
||||||
}
|
}
|
||||||
// Call the original window.open function to maintain its normal functionality.
|
|
||||||
return originalWindowOpen.call(window, url, name, specs);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Set the default zoom, There are problems with Loop without using try-catch.
|
// Set the default zoom, There are problems with Loop without using try-catch.
|
||||||
|
|||||||
Reference in New Issue
Block a user