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: render empty page when xml is empty (#4966)

Bogdan Chadkin7a41a1e73321
diff --git a/packages/cli/src/prebuild.ts b/packages/cli/src/prebuild.ts
index 7831edb9b2..0a51fd24c4 100644
--- a/packages/cli/src/prebuild.ts
+++ b/packages/cli/src/prebuild.ts
@@ -498,12 +498,9 @@ export const prebuild = async (options: {
// treat first body child as root
const bodyInstance = instances.get(rootInstanceId);
// @todo test empty xml
- if (
- bodyInstance &&
- bodyInstance.children.length > 0 &&
- bodyInstance.children[0].type === "id"
- ) {
- rootInstanceId = bodyInstance.children[0].value;
+ const firstChild = bodyInstance?.children.at(0);
+ if (firstChild?.type === "id") {
+ rootInstanceId = firstChild.value;
}
// remove all unexpected components
for (const instance of instances.values()) {
diff --git a/packages/react-sdk/src/component-generator.test.tsx b/packages/react-sdk/src/component-generator.test.tsx
index 21ed5a1e8c..dee11a4100 100644
--- a/packages/react-sdk/src/component-generator.test.tsx
+++ b/packages/react-sdk/src/component-generator.test.tsx
@@ -1250,3 +1250,25 @@ test("ignore ws:block-template when generate index attribute", () => {
)
);
});
+
+test("render empty component when no instances found", () => {
+ expect(
+ generateWebstudioComponent({
+ classesMap: new Map(),
+ scope: createScope(),
+ name: "Page",
+ rootInstanceId: "",
+ parameters: [],
+ metas: new Map(),
+ ...renderData(<$.Body ws:id="bodyId"></$.Body>),
+ })
+ ).toEqual(
+ validateJSX(
+ clear(`
+ const Page = () => {
+ return <></>
+ }
+ `)
+ )
+ );
+});
diff --git a/packages/react-sdk/src/component-generator.ts b/packages/react-sdk/src/component-generator.ts
index 02003573b1..dfa91b73fb 100644
--- a/packages/react-sdk/src/component-generator.ts
+++ b/packages/react-sdk/src/component-generator.ts
@@ -407,34 +407,35 @@ export const generateWebstudioComponent = ({
metas: Map<Instance["component"], WsComponentMeta>;
}) => {
const instance = instances.get(rootInstanceId);
- if (instance === undefined) {
- return "";
- }
const indexesWithinAncestors = getIndexesWithinAncestors(metas, instances, [
rootInstanceId,
]);
const usedDataSources: DataSources = new Map();
- const generatedJsx = generateJsxElement({
- context: "expression",
- scope,
- instance,
- props,
- dataSources,
- usedDataSources,
- indexesWithinAncestors,
- classesMap,
- children: generateJsxChildren({
+ let generatedJsx = "<></>\n";
+ // instance can be missing when generate xml
+ if (instance) {
+ generatedJsx = generateJsxElement({
+ context: "expression",
scope,
- children: instance.children,
- instances,
+ instance,
props,
dataSources,
usedDataSources,
indexesWithinAncestors,
classesMap,
- }),
- });
+ children: generateJsxChildren({
+ scope,
+ children: instance.children,
+ instances,
+ props,
+ dataSources,
+ usedDataSources,
+ indexesWithinAncestors,
+ classesMap,
+ }),
+ });
+ }
let generatedProps = "";
let generatedParameters = "";