Skip to content

Support collection sources with custom cwd paths #313

@FrederikBN

Description

@FrederikBN

Environment

  • nuxt-studio: 1.2.0
  • @nuxt/content: 3.x
  • nuxt: 4.x

Version

1.2

Reproduction

Project Structure

monorepo/
├── apps/website/
│   ├── nuxt.config.ts
│   └── content.config.ts
└── packages/content-layer/
    └── content/
        └── index.md

Configuration

// apps/website/content.config.ts
import { defineCollection, defineContentConfig } from '@nuxt/content'
import { resolve } from 'path'

export default defineContentConfig({
  collections: {
    pages: defineCollection({
      type: 'page',
      source: {
        cwd: resolve(__dirname, '../../packages/content-layer/content'),
        include: '**/*.md',
      },
    }),
  },
})

Steps

  1. Run dev server in apps/website
  2. Open Studio and edit index.md
  3. Live preview does not update

Expected

  • File written to packages/content-layer/content/index.md
  • watcher detects change
  • preview updates

Actual

  • File written to apps/website/content/index.md (?)
  • watcher sees no change
  • preview stale

Root Cause

1. Hardcoded storage path

src/module/module.ts - setupDevMode:

nuxt_studio_content: {
  driver: "fs",
  base: resolve(nuxt.options.rootDir, "content")  // Ignores collection cwd
}

2. Path matching ignores cwd

src/module/runtime/utils/collection.ts - getCollectionByFilePath:

    return paths.some((p) => {
      matchedSource = collection2.source.find((source) => {
        const include = minimatch(p, source.include);
        const exclude = source.exclude?.some((exclude2) => minimatch(p, exclude2));
        return include && !exclude;
      });
      return matchedSource;
    });
// source.cwd is not used in path resolution

3. Path generation ignores cwd

src/module/runtime/utils/collection.ts - generateFsPathFromId:

export function generateFsPathFromId(id, source) {
  const [_, ...rest] = id.split(/[/:]/);
  let path = rest.join("/");
  const { fixed } = parseSourceBase(source);
  const normalizedFixed = withoutTrailingSlash(fixed);
  const prefix = withoutTrailingSlash(withoutLeadingSlash(source.prefix || ""));
  if (prefix && prefix !== "/" && path.startsWith(prefix + "/")) {
    path = path.substring(prefix.length + 1);
  }
  if (normalizedFixed && path.startsWith(normalizedFixed)) {
    return path;
  }
  return join(fixed, path);
}
// Does not incorporate source.cwd

Description

nuxt-studio does not respect the cwd property in Nuxt Content v3 collection sources. In dev mode, files are written to {rootDir}/content/ regardless of the collection's configured cwd, breaking live preview for content sourced from layers or monorepo packages.

Additional context

In general i have had a hard time with incorporating nuxt studio in a context where there is an idea of layers. This seems not to be just a studio problem, but also an issue within nuxt content. I have come up with some workarounds to get nuxt content to be happy and also showcase the files within studio, but this breaks the live preview as previously mentioned.

I hope that there would come support for the usage of the CWD option, since it is supported by nuxt content and it would make it possible for me to use nuxt studio outside of a monorep context.

At the end i underline that i do appreciate nuxt studio and the efforts being made - it is a really cool product!

Logs

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions