Skip to content

Commit

Permalink
fix: sync content layer in dev (#11365)
Browse files Browse the repository at this point in the history
* wip

* wip

* wip

* Update demo

* Add meta

* wip

* Add file loader

* Add schema validation

* Remove log

* Changeset

* Format

* Lockfile

* Fix type

* Handle loading for data store JSON

* Use rollup util to import JSON

* Fix types

* Format

* Add tests

* Changes from review

* Sync content layer in dev
  • Loading branch information
ascorbic authored Jun 28, 2024
1 parent 9561a07 commit a66df94
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
7 changes: 5 additions & 2 deletions packages/astro/src/content/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function file(fileName: string): Loader {
name: 'file-loader',
load: async ({ store, logger, settings, parseData }) => {
const contentDir = new URL('./content/', settings.config.srcDir);

logger.debug(`Loading data from ${fileName}`)
const url = new URL(fileName, contentDir);
if (!existsSync(url)) {
logger.error(`File not found: ${fileName}`);
Expand All @@ -40,6 +40,7 @@ export function file(fileName: string): Loader {
if (json.length === 0) {
logger.warn(`No items found in ${fileName}`);
}
logger.debug(`Found ${json.length} item array in ${fileName}`);
for (const rawItem of json) {
const id = (rawItem.id ?? rawItem.slug)?.toString();
if (!id) {
Expand All @@ -50,7 +51,9 @@ export function file(fileName: string): Loader {
store.set(id, item);
}
} else if (typeof json === 'object') {
for (const [id, rawItem] of Object.entries<Record<string, unknown>>(json)) {
const entries = Object.entries<Record<string, unknown>>(json);
logger.debug(`Found object with ${entries.length} entries in ${fileName}`);
for (const [id, rawItem] of entries) {
const item = await parseData({ id, data: rawItem, filePath });
store.set(id, item);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/content/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export async function syncContentLayer({
store,
}: { settings: AstroSettings; logger: Logger; store?: DataStore }) {
const logger = globalLogger.forkIntegrationLogger('content');
logger.info('Syncing content');
if (!store) {
store = await DataStore.fromDisk(new URL(DATA_STORE_FILE, settings.config.cacheDir));
globalDataStore.set(store);
Expand Down Expand Up @@ -108,7 +109,7 @@ export async function syncContentLayer({
collection: name,
store: store.scopedStore(name),
meta: store.metaStore(name),
logger,
logger: globalLogger.forkIntegrationLogger(collection.loader.name ?? 'content'),
settings,
parseData,
});
Expand Down
3 changes: 3 additions & 0 deletions packages/astro/src/core/dev/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { gt, major, minor, patch } from 'semver';
import type * as vite from 'vite';
import type { AstroInlineConfig } from '../../@types/astro.js';
import { attachContentServerListeners } from '../../content/index.js';
import { syncContentLayer } from '../../content/loaders.js';
import { telemetry } from '../../events/index.js';
import * as msg from '../messages.js';
import { ensureProcessNodeEnv } from '../util.js';
Expand Down Expand Up @@ -102,6 +103,8 @@ export default async function dev(inlineConfig: AstroInlineConfig): Promise<DevS

await attachContentServerListeners(restart.container);

await syncContentLayer({ settings: restart.container.settings, logger: logger });

logger.info(null, green('watching for file changes...'));

return {
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/types/content.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ declare module 'astro:content' {
/** A simple KV store, designed for things like sync tokens */
meta: MetaStore;
logger: import('astro').AstroIntegrationLogger;
settings: any;
settings: import('astro').AstroSettings;
/** Validates and parses the data according to the schema */
parseData<T extends Record<string, unknown> = Record<string, unknown>>(
props: ParseDataOptions
Expand Down

0 comments on commit a66df94

Please sign in to comment.