Skip to content

Commit

Permalink
Proposal recipe / blog post for guide about disabling Keystatic route…
Browse files Browse the repository at this point in the history
…s in Astro prod (#579)

Co-authored-by: Florian LEFEBVRE <[email protected]>
Co-authored-by: Jed Watson <[email protected]>
  • Loading branch information
3 people authored Sep 4, 2023
1 parent bcb3b8e commit 39252f1
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
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
71 changes: 71 additions & 0 deletions docs/src/content/pages/astro-disable-admin-ui-in-production.mdoc
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.

3 comments on commit 39252f1

@vercel
Copy link

@vercel vercel bot commented on 39252f1 Sep 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

keystatic – ./dev-projects/next-app

keystatic-thinkmill-labs.vercel.app
keystatic.vercel.app
keystatic-git-main-thinkmill-labs.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 39252f1 Sep 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 39252f1 Sep 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

keystar-ui – ./design-system/docs

voussoir.vercel.app
keystar-ui-git-main-thinkmill-labs.vercel.app
keystar-ui.vercel.app
keystar-ui-thinkmill-labs.vercel.app

Please sign in to comment.