Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 168 additions & 0 deletions packages/astro/src/config/content.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import type { Loader } from 'esbuild';
import type { ZodLiteral, ZodNumber, ZodObject, ZodString, ZodType, ZodUnion } from 'zod';
import { CONTENT_LAYER_TYPE, LIVE_CONTENT_TYPE } from '../content/consts.js';
import type { LiveLoader } from '../content/loaders/types.js';
import { AstroError, AstroErrorData, AstroUserError } from '../core/errors/index.js';

export function getImporterFilename() {
// Find the first line in the stack trace that doesn't include 'defineCollection' or 'getImporterFilename'
const stackLine = new Error().stack
?.split('\n')
.find(
(line) =>
!line.includes('defineCollection') &&
!line.includes('getImporterFilename') &&
line !== 'Error',
);
if (!stackLine) {
return undefined;
}
// Extract the relative path from the stack line
const match = /\/((?:src|chunks)\/.*?):\d+:\d+/.exec(stackLine);

return match?.[1] ?? undefined;
}

// This needs to be in sync with ImageMetadata
Copy link
Contributor Author

@ascorbic ascorbic May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything below this line has been moved from runtime.ts and content.d.ts. The only changes are those needed to move types from a .d.ts to a .ts file

export type ImageFunction = () => ZodObject<{
src: ZodString;
width: ZodNumber;
height: ZodNumber;
format: ZodUnion<
[
ZodLiteral<'png'>,
ZodLiteral<'jpg'>,
ZodLiteral<'jpeg'>,
ZodLiteral<'tiff'>,
ZodLiteral<'webp'>,
ZodLiteral<'gif'>,
ZodLiteral<'svg'>,
ZodLiteral<'avif'>,
]
>;
}>;

export interface DataEntry {
id: string;
data: Record<string, unknown>;
filePath?: string;
body?: string;
}

export interface DataStore {
get: (key: string) => DataEntry;
entries: () => Array<[id: string, DataEntry]>;
set: (key: string, data: Record<string, unknown>, body?: string, filePath?: string) => void;
values: () => Array<DataEntry>;
keys: () => Array<string>;
delete: (key: string) => void;
clear: () => void;
has: (key: string) => boolean;
}

export interface MetaStore {
get: (key: string) => string | undefined;
set: (key: string, value: string) => void;
delete: (key: string) => void;
has: (key: string) => boolean;
}

export type BaseSchema = ZodType;

export type SchemaContext = { image: ImageFunction };

type ContentLayerConfig<S extends BaseSchema, TData extends { id: string } = { id: string }> = {
type?: 'content_layer';
schema?: S | ((context: SchemaContext) => S);
loader:
| Loader
| (() =>
| Array<TData>
| Promise<Array<TData>>
| Record<string, Omit<TData, 'id'> & { id?: string }>
| Promise<Record<string, Omit<TData, 'id'> & { id?: string }>>);
};

type DataCollectionConfig<S extends BaseSchema> = {
type: 'data';
schema?: S | ((context: SchemaContext) => S);
};

type ContentCollectionConfig<S extends BaseSchema> = {
type?: 'content';
schema?: S | ((context: SchemaContext) => S);
loader?: never;
};

type LiveDataCollectionConfig<S extends BaseSchema, L extends LiveLoader> = {
type: 'live';
schema?: S;
loader: L;
};

export type CollectionConfig<
S extends BaseSchema,
TLiveLoader = never,
> = TLiveLoader extends LiveLoader
? LiveDataCollectionConfig<S, TLiveLoader>
: ContentCollectionConfig<S> | DataCollectionConfig<S> | ContentLayerConfig<S>;

