Update default path and string manipulation in keystatic.config.ts

A default path for createKeyStaticConfig function is added and conditional code block entered to append '/' to the path, if it doesn't end with it. This prevents potential path issues in future codes. Also, the default value of NEXT_PUBLIC_KEYSTATIC_CONTENT_PATH is changed to an empty string from 'content'.
This commit is contained in:
giancarlo
2024-05-07 19:51:05 +07:00
parent ce993ad120
commit 951acd38a5

View File

@@ -40,7 +40,7 @@ const storage = z.union([local, cloud, github]).parse({
});
const keyStaticConfig = createKeyStaticConfig(
process.env.NEXT_PUBLIC_KEYSTATIC_CONTENT_PATH ?? 'content',
process.env.NEXT_PUBLIC_KEYSTATIC_CONTENT_PATH ?? '',
);
export default keyStaticConfig;
@@ -77,14 +77,18 @@ export type PostEntryProps = Entry<
(typeof keyStaticConfig)['collections']['posts']
>;
function createKeyStaticConfig(path: string) {
function createKeyStaticConfig(path = '') {
if (path && !path.endsWith('/')) {
path += '/';
}
return config({
storage,
collections: {
posts: collection({
label: 'Posts',
slugField: 'title',
path: `${path}/posts/*`,
path: `${path}posts/*`,
format: { contentField: 'content' },
schema: {
title: fields.slug({ name: { label: 'Title' } }),
@@ -109,7 +113,7 @@ function createKeyStaticConfig(path: string) {
documentation: collection({
label: 'Documentation',
slugField: 'title',
path: `${path}/documentation/**`,
path: `${path}documentation/**`,
format: { contentField: 'content' },
schema: {
title: fields.slug({ name: { label: 'Title' } }),