diff --git a/docs/src/content/navigation.yaml b/docs/src/content/navigation.yaml index 351d48693..7e1fecc69 100644 --- a/docs/src/content/navigation.yaml +++ b/docs/src/content/navigation.yaml @@ -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 diff --git a/docs/src/content/pages/astro-disable-admin-ui-in-production.mdoc b/docs/src/content/pages/astro-disable-admin-ui-in-production.mdoc new file mode 100644 index 000000000..728ee6612 --- /dev/null +++ b/docs/src/content/pages/astro-disable-admin-ui-in-production.mdoc @@ -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('/') ++ } +--- + + + +``` + +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"), ++ }); + ] +}) +``` + diff --git a/docs/src/content/pages/installation-astro.mdoc b/docs/src/content/pages/installation-astro.mdoc index 137b86ca7..69ef9c542 100644 --- a/docs/src/content/pages/installation-astro.mdoc +++ b/docs/src/content/pages/installation-astro.mdoc @@ -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. \ No newline at end of file