routing fixed

This commit is contained in:
Sergey Yarkov 2026-02-25 10:02:06 +03:00
parent f3e4b3b9d8
commit adf3238fe3
16 changed files with 47 additions and 26 deletions

View File

@ -5,27 +5,26 @@ import UIString from "./UIString.astro";
import { getLanguageFromURL } from "../i18n/utils"; import { getLanguageFromURL } from "../i18n/utils";
const pageLang = getLanguageFromURL(Astro.url.pathname); const pageLang = getLanguageFromURL(Astro.url.pathname);
console.log(pageLang);
--- ---
<footer> <footer>
<div class='container'> <div class="container">
<div> <div>
<i> <i>
<UIString key={"footer.developed"} />: <TranslatedLink to='/contacts/'> Sergey Yarkov </TranslatedLink> <UIString key={"footer.developed"} />: <TranslatedLink to="/contacts/"> Sergey Yarkov </TranslatedLink>
&copy; {new Date().getFullYear()} &copy; {new Date().getFullYear()}
</i> </i>
</div> </div>
<div> <div>
<a href=`/${pageLang}/rss.xml` target='_blank'><UIString key={"footer.rssFeed"} /></a> <a href=`/${pageLang}/rss.xml` target="_blank"><UIString key={"footer.rssFeed"} /></a>
<span> <span>
<Icon name='rss' width='20' height='20' /> <Icon name="rss" width="20" height="20" />
</span> </span>
</div> </div>
</div> </div>
</footer> </footer>
<style lang='scss'> <style lang="scss">
footer { footer {
font-weight: 500; font-weight: 500;
margin-top: 5rem; margin-top: 5rem;

View File

@ -17,8 +17,17 @@ export function getLanguageFromURL(pathname: string): LanguageKeys {
return DEFAULT_LANGUAGE; return DEFAULT_LANGUAGE;
} }
// Call only from ./src/[lang]/page.astro
export function isPageLangSupported(Astro: Readonly<AstroGlobal>) {
const params = Astro.params as { lang: string };
if (SUPPORTED_LANGUAGES.includes(params.lang as LanguageKeys)) return true;
return false;
}
export function removeLangFromURL(pathname: string): string { export function removeLangFromURL(pathname: string): string {
return pathname.split("/").slice(2).join("/"); const splittedPath = pathname.split("/");
if (splittedPath.length >= 3) return "/" + splittedPath.slice(2).join("/");
return pathname;
} }
function transformExports<T>(modules: Record<string, { default: T }>) { function transformExports<T>(modules: Record<string, { default: T }>) {

View File

@ -14,10 +14,7 @@ const { title, description, lang = DEFAULT_LANGUAGE, noIndex = false } = Astro.p
--- ---
<html lang={lang}> <html lang={lang}>
<BaseHead <BaseHead title={title ? `${title} | ${SITE_METADATA.title}` : SITE_METADATA.title} description={description}>
title={title ? `${title} | ${SITE_METADATA.title}` : SITE_METADATA.title}
description={description}
>
{noIndex && <meta name="robots" content="noindex" />} {noIndex && <meta name="robots" content="noindex" />}
</BaseHead> </BaseHead>
<BaseLayout> <BaseLayout>

View File

@ -1,5 +1,5 @@
--- ---
import NotFoundPage from "@templates/NotFound.astro"; import NotFoundTemplate from "@templates/NotFound.astro";
--- ---
<NotFoundPage /> <NotFoundTemplate />

View File

@ -1,5 +1,8 @@
--- ---
import AboutTemplate from "@templates/About.astro"; import AboutTemplate from "@templates/About.astro";
import { isPageLangSupported } from "@i18n/utils";
if (!isPageLangSupported(Astro)) return Astro.redirect("/404");
--- ---
<AboutTemplate /> <AboutTemplate />

View File

@ -1,5 +1,8 @@
--- ---
import ArticlesTemplate from "@templates/Articles.astro"; import ArticlesTemplate from "@templates/Articles.astro";
import { isPageLangSupported } from "@i18n/utils";
if (!isPageLangSupported(Astro)) return Astro.redirect("/404");
--- ---
<ArticlesTemplate /> <ArticlesTemplate />

View File

@ -1,5 +1,8 @@
--- ---
import ContactsTemplate from "@templates/Contacts.astro"; import ContactsTemplate from "@templates/Contacts.astro";
import { isPageLangSupported } from "@i18n/utils";
if (!isPageLangSupported(Astro)) return Astro.redirect("/404");
--- ---
<ContactsTemplate /> <ContactsTemplate />

View File

@ -1,5 +1,8 @@
--- ---
import HomeTemplate from "@templates/Home.astro"; import HomeTemplate from "@templates/Home.astro";
import { isPageLangSupported } from "@i18n/utils";
if (!isPageLangSupported(Astro)) return Astro.redirect("/404");
--- ---
<HomeTemplate /> <HomeTemplate />

View File

@ -1,5 +1,8 @@
--- ---
import ProjectsTemplate from "@templates/Projects.astro"; import ProjectsTemplate from "@templates/Projects.astro";
import { isPageLangSupported } from "@i18n/utils";
if (!isPageLangSupported(Astro)) return Astro.redirect("/404");
--- ---
<ProjectsTemplate /> <ProjectsTemplate />

View File

@ -1,5 +1,5 @@
--- ---
import AboutPage from "@templates/About.astro"; import AboutTemplate from "@templates/About.astro";
--- ---
<AboutPage /> <AboutTemplate />

View File

@ -1,5 +1,5 @@
--- ---
import ArticlesPage from "@templates/Articles.astro"; import ArticlesTemplate from "@templates/Articles.astro";
--- ---
<ArticlesPage /> <ArticlesTemplate />

View File

@ -2,7 +2,8 @@
import ArticleLayout from "@root/layouts/article.astro"; import ArticleLayout from "@root/layouts/article.astro";
import { getArticleTranslationsBySlug, incArticleTranslationViews } from "@/src/queries"; import { getArticleTranslationsBySlug, incArticleTranslationViews } from "@/src/queries";
const { url } = Astro.params as { blog: string; url: string }; // Example of an URL: https://website.com/blog/01-01-2025/my-article/
const { url } = Astro.params as { url: string };
const slug = url.split("/").at(-1); const slug = url.split("/").at(-1);
if (!slug) return Astro.redirect("/404"); if (!slug) return Astro.redirect("/404");

View File

@ -1,5 +1,5 @@
--- ---
import ContactsPage from "@root/templates/Contacts.astro"; import ContactsTemplate from "@templates/Contacts.astro";
--- ---
<ContactsPage /> <ContactsTemplate />

View File

@ -1,5 +1,5 @@
--- ---
import Home from "@root/templates/Home.astro"; import HomeTemplate from "@templates/Home.astro";
--- ---
<Home /> <HomeTemplate />

View File

@ -1,5 +1,5 @@
--- ---
import ProjectsPage from "@root/templates/Projects.astro"; import ProjectsTemplate from "@templates/Projects.astro";
--- ---
<ProjectsPage /> <ProjectsTemplate />

View File

@ -22,8 +22,8 @@ const t = useTranslation(Astro);
<p><UIString key={"splash.description"} /></p> <p><UIString key={"splash.description"} /></p>
</SplashHeading> </SplashHeading>
<SplashLinks> <SplashLinks>
<SplashLink icon='information' href='/about/'><UIString key={"splash.about"} /></SplashLink> <SplashLink icon="information" href="/about/"><UIString key={"splash.about"} /></SplashLink>
<SplashLink icon='email_filled' href='/contacts/'> <SplashLink icon="email_filled" href="/contacts/">
<UIString key={"splash.contact"} /> <UIString key={"splash.contact"} />
</SplashLink> </SplashLink>
</SplashLinks> </SplashLinks>