Skip to content

fix: render empty page when xml is empty #4966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions packages/cli/src/prebuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
22 changes: 22 additions & 0 deletions packages/react-sdk/src/component-generator.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <></>
}
`)
)
);
});
35 changes: 18 additions & 17 deletions packages/react-sdk/src/component-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down