Replies: 3 comments 9 replies
-
I'm a little confused with the example. Do you mind creating a Codesandbox that reproduces the issue? |
Beta Was this translation helpful? Give feedback.
-
+1, The page size is too big if we have many components. @prevwong |
Beta Was this translation helpful? Give feedback.
-
For those that stumble on this page looking for a temporary solution, here is what I have been doing: The exported JSON should be enough to create your own solution. We can code-split based on our used components as follows. // Editor Elements
const XXXX = {
TestingBarChart: import('../BuilderComponents/BarChart'),
//@ts-ignore
Container: import('../BuilderComponents/Container'),
//@ts-ignore
Text: import('../BuilderComponents/Text'),
BuilderApp: import('../BuilderComponents/BuilderApp'),
};
export default XXXX; Recursively iterate over the provided JSON to create our tree const [toRender, setToRender] = useState(null);
useEffect(() => {
const component = (() => {
const parse = async (key: string) => {
// no key, base case
if (key == null || key == '') return null;
const nodeNames: string[] = obj[key].nodes;
// Exported components with import(...)
let reactComp = await XXXX[obj[key].type.resolvedName];
// if it has no child nodes base case
if (nodeNames.length == 0) {
return React.createElement(
reactComp.default?.default ?? reactComp.default,
{
...obj[key].props
}
)
}
//iterate over children and await result
let nodes = nodeNames.map((key) => {
return parse(key);
})
nodes = await Promise.all(nodes);
return React.createElement(
reactComp.default?.default ?? reactComp.default,
{
...obj[key].props,
children: nodes,
}
)
}
return parse('ROOT');
})();
const wait = async () => {
const test = await component
setToRender(test);
}
wait();
}, [])
return <React.Fragment>
{toRender}
</React.Fragment>; Although I have not created the fallback components that do not use CraftJS dependencies yet, I can confirm that this is splitting the components how I want: Tested this on the default webpack config for Redwood.JS. |
Beta Was this translation helpful? Give feedback.
-
Using many elements/components will drastically increase bundle size, let say we have 20 components in the dashboard, even if we use only 2 of them on our page, Craft.js will load the other 18 (since we have to pass those components to
<Editor resolver={} />
).The ideal solution would be to use
React.lazy()
or next.js's Dynamic Import but none seems to work out of the box (i tried Dynamic Import without any success).Edit 3
Following @ankri idea now im able to reduce bundle size not loading unused components, this is useful when you need to render you page built with craft.js :
My page looks similar to this :
I dynamically load components from UCT.js
editor.js:
Edit 2
It seems that Craft.js resolver needs to import the function that generate the component, while react.lazy and next.js
next/dynamic
will import the component, so using default ES6 dynamic import just works.Edit.
I tried to use next.js
Dynamic import
, basically I'm interested to use Dynamic Import only when rendering the page, i have a simple page similar to this :My
UserComponents
looks like thisThis solution seems to work when components dont have childrens, instead if they have, the component will render the default (.craft.props) component data.
Example of a component with childrens :
Do you have any suggestions or ideas on how to reduce bundle size?Do you have any idea on how get Dynamic Import work properly?
Thanks
Beta Was this translation helpful? Give feedback.
All reactions