Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

fix: Compile dynamic MDX from mutable variables (#6472)

Oleg Isonenc0ddc4dca01a
diff --git a/packages/cli/src/commands/sync.test.ts b/packages/cli/src/commands/sync.test.ts
index 891727e7b6..6fa2e95544 100644
--- a/packages/cli/src/commands/sync.test.ts
+++ b/packages/cli/src/commands/sync.test.ts
@@ -15,6 +15,7 @@ import { promisify } from "node:util";
import { build } from "esbuild";
import {
createStructuredAssetQueryResourceBody,
+ encodeDataVariableId,
encodeDataSourceVariable,
} from "@webstudio-is/sdk";
import {
@@ -345,6 +346,132 @@ test.each([
}
);
+test("hosted sync compiles all MDX assets reachable from a mutable project variable", async () => {
+ const article = {
+ ...createImageAssetFixture(),
+ id: "article",
+ name: "article.mdx",
+ type: "file" as const,
+ format: "mdx",
+ size: new TextEncoder().encode("# Current project variable value")
+ .byteLength,
+ meta: {},
+ };
+ const updatedArticle = {
+ ...createImageAssetFixture(),
+ id: "updated-article",
+ name: "updated-article.mdx",
+ type: "file" as const,
+ format: "mdx",
+ size: new TextEncoder().encode("# Updated project variable value")
+ .byteLength,
+ meta: {},
+ };
+ const articles = [article, updatedArticle];
+ const sources = new Map([
+ [article.id, "# Current project variable value"],
+ [updatedArticle.id, "# Updated project variable value"],
+ ]);
+ loadProjectBundleByBuildId.mockResolvedValue(
+ createProjectBundle({
+ assets: articles,
+ build: {
+ deployment: { destination: "saas", domains: [] },
+ instances: [
+ [
+ "root",
+ {
+ type: "instance",
+ id: "root",
+ component: "Box",
+ children: [{ type: "id", value: "content" }],
+ },
+ ],
+ [
+ "content",
+ {
+ type: "instance",
+ id: "content",
+ component: "ws:block",
+ children: [{ type: "id", value: "templates" }],
+ },
+ ],
+ [
+ "templates",
+ {
+ type: "instance",
+ id: "templates",
+ component: "ws:block-template",
+ children: [],
+ },
+ ],
+ ],
+ props: [
+ [
+ "src",
+ {
+ id: "src",
+ instanceId: "content",
+ name: "src",
+ type: "expression",
+ value: encodeDataVariableId("article-source"),
+ },
+ ],
+ ],
+ dataSources: [
+ [
+ "article-source",
+ {
+ id: "article-source",
+ type: "variable",
+ scopeInstanceId: "content",
+ name: "articleSource",
+ value: { type: "string", value: article.id },
+ },
+ ],
+ ],
+ },
+ })
+ );
+ downloadAssetFiles.mockImplementation(async ({ assets }) => {
+ await mkdir(".webstudio/assets", { recursive: true });
+ await Promise.all(
+ assets.map((asset: { id: string; name: string }) =>
+ writeFile(
+ `.webstudio/assets/${asset.name}`,
+ sources.get(asset.id) ?? ""
+ )
+ )
+ );
+ });
+
+ await sync(
+ {
+ authToken: "token-1",
+ buildId: "build-1",
+ origin: "https://example.com",
+ },
+ dependencies
+ );
+
+ expect(downloadAssetFiles).toHaveBeenCalledWith({
+ assets: expect.arrayContaining([article, updatedArticle]),
+ origin: "https://example.com",
+ });
+ const data = JSON.parse(await readFile(".webstudio/data.json", "utf8"));
+ for (const [id, source] of sources) {
+ expect(data.assetIndex.documents).toContainEqual(
+ expect.objectContaining({ _id: id, extension: "mdx" })
+ );
+ expect(Object.values(data.assetIndex.contents)).toContain(source);
+ }
+
+ await prebuild({ assets: false, template: ["react-router"] });
+ const generatedPage = await readFile("app/__generated__/_index.tsx", "utf8");
+ expect(generatedPage).toContain("Current project variable value");
+ expect(generatedPage).toContain("Updated project variable value");
+});
+
test("hosted sync compiles metadata-only dynamic MDX into a server-rendered page", async () => {
const source =
"---\nslug: post\ntitle: Article title\n---\n# Published article body";
diff --git a/packages/cli/src/commands/sync.ts b/packages/cli/src/commands/sync.ts
index c16e78937d..62b0168d24 100644
--- a/packages/cli/src/commands/sync.ts
+++ b/packages/cli/src/commands/sync.ts
@@ -35,6 +35,7 @@ import {
createPublishedBuildContentCompilationPlan,
createPublishedMdxDependencyClosureResolver,
getDynamicPublishedMdxSourceBlockIds,
+ getPublishedMdxAssetIds,
getPublishedMdxContentDatabaseMaxBytes,
resolvePublishedMdxAssetCandidates,
} from "@webstudio-is/project-build";
@@ -256,10 +257,12 @@ export const sync = async (
...project.build,
pages: migratePages(project.build.pages),
};
+ const allMdxAssetIds = getPublishedMdxAssetIds(project.assets);
const projectCandidates = resolvePublishedMdxAssetCandidates({
build: publicationBuild,
artifact: project.assetIndex,
allowUnresolved: true,
+ allMdxAssetIds,
});
const dynamicBlockIds =
getDynamicPublishedMdxSourceBlockIds(publicationBuild);
@@ -338,7 +341,11 @@ export const sync = async (
plan,
compile,
resolvePlan: async (artifact) =>
- await resolvePlan({ build: publicationBuild, artifact }),
+ await resolvePlan({
+ build: publicationBuild,
+ artifact,
+ allMdxAssetIds,
+ }),
});
} else {
project.assetIndex = await compile(plan);
diff --git a/packages/cli/src/prebuild.ts b/packages/cli/src/prebuild.ts
index 621945b7b9..1633b1017a 100644
--- a/packages/cli/src/prebuild.ts
+++ b/packages/cli/src/prebuild.ts
@@ -75,6 +75,7 @@ import {
import {
createPublishedBuildContentCompilationPlan,
getDynamicPublishedMdxSourceBlockIds,
+ getPublishedMdxAssetIds,
hasDynamicPublishedMdxSources,
resolvePublishedMdxAssetCandidates,
} from "@webstudio-is/project-build";
@@ -976,6 +977,7 @@ export const prebuild = async (options: {
const siteData = parsedSiteData.data;
const pages = migratePages(siteData.build.pages);
const publicationBuild = { ...siteData.build, pages };
+ const allMdxAssetIds = getPublishedMdxAssetIds(siteData.assets);
if (
siteData.assetIndex === undefined &&
(createPublishedBuildContentCompilationPlan(publicationBuild) !==
@@ -1058,14 +1060,12 @@ export const prebuild = async (options: {
}
let dynamicMdxCandidates: ReadonlyMap<string, readonly string[]> | undefined;
if (hasDynamicPublishedMdxSources(publicationBuild)) {
- dynamicMdxCandidates =
- verifiedAssetIndex === undefined
- ? new Map()
- : resolvePublishedMdxAssetCandidates({
- build: publicationBuild,
- artifact: verifiedAssetIndex,
- allowUnresolved: true,
- });
+ dynamicMdxCandidates = resolvePublishedMdxAssetCandidates({
+ build: publicationBuild,
+ artifact: verifiedAssetIndex,
+ allowUnresolved: true,
+ allMdxAssetIds,
+ });
for (const blockInstanceId of getDynamicPublishedMdxSourceBlockIds(
publicationBuild
)) {
@@ -1332,6 +1332,7 @@ export const prebuild = async (options: {
build: pageBuild,
artifact: verifiedAssetIndex,
blockInstanceIds: pendingBlockIds,
+ allMdxAssetIds,
})
: undefined;
const materialized = await materializePublishedMdx({
diff --git a/packages/project-build/src/content-database.test.ts b/packages/project-build/src/content-database.test.ts
index 7685521364..515b0cd5f5 100644
--- a/packages/project-build/src/content-database.test.ts
+++ b/packages/project-build/src/content-database.test.ts
@@ -556,7 +556,7 @@ describe("Content Block MDX compilation", () => {
}
);
- test("rejects a mutable project variable as an incomplete candidate set", () => {
+ test("bounds a mutable project variable to the project's MDX assets", () => {
const build = createBuild({ sourceType: "expression" });
build.props[0].value = encodeDataVariableId("article-source");
build.dataSources.push({
@@ -567,6 +567,12 @@ describe("Content Block MDX compilation", () => {
value: { type: "string", value: "article.mdx" },
});
+ expect(
+ resolvePublishedMdxAssetCandidates({
+ build,
+ allMdxAssetIds: new Set(["article.mdx", "another-article.mdx"]),
+ }).get("block")
+ ).toEqual(["another-article.mdx", "article.mdx"]);
expect(() => resolvePublishedMdxAssetCandidates({ build })).toThrow(
'Content Block "block" has no finite dynamic MDX Asset candidates'
);
diff --git a/packages/project-build/src/content-database.ts b/packages/project-build/src/content-database.ts
index b0768b100b..151bb69311 100644
--- a/packages/project-build/src/content-database.ts
+++ b/packages/project-build/src/content-database.ts
@@ -28,6 +28,7 @@ import {
getStaticContentBlockSourceAssetId,
isMdxFileAsset,
getPublishablePages,
+ type Asset,
type DataSource,
type Instance,
type Prop,
@@ -59,6 +60,9 @@ type PublishedContentDatabaseBuild = ContentDatabaseBuild & {
const publishedMdxTotalBytes = 32 * 1024 * 1024;
+export const getPublishedMdxAssetIds = (assets: readonly Asset[]) =>
+ new Set(assets.filter(isMdxFileAsset).map(({ id }) => id));
+
export const getPublishedMdxContentDatabaseMaxBytes = ({
baseBytes,
assets,
@@ -227,12 +231,14 @@ type ResolvePublishedMdxDependencyClosureOptions = {
build: PublishedContentDatabaseBuild;
artifact: ContentArtifactV1;
onTemplateOmission?: (issue: PublishedMdxTemplateOmission) => void;
+ allMdxAssetIds?: ReadonlySet<string>;
};
const resolvePublishedMdxDependencyClosureWithParser = async ({
build,
artifact,
onTemplateOmission,
+ allMdxAssetIds,
parseDocument,
}: ResolvePublishedMdxDependencyClosureOptions & {
parseDocument: (input: {
@@ -295,6 +301,7 @@ const resolvePublishedMdxDependencyClosureWithParser = async ({
build,
artifact,
blockInstanceIds: new Set([blockId]),
+ allMdxAssetIds,
}).get(blockId) ?? []);
for (const assetId of assetIds) {
const documentEntry = documentsById.get(assetId);
@@ -352,6 +359,7 @@ const resolvePublishedMdxDependencyClosureWithParser = async ({
artifact,
blockInstanceIds: reachableIds,
allowUnresolved: true,
+ allMdxAssetIds,
});
return createPublishedBuildContentCompilationPlan(
build,
@@ -439,11 +447,13 @@ export const resolvePublishedMdxAssetCandidates = ({
artifact,
allowUnresolved = false,
blockInstanceIds = getPublishedInstanceIds(build),
+ allMdxAssetIds,
}: {
build: PublishedContentDatabaseBuild;
artifact?: ContentArtifactV1;
allowUnresolved?: boolean;
blockInstanceIds?: ReadonlySet<string>;
+ allMdxAssetIds?: ReadonlySet<string>;
}) => {
const instances = new Map(
getBuildValues<Instance>(build.instances).map((instance) => [
@@ -479,7 +489,8 @@ export const resolvePublishedMdxAssetCandidates = ({
const evaluateDataSource = (
dataSourceId: string,
path: readonly string[],
- visiting = new Set<string>()
+ visiting = new Set<string>(),
+ mutableVariables = new Set<string>()
): unknown[] => {
if (visiting.has(dataSourceId)) {
throw new Error("Dynamic MDX source data dependency is cyclic");
@@ -491,6 +502,7 @@ export const resolvePublishedMdxAssetCandidates = ({
if (dataSource.type === "variable") {
// Project variables can be changed by actions after publication. Their
// initial value is therefore not a complete dependency set.
+ mutableVariables.add(dataSourceId);
return [];
}
if (dataSource.type === "resource") {
@@ -584,7 +596,8 @@ export const resolvePublishedMdxAssetCandidates = ({
iterables = evaluateDataSource(
dependencyId,
dataPath.slice(1),
- visiting
+ visiting,
+ mutableVariables
);
visiting.delete(dataSourceId);
}
@@ -628,11 +641,17 @@ export const resolvePublishedMdxAssetCandidates = ({
const dataSourceId =
path === undefined ? undefined : decodeDataSourceVariable(path[0]);
let assetIds: string[] = [];
+ const mutableVariables = new Set<string>();
try {
assetIds =
dataSourceId === undefined || path === undefined
? []
- : evaluateDataSource(dataSourceId, path.slice(1)).filter(
+ : evaluateDataSource(
+ dataSourceId,
+ path.slice(1),
+ new Set(),
+ mutableVariables
+ ).filter(
(value): value is string =>
typeof value === "string" && value.length > 0
);
@@ -641,6 +660,9 @@ export const resolvePublishedMdxAssetCandidates = ({
throw error;
}
}
+ if (mutableVariables.size > 0 && allMdxAssetIds !== undefined) {
+ assetIds.push(...allMdxAssetIds);
+ }
if (assetIds.length === 0) {
if (allowUnresolved) {
continue;