Update Astro & created custom middleware

- Updated Astro to newest version (4.11.0)
- Created custom middleware (middleware.js), to extend on Astro's default i18n
- Code cleanup and bugfixes
This commit is contained in:
2024-06-20 14:17:06 +02:00
parent 3b33ffa05d
commit cd27a8e508
12 changed files with 787 additions and 829 deletions

View File

@ -90,7 +90,7 @@ export function ContactForm({ currentLocale }) {
<FormControl>
<Input
placeholder={t("contact").nameDefaultValue}
className="col-span-3"
className="col-span-3 text-foreground"
{...field}
/>
</FormControl>
@ -111,7 +111,7 @@ export function ContactForm({ currentLocale }) {
<FormControl>
<Input
placeholder={t("contact").emailDefaultValue}
className="col-span-3"
className="col-span-3 text-foreground"
type="email"
{...field}
/>
@ -133,7 +133,7 @@ export function ContactForm({ currentLocale }) {
<FormControl>
<Textarea
placeholder={t("contact").messageDefaultValue}
className="col-span-3"
className="col-span-3 text-foreground"
{...field}
/>
</FormControl>

View File

@ -24,10 +24,16 @@ const t = useTranslations(getLangFromUrl(Astro.url))
<div class="mt-[5.5rem] w-full grid justify-around grid-cols-2">
<button
type="button"
onclick="linkToBlog()"
class="text-3xl bg-accent text-bkg p-1 rounded-md bg-opacity-75 hover:bg-opacity-100 m-2"
>
{t("hero").buttons[0]}
</button>
<ContactForm currentLocale={Astro.currentLocale} client:load />
</div>
<script client:load is:inline>
function linkToBlog() {
document.getElementById("blogBtn").click()
}
</script>
</SinglePage>

View File

@ -10,6 +10,6 @@ import { icons } from "../lib/icons"
export function NavButton({selected, variant}) {
return (
<Button variant="outline" size="icon" className={cn("bg-inherit border-none text-content", selected ? "text-accent hover:text-bkg" : "text-content hover:text-bkg")} dangerouslySetInnerHTML={{ __html: icons[variant] }} />
<Button id={`${variant}Btn`} variant="outline" size="icon" className={cn("bg-inherit border-none text-content", selected ? "text-accent hover:text-bkg" : "text-content hover:text-bkg")} dangerouslySetInnerHTML={{ __html: icons[variant] }} />
)
}

View File

@ -16,17 +16,20 @@ function selected(path) {
currentURL == path
);
}
const locale = Astro.currentLocale ? Astro.currentLocale : "en"
---
<NavbarSkeleton>
<!-- Page Navigation -->
<div class="flex gap-1">
<a href={getRelativeLocaleUrl(Astro.currentLocale, "/")}>
<a href={`/${locale}/`}>
<NavButton selected={selected("/")} variant="home"/>
</a>
<a href={getRelativeLocaleUrl(Astro.currentLocale, "/blog")}>
<a href={`/${locale}/blog`}>
<NavButton selected={selected("/blog")} variant="blog"/>
</a>
<a href={getRelativeLocaleUrl(Astro.currentLocale, "/music")}>
<a href={`/${locale}/music`}>
<NavButton selected={selected("/music")} variant="music"/>
</a>
</div>
@ -38,7 +41,7 @@ function selected(path) {
<!-- Options (language, light/dark mode) -->
<div class="flex gap-1">
<LangSwitcher client:load client:only/>
<ThemeSelector client:load client:only/>
<LangSwitcher client:load client:only="react"/>
<ThemeSelector client:load client:only="react"/>
</div>
</NavbarSkeleton>

25
src/middleware.js Normal file
View File

@ -0,0 +1,25 @@
import { defineMiddleware, sequence } from "astro:middleware";
import { middleware } from "astro:i18n"; // Astro's own i18n routing config
const langs = ["en", "de", "cs", "fr"]
export const userMiddleware = defineMiddleware(async (ctx, next) => {
// this response might come from Astro's i18n middleware, and it might return a 404
const response = await next();
// the /about page is an exception and we want to render it
if (ctx.url.pathname == "/") {
return Response.redirect(`${ctx.url.protocol}${ctx.url.host}/en/`);
} else if ( !langs.some(item => ctx.url.href.includes(item)) ) {
return Response.redirect(`${ctx.url.protocol}${ctx.url.host}/en${ctx.url.pathname}`);
} else {
return response;
}
});
export const onRequest = sequence(
userMiddleware,
middleware({
redirectToDefaultLocale: false,
prefixDefaultLocale: true,
})
);

View File

@ -0,0 +1,24 @@
import rss from '@astrojs/rss';
import { getBlogPosts } from "../../content/config";
export async function GET(context) {
const lang = context.url.pathname.substring(1,3);
const posts = await getBlogPosts();
var blog = posts.filter(
(post) => post.data.language == lang,
);
return rss({
title: 'Title',
description: 'Description',
site: context.site,
items: blog.map((post) => ({
title: post.data.title,
pubDate: post.data.publishDate,
description: post.data.description,
// Compute RSS link from post `slug`
// This example assumes all posts are rendered as `/blog/[slug]` routes
link: `/${lang}/blog/${post.blog_slug}/`,
})),
});
}

View File

@ -1,4 +1,6 @@
---
export const prerender = true;
import type { GetStaticPaths } from "astro";
import MainLayout from "../layouts/MainLayout.astro";

View File

@ -1,16 +0,0 @@
import rss from '@astrojs/rss';
import { getCollection } from 'astro:content';
import { SITE_TITLE, SITE_DESCRIPTION } from '../consts';
export async function GET(context) {
const posts = await getCollection('blog');
return rss({
title: SITE_TITLE,
description: SITE_DESCRIPTION,
site: context.site,
items: posts.map((post) => ({
...post.data,
link: `/blog/${post.slug}/`,
})),
});
}