refactor: update LocaleLink usage and routing configuration

- Remove type assertions for href in various components
- Update blog card and blog post page to use `query` instead of `params`
- Simplify routing configuration by commenting out `pathnames` in `routing.ts`
- Remove commented TODOs related to href type handling
- Improve type safety and routing consistency across components
This commit is contained in:
javayhu 2025-03-08 12:17:18 +08:00
parent 8c3f64d0aa
commit 7875c19a3c
8 changed files with 21 additions and 30 deletions

View File

@ -163,10 +163,7 @@ export default async function BlogPostPage(props: NextPageProps) {
category && (
<li key={category.slug}>
<LocaleLink
href={{
pathname: "/blog/category/[slug]",
params: { slug: category.slug }
}}
href={`/blog/category/${category.slug}`}
className="text-sm link-underline"
>
{category.name}

View File

@ -11,9 +11,6 @@ export const metadata = constructMetadata({
canonicalUrl: `${siteConfig.url}${Routes.Login}`,
});
/**
* TODO: href={Routes.TermsOfService as any}
*/
const LoginPage = () => {
const t = useTranslations("AuthPage.login");
@ -23,14 +20,14 @@ const LoginPage = () => {
<div className="text-balance text-center text-xs text-muted-foreground">
{t("byClickingContinue")}
<LocaleLink
href={Routes.TermsOfService as any}
href={Routes.TermsOfService}
className="underline underline-offset-4 hover:text-primary"
>
{t("termsOfService")}
</LocaleLink>{" "}
{t("and")}{" "}
<LocaleLink
href={Routes.PrivacyPolicy as any}
href={Routes.PrivacyPolicy}
className="underline underline-offset-4 hover:text-primary"
>
{t("privacyPolicy")}

View File

@ -9,13 +9,10 @@ interface BottomButtonProps {
label: string;
}
/**
* TODO: solve this error: href={href as any}
*/
export const BottomButton = ({ href, label }: BottomButtonProps) => {
return (
<LocaleLink
href={href as any}
href={href}
className={cn(
buttonVariants({ variant: "link", size: "sm" }),
"font-normal w-full text-muted-foreground hover:underline underline-offset-4 hover:text-primary"

View File

@ -17,10 +17,7 @@ export default function BlogCard({ post }: BlogCardProps) {
return (
<LocaleLink
href={{
pathname: "/blog/[...slug]",
params: { slug: slugParts }
}}
href={`/blog/${slugParts.join('/')}`}
className="block h-full"
>
<div className="group flex flex-col border border-gray-200 dark:border-gray-800 rounded-lg overflow-hidden h-full">

View File

@ -64,7 +64,7 @@ export function NavbarMobile({
{...other}
>
{/* navbar left shows logo */}
<LocaleLink href={Routes.Root as any} className="flex items-center gap-2">
<LocaleLink href={Routes.Root} className="flex items-center gap-2">
<Logo />
<span className="text-xl font-semibold">{siteConfig.name}</span>
</LocaleLink>
@ -114,7 +114,7 @@ function MainMobileMenu({ onLinkClicked }: MainMobileMenuProps) {
{/* action buttons */}
<div className="flex w-full flex-col gap-2">
<LocaleLink
href={Routes.Login as any}
href={Routes.Login}
onClick={onLinkClicked}
className={cn(
buttonVariants({
@ -127,7 +127,7 @@ function MainMobileMenu({ onLinkClicked }: MainMobileMenuProps) {
Log in
</LocaleLink>
<LocaleLink
href={Routes.Register as any}
href={Routes.Register}
className={cn(
buttonVariants({
variant: 'default',

View File

@ -174,7 +174,7 @@ export function Navbar({ scroll, config }: NavBarProps) {
size="sm"
asChild
>
<LocaleLink href={Routes.Register as any}>
<LocaleLink href={Routes.Register}>
Sign up
</LocaleLink>
</Button>

View File

@ -33,15 +33,17 @@ export const routing = defineRouting({
localePrefix: "as-needed",
// The pathnames for each locale
// https://next-intl.dev/docs/routing#pathnames
// whenever use pathname in LocaleLink, you need to add it here
pathnames: {
// used in sietmap.ts
"/": "/",
// used in blog pages
"/blog/[...slug]": "/blog/[...slug]",
"/blog/category/[slug]": "/blog/category/[slug]",
},
//
// https://next-intl.dev/docs/routing/navigation#link
// if we set pathnames, we need to use pathname in LocaleLink
// pathnames: {
// // used in sietmap.ts
// "/": "/",
// // used in blog pages
// "/blog/[...slug]": "/blog/[...slug]",
// "/blog/category/[slug]": "/blog/category/[slug]",
// },
});
export type Pathnames = keyof typeof routing.pathnames;
// export type Pathnames = keyof typeof routing.pathnames;
export type Locale = (typeof routing.locales)[number];

View File

@ -10,6 +10,7 @@ export default function sitemap(): MetadataRoute.Sitemap {
type Href = Parameters<typeof getLocalePathname>[0]['href'];
/**
* https://next-intl.dev/docs/environments/actions-metadata-route-handlers#sitemap
* https://github.com/amannn/next-intl/blob/main/examples/example-app-router/src/app/sitemap.ts
*/
function getEntries(href: Href) {