Astro: Disable Admin UI Routes in Production
This is a community contribution from Florian Lefebvre — thank you Florian!
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 when using the Astro framework.
Conditionally render the Astro plugin
If you've followed the Astro integration guide, you should have a keystatic
integration in your astro.config.mjs
file. Now, we'll introduce a condition in the configuration file which will enable us to use an environment variable to determine when Keystatic should be mounted.
// astro.config.mjs
import { defineConfig } from 'astro/config'
import react from '@astrojs/react'
import markdoc from '@astrojs/markdoc'
import keystatic from '@keystatic/astro'
// https://astro.build/config
export default defineConfig({
- integrations: [react(), markdoc(), keystatic()],
+ integrations: [react(), markdoc(), ...(process.env.SKIP_KEYSTATIC ? [] : [keystatic()])],
output: 'hybrid',
})
The condition above will not mount Keystatic if SKIP_KEYSTATIC=true
is present in your environment variables. If SKIP_KEYSTATIC
is not defined or set to false, Keystatic will be mounted.
Adding environment variables
We won't cover every host here but you can check out Astro's deploy guides for a more exhaustive list.
Vercel
If you're deploying your Astro project with Vercel, you can add environment variables in your project's settings.
- Navigate to Project -> Settings -> Environment variables.
- In the 'Key' field, type
SKIP_KEYSTATIC
and in the 'Value' field, typetrue
. - Click 'Save'
Check out Vercel's official docs for further details.
Netlify
If you're deploying your Astro project with Netlify, you can add environment variables in your site's configuration page.
- Navigate to Site -> Site configuration -> Environment variables.
- Add a new variable by clicking 'Add a variable' and 'Add a single variable'.
- Set the 'Key' as
SKIP_KEYSTATIC
and the 'Value' astrue
. - Click 'Save variable'
Refer to Netlify's official docs for additional guidance.
Re-deploy
Re-deploy your project for your changes to take effect. Keystatic routes will no longer deploy to production (routes won't be indexed or added to sitemaps) and you can still run Keystatic on your local machine.