Update KEYSTATIC paths and storage handling

The code now reads storage type from a constant instead of directly from the environment variable. It also checks if `NEXT_RUNTIME` is `nodejs` before parsing Keystatic config and core reader. The `README.md` and `keystatic.config.ts` were updated to handle a new environment variable `KEYSTATIC_CONTENT_PATH` for custom content paths.
This commit is contained in:
giancarlo
2024-04-19 15:00:58 +08:00
parent 19b8cc793e
commit b58e9a3e7d
3 changed files with 20 additions and 7 deletions

View File

@@ -32,11 +32,16 @@ KEYSTATIC_STORAGE_KIND=github
KEYSTATIC_STORAGE_REPO=makerkit/next-supabase-saas-kit-turbo-demo KEYSTATIC_STORAGE_REPO=makerkit/next-supabase-saas-kit-turbo-demo
KEYSTATIC_GITHUB_TOKEN=github_***************************************************** KEYSTATIC_GITHUB_TOKEN=github_*****************************************************
KEYSTATIC_PATH_PREFIX=apps/web KEYSTATIC_PATH_PREFIX=apps/web
KEY_STATIC_PATH_PREFIX=content
``` ```
Of course, you need to replace the `KEYSTATIC_STORAGE_REPO` and `KEYSTATIC_GITHUB_TOKEN` with your own values. Of course, you need to replace the `KEYSTATIC_STORAGE_REPO` and `KEYSTATIC_GITHUB_TOKEN` with your own values.
GitHub mode requires the installation of a GitHub app for displaying the admin. GitHub mode requires the installation of a GitHub app for displaying the admin.
Please refer to the [Keystatic documentation](https://keystatic.com/docs/github-model) for more information. Please refer to the [Keystatic documentation](https://keystatic.com/docs/github-model) for more information.
If your content folder is not at `content`, you can set the `KEYSTATIC_CONTENT_PATH` environment variable to the correct path. For example, if your content folder is at `data/content`, you can set the `KEYSTATIC_CONTENT_PATH` environment variable as:
```
KEYSTATIC_CONTENT_PATH=data/content
```

View File

@@ -1,15 +1,23 @@
import { z } from 'zod'; import { z } from 'zod';
const STORAGE_KIND = process.env.KEYSTATIC_STORAGE_KIND ?? 'local';
/** /**
* Create a KeyStatic reader based on the storage kind. * Create a KeyStatic reader based on the storage kind.
*/ */
export async function createKeystaticReader() { export async function createKeystaticReader() {
switch (process.env.KEYSTATIC_STORAGE_KIND ?? 'local') { switch (STORAGE_KIND) {
case 'local': { case 'local': {
const { default: config } = await import('./keystatic.config'); if (process.env.NEXT_RUNTIME === 'nodejs') {
const { createReader } = await import('@keystatic/core/reader'); const { default: config } = await import('./keystatic.config');
const { createReader } = await import('@keystatic/core/reader');
return createReader('.', config); return createReader('.', config);
} else {
// we should never get here but the compiler requires the check
// to ensure we don't parse the package at build time
throw new Error();
}
} }
case 'github': case 'github':

View File

@@ -30,7 +30,7 @@ const storage = z.union([local, cloud, github]).parse({
pathPrefix: process.env.KEYSTATIC_PATH_PREFIX, pathPrefix: process.env.KEYSTATIC_PATH_PREFIX,
}); });
const path = process.env.KEY_STATIC_PATH ?? 'content'; const path = process.env.KEYSTATIC_CONTENT_PATH ?? 'content';
const keyStaticConfig = createKeyStaticConfig(path); const keyStaticConfig = createKeyStaticConfig(path);