Skip to content

Commit

Permalink
Add config option to disable 404 page (#1389)
Browse files Browse the repository at this point in the history
Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
  • Loading branch information
connor-baer and delucis authored Jan 26, 2024
1 parent e3e38ae commit 21b3620
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/odd-rocks-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/starlight": minor
---

Adds new `disable404Route` config option to disable injection of Astro’s default 404 route
19 changes: 19 additions & 0 deletions docs/src/content/docs/guides/customization.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,25 @@ hero:
---
```

### Disabling the default 404 page

If your project requires an entirely customized 404 layout, you can create a `src/pages/404.astro` route and set the [`disable404Route`](/reference/configuration/#disable404route) config option to disable Starlight’s default route:

```js {9}
// astro.config.mjs
import { defineConfig } from 'astro/config';
import starlight from '@astrojs/starlight';

export default defineConfig({
integrations: [
starlight({
title: 'Docs With Custom 404',
disable404Route: true,
}),
],
});
```

## Custom fonts

By default, Starlight uses sans-serif fonts available on a user’s local device for all text.
Expand Down
7 changes: 7 additions & 0 deletions docs/src/content/docs/reference/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,13 @@ Sets the delimiter between page title and site title in the page’s `<title>` t
By default, every page has a `<title>` of `Page Title | Site Title`.
For example, this page is titled “Configuration Reference” and this site is titled “Starlight”, so the `<title>` for this page is “Configuration Reference | Starlight”.

### `disable404Route`

**type:** `boolean`
**default:** `'false'`

Disables injecting Starlight's default [404 page](https://docs.astro.build/en/core-concepts/astro-pages/#custom-404-error-page). To use a custom `src/pages/404.astro` route in your project, set this option to `false`.

### `components`

**type:** `Record<string, string>`
Expand Down
10 changes: 6 additions & 4 deletions packages/starlight/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ export default function StarlightIntegration({

const useTranslations = createTranslationSystemFromFs(starlightConfig, config);

injectRoute({
pattern: '404',
entrypoint: '@astrojs/starlight/404.astro',
});
if (!userConfig.disable404Route) {
injectRoute({
pattern: '404',
entrypoint: '@astrojs/starlight/404.astro',
});
}
injectRoute({
pattern: '[...slug]',
entrypoint: '@astrojs/starlight/index.astro',
Expand Down
3 changes: 3 additions & 0 deletions packages/starlight/utils/user-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ const UserConfigSchema = z.object({
.string()
.default('|')
.describe('Will be used as title delimiter in the generated `<title>` tag.'),

/** Disable Starlight's default 404 page. */
disable404Route: z.boolean().default(false).describe("Disable Starlight's default 404 page."),
});

export const StarlightConfigSchema = UserConfigSchema.strict().transform(
Expand Down

0 comments on commit 21b3620

Please sign in to comment.