Skip to content

Commit 4802cfa

Browse files
committed
fix(i18n): guard missing route keys
1 parent d0fc2c0 commit 4802cfa

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

src/layouts/BaseLayout.astro

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type Props = {
1010
title?: string;
1111
description?: string;
1212
lang?: "pl" | "en";
13-
routeKey: RouteKey;
13+
routeKey?: RouteKey;
1414
routeParams?: RouteParams;
1515
};
1616
@@ -23,10 +23,10 @@ const {
2323
} = Astro.props as Props;
2424
2525
const baseUrl = "https://rocketdeploy.dev";
26-
const { pl: plPath, en: enPath } = getRoutePaths(routeKey, routeParams);
27-
const plUrl = new URL(plPath, baseUrl).toString();
28-
const enUrl = new URL(enPath, baseUrl).toString();
29-
const canonicalUrl = lang === "pl" ? plUrl : enUrl;
26+
const routePaths = routeKey ? getRoutePaths(routeKey, routeParams) : null;
27+
const plUrl = routePaths ? new URL(routePaths.pl, baseUrl).toString() : null;
28+
const enUrl = routePaths ? new URL(routePaths.en, baseUrl).toString() : null;
29+
const canonicalUrl = routePaths ? (lang === "pl" ? plUrl : enUrl) : null;
3030
---
3131

3232
<!doctype html>
@@ -42,16 +42,16 @@ const canonicalUrl = lang === "pl" ? plUrl : enUrl;
4242
{description && <meta property="og:description" content={description} />}
4343
{description && <meta name="twitter:description" content={description} />}
4444

45-
<link rel="canonical" href={canonicalUrl} />
46-
<link rel="alternate" hreflang="pl" href={plUrl} />
47-
<link rel="alternate" hreflang="en" href={enUrl} />
48-
<link rel="alternate" hreflang="x-default" href={enUrl} />
45+
{canonicalUrl && <link rel="canonical" href={canonicalUrl} />}
46+
{plUrl && <link rel="alternate" hreflang="pl" href={plUrl} />}
47+
{enUrl && <link rel="alternate" hreflang="en" href={enUrl} />}
48+
{enUrl && <link rel="alternate" hreflang="x-default" href={enUrl} />}
4949
</head>
5050

5151
<body>
5252
<div class="header">
5353
<div class="container">
54-
<Header lang={lang} routeKey={routeKey} routeParams={routeParams} />
54+
<Header lang={lang} routeKey={routeKey ?? "home"} routeParams={routeParams} />
5555
</div>
5656
</div>
5757

src/layouts/CaseStudyLayout.astro

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
---
22
import BaseLayout from "./BaseLayout.astro";
3-
const { title, lang = "en", tags = [] } = Astro.props;
3+
import type { RouteKey, RouteParams } from "../i18n/routes";
4+
5+
type Props = {
6+
title: string;
7+
lang?: "pl" | "en";
8+
tags?: string[];
9+
routeKey?: RouteKey;
10+
routeParams?: RouteParams;
11+
};
12+
13+
const {
14+
title,
15+
lang = "en",
16+
tags = [],
17+
routeKey,
18+
routeParams,
19+
} = Astro.props as Props;
420
---
521

6-
<BaseLayout lang={lang} title={title}>
22+
<BaseLayout lang={lang} title={title} routeKey={routeKey} routeParams={routeParams}>
723
<div style="max-width: 78ch;">
824
<div style="font-family: var(--mono); color: rgba(122,162,255,.85); font-size: 12px;">case study</div>
925
<h1 style="margin-top:10px;">{title}</h1>

0 commit comments

Comments
 (0)