Skip to content

Commit b45719b

Browse files
Add titleDelimiter option to the api and a new design for <title> (#447)
Co-authored-by: Chris Swithinbank <[email protected]>
1 parent 08b5a75 commit b45719b

File tree

5 files changed

+44
-4
lines changed

5 files changed

+44
-4
lines changed

.changeset/modern-timers-switch.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
'@astrojs/starlight': minor
3+
---
4+
5+
Add `titleDelimiter` configuration option and include site title in page `<title>` tags
6+
7+
⚠️ **BREAKING CHANGE** — Previously, every page’s `<title>` only included its individual frontmatter title.
8+
Now, `<title>` tags include the page title, a delimiter character (`|` by default), and the site title.
9+
For example, in the Startlight docs, `<title>Configuration Reference</title>` is now `<title>Configuration Reference | Starlight</title>`.
10+
11+
If you have a page where you need to override this new behaviour, set a custom title using the `head` frontmatter property:
12+
13+
14+
```md
15+
---
16+
title: My Page
17+
head:
18+
- tag: title
19+
content: Custom Title
20+
---
21+
```

docs/src/content/docs/index.mdx

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
---
22
title: Starlight 🌟 Build documentation sites with Astro
3+
head:
4+
- tag: title
5+
content: Starlight 🌟 Build documentation sites with Astro
36
description: Starlight helps you build beautiful, high-performance documentation websites with Astro.
47
template: splash
58
banner:

docs/src/content/docs/reference/configuration.md

+10
Original file line numberDiff line numberDiff line change
@@ -425,3 +425,13 @@ starlight({
425425
],
426426
});
427427
```
428+
429+
### `titleDelimiter`
430+
431+
**type:** `string`
432+
**default:** `'|'`
433+
434+
Sets the delimiter between page title and site title in the page’s `<title>` tag, which is displayed on browser tabs.
435+
436+
By default, every page has a `<title>` of `Page Title | Site Title`.
437+
For example, this page is titled “Configuration Reference” and this site is titled “Starlight”, so the `<title>` for this page is “Configuration Reference | Starlight”.

packages/starlight/components/HeadSEO.astro

+3-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ interface Props {
1515
const { data, lang } = Astro.props;
1616
1717
const canonical = Astro.site ? new URL(Astro.url.pathname, Astro.site) : undefined;
18-
const title = data.title || config.title;
1918
const description = data.description || config.description;
2019
2120
const headDefaults: z.input<ReturnType<typeof HeadConfigSchema>> = [
@@ -24,7 +23,7 @@ const headDefaults: z.input<ReturnType<typeof HeadConfigSchema>> = [
2423
tag: 'meta',
2524
attrs: { name: 'viewport', content: 'width=device-width, initial-scale=1' },
2625
},
27-
{ tag: 'title', content: title },
26+
{ tag: 'title', content: `${data.title} ${config.titleDelimiter} ${config.title}` },
2827
{ tag: 'link', attrs: { rel: 'canonical', href: canonical?.href } },
2928
{ tag: 'meta', attrs: { name: 'generator', content: Astro.generator } },
3029
{
@@ -41,7 +40,7 @@ const headDefaults: z.input<ReturnType<typeof HeadConfigSchema>> = [
4140
},
4241
},
4342
// OpenGraph Tags
44-
{ tag: 'meta', attrs: { property: 'og:title', content: title } },
43+
{ tag: 'meta', attrs: { property: 'og:title', content: data.title } },
4544
{ tag: 'meta', attrs: { property: 'og:type', content: 'article' } },
4645
{ tag: 'meta', attrs: { property: 'og:url', content: canonical?.href } },
4746
{ tag: 'meta', attrs: { property: 'og:locale', content: lang } },
@@ -52,7 +51,7 @@ const headDefaults: z.input<ReturnType<typeof HeadConfigSchema>> = [
5251
tag: 'meta',
5352
attrs: { name: 'twitter:card', content: 'summary_large_image' },
5453
},
55-
{ tag: 'meta', attrs: { name: 'twitter:title', content: title } },
54+
{ tag: 'meta', attrs: { name: 'twitter:title', content: data.title } },
5655
{ tag: 'meta', attrs: { name: 'twitter:description', content: description } },
5756
];
5857

packages/starlight/utils/user-config.ts

+7
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,13 @@ const UserConfigSchema = z.object({
279279

280280
/** The default favicon for your site which should be a path to an image in the `public/` directory. */
281281
favicon: FaviconSchema(),
282+
283+
/** Will be used as title delimiter in the generated `<title>` tag. */
284+
titleDelimiter: z
285+
.string()
286+
.default('|')
287+
.describe("Will be used as title delimiter in the generated `<title>` tag."),
288+
282289
});
283290

284291
export const StarlightConfigSchema = UserConfigSchema.strict().transform(

0 commit comments

Comments
 (0)