-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proposal recipe / blog post for guide about disabling Keystatic route…
…s in Astro prod (#579) Co-authored-by: Florian LEFEBVRE <[email protected]> Co-authored-by: Jed Watson <[email protected]>
- Loading branch information
1 parent
bcb3b8e
commit 39252f1
Showing
3 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
docs/src/content/pages/astro-disable-admin-ui-in-production.mdoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
+ }); | ||
] | ||
}) | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39252f1
There was a problem hiding this comment.
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
39252f1
There was a problem hiding this comment.
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-site – ./docs
keystatic.com
keystatic-site.vercel.app
keystatic-site-git-main-thinkmill-labs.vercel.app
keystatic.thinkmill.com
keystatic.thinkmill.com.au
www.keystatic.com
keystatic-site-thinkmill-labs.vercel.app
39252f1
There was a problem hiding this comment.
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