fix: Switching language error on tabbed pages, add 404 page

This commit is contained in:
radishzzz 2025-03-15 13:55:06 +00:00
parent a61b299b51
commit 2a9500089c
4 changed files with 35 additions and 12 deletions

View file

@ -19,7 +19,7 @@ const marginBottom = {
--- ---
<header class="mb-10.625 lg:fixed"> <header class="mb-10.625 lg:fixed">
<h1 class={`${marginBottom} text-8 c-primary font-bold font-title lg:text-9 w-75%`}> <h1 class={`${marginBottom} text-8 c-primary font-bold font-title lg:text-9 w-75% lg:w-full`}>
<!-- Fix text cropping issues during view transition on ios by adding a div tag --> <!-- Fix text cropping issues during view transition on ios by adding a div tag -->
<div <div
class="box-content inline-block pr-1.25" class="box-content inline-block pr-1.25"

View file

@ -1,6 +1,6 @@
--- ---
import { getNextLangUrl, getPostNextLangUrl } from '@/i18n/path' import { getNextLangUrl, getPostNextLangUrl } from '@/i18n/path'
import { isPostPage } from '@/utils/page' import { isPostPage, isTagPage } from '@/utils/page'
interface Props { interface Props {
supportedLangs: string[] supportedLangs: string[]
@ -9,10 +9,15 @@ interface Props {
const { supportedLangs } = Astro.props const { supportedLangs } = Astro.props
const currentPath = Astro.url.pathname const currentPath = Astro.url.pathname
const isPost = isPostPage(currentPath) const isPost = isPostPage(currentPath)
const isTag = isTagPage(currentPath)
const nextUrl = isPost // 检查是否应使用受限的语言列表
? getPostNextLangUrl(currentPath, supportedLangs) const useFilteredLangs = isPost || (isTag && supportedLangs.length > 0)
: getNextLangUrl(currentPath)
// 根据页面类型选择合适的语言切换方式
const nextUrl = useFilteredLangs
? getPostNextLangUrl(currentPath, supportedLangs) // 只在有内容的语言间切换
: getNextLangUrl(currentPath) // 在所有语言间切换
--- ---
<a <a

8
src/pages/404.astro Normal file
View file

@ -0,0 +1,8 @@
---
import Layout from '@/layouts/Layout.astro'
---
<Layout>
<div class="uno-decorative-line"></div>
<h1 class="mt-10 text-8 font-title">404</h1>
</Layout>

View file

@ -5,8 +5,6 @@ import Layout from '@/layouts/Layout.astro'
import { getAllTags, getPostsByTag } from '@/utils/content' import { getAllTags, getPostsByTag } from '@/utils/content'
export async function getStaticPaths() { export async function getStaticPaths() {
const tags = await getAllTags()
// 定义路径数组的类型 // 定义路径数组的类型
type PathItem = { type PathItem = {
params: { tags_tag: string } params: { tags_tag: string }
@ -16,7 +14,8 @@ export async function getStaticPaths() {
const paths: PathItem[] = [] const paths: PathItem[] = []
// 默认语言的标签页面 (没有语言前缀) // 默认语言的标签页面 (没有语言前缀)
tags.forEach((tag: string) => { const defaultTags = await getAllTags(defaultLocale)
defaultTags.forEach((tag: string) => {
paths.push({ paths.push({
params: { tags_tag: `tags/${tag}` }, params: { tags_tag: `tags/${tag}` },
props: { tag, lang: defaultLocale }, props: { tag, lang: defaultLocale },
@ -24,16 +23,17 @@ export async function getStaticPaths() {
}) })
// 更多语言的标签页面 (有语言前缀) // 更多语言的标签页面 (有语言前缀)
allLocales.forEach((lang: string) => { for (const lang of allLocales) {
if (lang !== defaultLocale) { if (lang !== defaultLocale) {
tags.forEach((tag: string) => { const langTags = await getAllTags(lang)
langTags.forEach((tag: string) => {
paths.push({ paths.push({
params: { tags_tag: `${lang}/tags/${tag}` }, params: { tags_tag: `${lang}/tags/${tag}` },
props: { tag, lang }, props: { tag, lang },
}) })
}) })
} }
}) }
return paths return paths
} }
@ -42,6 +42,16 @@ const { tag, lang } = Astro.props
const posts = await getPostsByTag(tag, lang) const posts = await getPostsByTag(tag, lang)
const allTags = await getAllTags(lang) const allTags = await getAllTags(lang)
// 获取当前标签在每种语言下是否有文章
const tagSupportedLangs = await Promise.all(
allLocales.map(async (locale) => {
const postsInLang = await getPostsByTag(tag, locale)
return postsInLang.length > 0 ? locale : null
}),
)
// 过滤出支持当前标签的语言列表
const supportedLangs = tagSupportedLangs.filter(Boolean) as string[]
// 构建标签链接 // 构建标签链接
function getTagUrl(tagName: string): string { function getTagUrl(tagName: string): string {
return lang === defaultLocale return lang === defaultLocale
@ -50,7 +60,7 @@ function getTagUrl(tagName: string): string {
} }
--- ---
<Layout> <Layout supportedLangs={supportedLangs}>
<div class="uno-decorative-line"></div> <div class="uno-decorative-line"></div>
<div class="uno-tags-wrapper"> <div class="uno-tags-wrapper">
{allTags.map(tagName => ( {allTags.map(tagName => (