export function defineCollection<S extends BaseSchema, TLiveLoader = undefined>(
config: CollectionConfig<S, TLiveLoader>,
): CollectionConfig<S, TLiveLoader> {
const importerFilename = getImporterFilename();
const isInLiveConfig = importerFilename?.includes('live.config');

if (config.type === LIVE_CONTENT_TYPE) {
if (!isInLiveConfig) {
throw new AstroError({
...AstroErrorData.LiveContentConfigError,
message: AstroErrorData.LiveContentConfigError.message(
'Collections with type `live` must be defined in a `src/live.config.ts` file.',
importerFilename ?? 'your content config file',
),
});
}
if (!config.loader) {
throw new AstroError({
...AstroErrorData.LiveContentConfigError,
message: AstroErrorData.LiveContentConfigError.message(
'Collections with type `live` must have a `loader` defined.',
importerFilename,
),
});
}
if (config.schema) {
if (typeof config.schema === 'function') {
throw new AstroError({
...AstroErrorData.LiveContentConfigError,
message: AstroErrorData.LiveContentConfigError.message(
'The schema cannot be a function for live collections. Please use a schema object instead.',
importerFilename,
),
});
}
}
return config;
}
if (isInLiveConfig) {
throw new AstroError({
...AstroErrorData.LiveContentConfigError,
message: AstroErrorData.LiveContentConfigError.message(
'Collections in a `live.config.ts` file must have a type of `live`.',
getImporterFilename(),
),
});
}

if ('loader' in config) {
if (config.type && config.type !== CONTENT_LAYER_TYPE) {
throw new AstroUserError(
`Collections that use the Content Layer API must have a \`loader\` defined and no \`type\` set. Check your collection definitions in ${getImporterFilename() ?? 'your content config file'}.`,
);
}
config.type = CONTENT_LAYER_TYPE;
}
if (!config.type) config.type = 'content';
return config;
}
10 changes: 9 additions & 1 deletion packages/astro/src/config/entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

import type { SharpImageServiceConfig } from '../assets/services/sharp.js';
import type { ImageServiceConfig } from '../types/public/index.js';

export {
defineCollection,
type ImageFunction,
type DataEntry,
type DataStore,
type MetaStore,
type BaseSchema,
type SchemaContext,
} from './content.js';
export { defineConfig, getViteConfig } from './index.js';
export { envField } from '../env/config.js';
export { mergeConfig } from '../core/config/merge.js';
Expand Down
6 changes: 3 additions & 3 deletions packages/astro/src/content/loaders/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ export interface LoadCollectionContext<TCollectionFilter = unknown> {
}

