Performance: Block development basics
This affects the page load speed and interaction responsiveness by how blocks are registered and rendered in WordPress editor and frontend.
import { registerBlockType } from '@wordpress/blocks'; import Edit from './edit'; import save from './save'; import './style.css'; registerBlockType('myplugin/block', { edit: Edit, save, });
wp.blocks.registerBlockType('myplugin/block', { edit: () => { return wp.element.createElement('div', null, 'Hello World'); }, save: () => { return wp.element.createElement('div', null, 'Hello World'); }, // Inline styles and scripts added directly in edit and save });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Inline scripts and styles in block | Moderate DOM nodes | Multiple reflows due to style recalculations | High paint cost from unoptimized styles | [X] Bad |
| Separate JS and CSS files for block | Same DOM nodes | Single reflow after style load | Lower paint cost due to cached styles | [OK] Good |
registerBlockType.createBlockType, addBlock, and initBlock do not exist in WordPress block API.registerBlockType('myplugin/example', {
edit: () => {
return Hello, WordPress!
;
},
save: () => {
return Hello, WordPress!
;
}
});registerBlockType('myplugin/test', {
edit: () => {
return Test Block;
},
save: () => {
return div>Test Block;
}
});div>Test Block; which is missing the opening < for the div tag.registerBlockType and accessed via props.attributes in both edit and save functions.Pass the test and this pattern moves unknown