Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal recipe / blog post for guide about disabling Keystatic routes in Astro prod #579

Merged
merged 11 commits into from
Sep 4, 2023
6 changes: 6 additions & 0 deletions docs/src/content/navigation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ navGroups:
link:
discriminant: page
value: github-mode
- groupName: Recipes
items:
- label: 'Astro: Disable Admin UI Routes in Production'
link:
discriminant: page
value: astro-disable-admin-ui-in-production
- groupName: Reference
items:
- label: Configuration
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
title: 'Astro: Disable Admin UI Routes in Production'
summary: >-
This recipe shows you how to prevent access to (and indexing of) `/keystatic`
routes in production if you're using the Astro framework.
---

{% aside icon="🙏" %}
This is a community contribution from [Florian Lefebvre](https://florian-lefebvre.dev) — thank you Florian!
{% /aside %}

When using the `local` strategy, you may want to disable access to the `/keystatic` routes in production.

Here's how you can prevent access to (and indexing of) those routes if you're using the Astro framework.

## Adding redirects

You can redirect visits to the `/keystatic` route in production with `Astro.redirect`:

```diff
---
// src/pages/keystatic/[...params].astro
import { Keystatic } from '../../../keystatic.page'

export const prerender = false

+ if (import.meta.env.MODE === 'production') {
+ return Astro.redirect('/')
+ }
---

<Keystatic client:only />

```

You will need to do the same for the `api/keystatic` routes:

```jsx
// src/pages/api/keystatic/[...params].ts
import { makeHandler } from '@keystatic/astro/api'
import keystaticConfig from '../../../../keystatic.config'

export const all = makeHandler({
config: keystaticConfig,
})

export const prerender = false

+ if (import.meta.env.MODE === 'production') {
+ return Astro.redirect('/')
+ }
```

## Excluding routes from sitemap

If you're using `@astrojs/sitemap`, you can exclude those routes as well:

```diff
// astro.config.mjs
import { defineConfig } from 'astro/config'
import sitemap from '@astrojs/sitemap';

export default defineConfig({
integrations: [
+ sitemap({
+ filter: (page) => !page.includes("keystatic"),
+ });
]
})
```

2 changes: 1 addition & 1 deletion docs/src/content/pages/installation-astro.mdoc
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,4 @@ const { Content } = await post.render()

Because Keystatic needs to run serverside code and use Node.js APIs, you will need to add an [Astro adapter](https://docs.astro.build/en/guides/server-side-rendering/#adding-an-adapter) to deploy your project.

You will also probably want to [connect Keystatic to GitHub](/docs/github-mode) so you can manage content on the deployed instance of the project.
You will also probably want to [connect Keystatic to GitHub](/docs/github-mode) so you can manage content on the deployed instance of the project.
Loading