]> danix's work - danix.xyz-2.git/commitdiff
fix: use client-side language detection with redirect rules for 404 navigation
authorDanilo M. <redacted>
Fri, 17 Apr 2026 09:54:12 +0000 (11:54 +0200)
committerDanilo M. <redacted>
Fri, 17 Apr 2026 09:54:12 +0000 (11:54 +0200)
Rely on hugo.toml redirect rules to serve correct 404.html and detect language
from window.location.pathname in notFoundNav() Alpine component. Navigation
links now properly route to Italian (/it/*) or English (/*) sections based on
request URL.

Co-Authored-By: Claude Haiku 4.5 <redacted>
hugo.toml
themes/danix-xyz-hacker/assets/js/not-found-page.js
themes/danix-xyz-hacker/layouts/404.html

index f5019c0268d7d7387f3a8a18ce0431d24699754f..4913f63e18392d54b456e37a5cbcef048cfe4699 100644 (file)
--- a/hugo.toml
+++ b/hugo.toml
@@ -124,3 +124,13 @@ enableRobotsTXT = true
     label = "Tech"
     color_dark = "#a855f7"
     color_light = "#7c3aed"
+
+[[redirects]]
+  from = '/it/**'
+  to = '/it/404.html'
+  status = 404
+
+[[redirects]]  # Default language should be last.
+  from = '/**'
+  to = '/404.html'
+  status = 404
\ No newline at end of file
index 78e35911352e3aa55601c41b58b9061f6ce54e2d..cec60bd469b1f851276fe9536995c58dfdbfbbef 100644 (file)
@@ -31,17 +31,29 @@ document.addEventListener('alpine:init', () => {
     }
   }));
 
-  Alpine.data('notFoundNav', () => ({
-    get homeLink() {
-      return window.currentLang === 'it' ? '/it/' : '/';
-    },
-    get articlesLink() {
-      return window.currentLang === 'it' ? '/it/articles/' : '/articles/';
-    },
-    get contactLink() {
-      return window.currentLang === 'it' ? '/it/is/here/' : '/is/here/';
-    }
-  }));
+  Alpine.data('notFoundNav', () => {
+    const isItalian = window.location.pathname.startsWith('/it/');
+    return {
+      get homeLink() {
+        return isItalian ? '/it/' : '/';
+      },
+      get articlesLink() {
+        return isItalian ? '/it/articles/' : '/articles/';
+      },
+      get contactLink() {
+        return isItalian ? '/it/is/here/' : '/is/here/';
+      },
+      goHome() {
+        window.location.href = this.homeLink;
+      },
+      goArticles() {
+        window.location.href = this.articlesLink;
+      },
+      goContact() {
+        window.location.href = this.contactLink;
+      }
+    };
+  });
 
   console.log('notFoundPage Alpine component registered');
 });
index 0fb6c9a6ed213a17c2bb736da615b3eb4180b708..341f90b16e75e550b2e32d36d79b6ad961f6955c 100644 (file)
@@ -82,13 +82,13 @@ window.articlesData = [
 
     <!-- Navigation Links -->
     <div class="space-y-4 flex flex-col items-center mb-12" x-data="notFoundNav()">
-      <a :href="homeLink" class="btn btn-primary">
+      <a :href="homeLink" @click.prevent="goHome()" class="btn btn-primary">
         {{ i18n "goHome" }}
       </a>
-      <a :href="articlesLink" class="btn btn-secondary">
+      <a :href="articlesLink" @click.prevent="goArticles()" class="btn btn-secondary">
         {{ i18n "browseArticles" }}
       </a>
-      <a :href="contactLink" class="btn btn-outline">
+      <a :href="contactLink" @click.prevent="goContact()" class="btn btn-outline">
         {{ i18n "contactSupport" }}
       </a>
     </div>