Skip to content

Commit 7a41a1e

Browse files
authored
fix: render empty page when xml is empty (#4966)
1 parent b6437fe commit 7a41a1e

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

packages/cli/src/prebuild.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -498,12 +498,9 @@ export const prebuild = async (options: {
498498
// treat first body child as root
499499
const bodyInstance = instances.get(rootInstanceId);
500500
// @todo test empty xml
501-
if (
502-
bodyInstance &&
503-
bodyInstance.children.length > 0 &&
504-
bodyInstance.children[0].type === "id"
505-
) {
506-
rootInstanceId = bodyInstance.children[0].value;
501+
const firstChild = bodyInstance?.children.at(0);
502+
if (firstChild?.type === "id") {
503+
rootInstanceId = firstChild.value;
507504
}
508505
// remove all unexpected components
509506
for (const instance of instances.values()) {

packages/react-sdk/src/component-generator.test.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,3 +1250,25 @@ test("ignore ws:block-template when generate index attribute", () => {
12501250
)
12511251
);
12521252
});
1253+
1254+
test("render empty component when no instances found", () => {
1255+
expect(
1256+
generateWebstudioComponent({
1257+
classesMap: new Map(),
1258+
scope: createScope(),
1259+
name: "Page",
1260+
rootInstanceId: "",
1261+
parameters: [],
1262+
metas: new Map(),
1263+
...renderData(<$.Body ws:id="bodyId"></$.Body>),
1264+
})
1265+
).toEqual(
1266+
validateJSX(
1267+
clear(`
1268+
const Page = () => {
1269+
return <></>
1270+
}
1271+
`)
1272+
)
1273+
);
1274+
});

packages/react-sdk/src/component-generator.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -407,34 +407,35 @@ export const generateWebstudioComponent = ({
407407
metas: Map<Instance["component"], WsComponentMeta>;
408408
}) => {
409409
const instance = instances.get(rootInstanceId);
410-
if (instance === undefined) {
411-
return "";
412-
}
413410
const indexesWithinAncestors = getIndexesWithinAncestors(metas, instances, [
414411
rootInstanceId,
415412
]);
416413

417414
const usedDataSources: DataSources = new Map();
418-
const generatedJsx = generateJsxElement({
419-
context: "expression",
420-
scope,
421-
instance,
422-
props,
423-
dataSources,
424-
usedDataSources,
425-
indexesWithinAncestors,
426-
classesMap,
427-
children: generateJsxChildren({
415+
let generatedJsx = "<></>\n";
416+
// instance can be missing when generate xml
417+
if (instance) {
418+
generatedJsx = generateJsxElement({
419+
context: "expression",
428420
scope,
429-
children: instance.children,
430-
instances,
421+
instance,
431422
props,
432423
dataSources,
433424
usedDataSources,
434425
indexesWithinAncestors,
435426
classesMap,
436-
}),
437-
});
427+
children: generateJsxChildren({
428+
scope,
429+
children: instance.children,
430+
instances,
431+
props,
432+
dataSources,
433+
usedDataSources,
434+
indexesWithinAncestors,
435+
classesMap,
436+
}),
437+
});
438+
}
438439

439440
let generatedProps = "";
440441
let generatedParameters = "";

0 commit comments

Comments
 (0)