export interface LiveLoader<
TData extends Record<string, unknown> = Record<string, unknown>,
TEntryFilter extends Record<string, unknown> | never = never,
TCollectionFilter extends Record<string, unknown> | never = never,
TData extends Record<string, any> = Record<string, unknown>,
TEntryFilter extends Record<string, any> | never = never,
TCollectionFilter extends Record<string, any> | never = never,
> {
/** Unique name of the loader, e.g. the npm package name */
name: string;
Expand Down
85 changes: 15 additions & 70 deletions packages/astro/src/content/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import pLimit from 'p-limit';
import { ZodIssueCode, z } from 'zod';
import type { GetImageResult, ImageMetadata } from '../assets/types.js';
import { imageSrcToImportId } from '../assets/utils/resolveImports.js';
import { AstroError, AstroErrorData, AstroUserError } from '../core/errors/index.js';
import { defineCollection as defineCollectionOrig } from '../config/content.js';
import { AstroError, AstroErrorData } from '../core/errors/index.js';
import { prependForwardSlash } from '../core/path.js';

import {
Expand All @@ -20,7 +21,7 @@ import {
unescapeHTML,
} from '../runtime/server/index.js';
import type { LiveDataEntry } from '../types/public/content.js';
import { CONTENT_LAYER_TYPE, IMAGE_IMPORT_PREFIX, LIVE_CONTENT_TYPE } from './consts.js';
import { IMAGE_IMPORT_PREFIX, type LIVE_CONTENT_TYPE } from './consts.js';
import { type DataEntry, globalDataStore } from './data-store.js';
import type { LiveLoader } from './loaders/types.js';
import type { ContentLookupMap } from './utils.js';
Expand All @@ -34,74 +35,6 @@ type LiveCollectionConfigMap = Record<
{ loader: LiveLoader; type: typeof LIVE_CONTENT_TYPE; schema?: z.ZodType }
>;

export function getImporterFilename() {
// The 4th line in the stack trace should be the importer filename
const stackLine = new Error().stack?.split('\n')?.[3];
if (!stackLine) {
return undefined;
}
// Extract the relative path from the stack line
const match = /\/(src\/.*?):\d+:\d+/.exec(stackLine);
return match?.[1] ?? undefined;
}

export function defineCollection(config: any) {
const isInLiveConfig = getImporterFilename()?.endsWith('/live.config.ts');

if (config.type === LIVE_CONTENT_TYPE) {
if (!isInLiveConfig) {
throw new AstroError({
...AstroErrorData.LiveContentConfigError,
message: AstroErrorData.LiveContentConfigError.message(
'Collections with type `live` must be defined in a `src/live.config.ts` file.',
getImporterFilename() ?? 'your content config file',
),
});
}
if (!config.loader) {
throw new AstroError({
...AstroErrorData.LiveContentConfigError,
message: AstroErrorData.LiveContentConfigError.message(
'Collections with type `live` must have a `loader` defined.',
getImporterFilename(),
),
});
}
if (config.schema) {
if (typeof config.schema === 'function') {
throw new AstroError({
...AstroErrorData.LiveContentConfigError,
message: AstroErrorData.LiveContentConfigError.message(
'The schema cannot be a function for live collections. Please use a schema object instead.',
getImporterFilename(),
),
});
}
}
return config;
}
if (isInLiveConfig) {
throw new AstroError({
...AstroErrorData.LiveContentConfigError,
message: AstroErrorData.LiveContentConfigError.message(
'Collections in a `live.config.ts` file must have a type of `live`.',
getImporterFilename(),
),
});
}

if ('loader' in config) {
if (config.type && config.type !== CONTENT_LAYER_TYPE) {
throw new AstroUserError(
`Collections that use the Content Layer API must have a \`loader\` defined and no \`type\` set. Check your collection definitions in ${getImporterFilename() ?? 'your content config file'}.`,
);
}
config.type = CONTENT_LAYER_TYPE;
}
if (!config.type) config.type = 'content';
return config;
}

export function createCollectionToGlobResultMap({
globResult,
contentDir,
Expand Down Expand Up @@ -883,3 +816,15 @@ type PropagatedAssetsModule = {
function isPropagatedAssetsModule(module: any): module is PropagatedAssetsModule {
return typeof module === 'object' && module != null && '__astroPropagation' in module;
}

export function defineCollection(config: any) {
if (config.type === 'live') {
throw new AstroError({
...AstroErrorData.LiveContentConfigError,
message: AstroErrorData.LiveContentConfigError.message(
'You must import defineCollection from "astro/config" to use live collections.',
),
});
}
return defineCollectionOrig(config);
}
45 changes: 37 additions & 8 deletions packages/astro/test/fixtures/live-loaders/src/live.config.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,45 @@
import { defineCollection, z } from 'astro:content';
import { defineCollection } from 'astro/config';
import { z } from 'astro/zod';
import type { LiveLoader } from 'astro/loaders';

type Entry = {
title: string;
age?: number;
};

interface CollectionFilter {
addToAge?: number;
}

type EntryFilter = {
id: keyof typeof entries;
addToAge?: number;
};


const entries = {
'123': { id: '123', data: { title: 'Page 123' } },
'456': { id: '456', data: { title: 'Page 456' } },
'789': { id: '789', data: { title: 'Page 789' } },
'123': { id: '123', data: { title: 'Page 123', age: 10 } },
'456': { id: '456', data: { title: 'Page 456', age: 20 } },
'789': { id: '789', data: { title: 'Page 789', age: 30 } },
};

const loader: LiveLoader<Entry, { id: keyof typeof entries }> = {
const loader: LiveLoader<Entry, EntryFilter, CollectionFilter> = {
name: 'test-loader',
loadEntry: async (context) => {
if(!entries[context.filter.id]) {
const entry = entries[context.filter.id];
if (!entry) {
return;
}
return {
...entries[context.filter.id],
...entry,
data: {
...entry.data,
age: context.filter?.addToAge
? entry.data.age
? entry.data.age + context.filter.addToAge
: context.filter.addToAge
: entry.data.age,
},
cacheHint: {
tags: [`page:${context.filter.id}`],
maxAge: 60,
Expand All @@ -27,7 +48,15 @@ const loader: LiveLoader<Entry, { id: keyof typeof entries }> = {
},
loadCollection: async (context) => {
return {
entries: Object.values(entries),
entries: context.filter?.addToAge
? Object.values(entries).map((entry) => ({
...entry,
data: {
...entry.data,
age: entry.data.age ? entry.data.age + context.filter!.addToAge! : undefined,
},
}))
: Object.values(entries),
cacheHint: {
tags: ['page'],
maxAge: 60,
Expand Down
16 changes: 16 additions & 0 deletions packages/astro/test/fixtures/live-loaders/src/pages/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { APIRoute } from 'astro';
import { getCollection, getEntry } from 'astro:content';

export const prerender = false;

export const GET: APIRoute = async ({ request }) => {
const addToAge = new URL(request.url).searchParams.get('addToAge');
const filter = addToAge ? { addToAge: parseInt(addToAge) } : undefined;
const collection = await getCollection('liveStuff', filter);
const entryByString = await getEntry('liveStuff', '123');
const entryByObject = await getEntry(
'liveStuff',
{ id: '456', ...filter },
);
return Response.json({ collection, entryByObject, entryByString });
};
Loading
Loading