Skip to content
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

[FEATURE] - Blocks Lazy Loading #778

Open
volfkarlo opened this issue Jan 3, 2024 · 1 comment
Open

[FEATURE] - Blocks Lazy Loading #778

volfkarlo opened this issue Jan 3, 2024 · 1 comment
Assignees
Labels
feature request New feature request

Comments

@volfkarlo
Copy link
Contributor

We might take a look at lazy loading of the blocks inside the boilerplate.
It might lower the initial load size and reduce the main thread execution time.

This is the idea:

import React from 'react';
import { props } from '@eightshift/frontend-libs/scripts';
import { GridEditor as GridEditorComponent } from '../../../components/grid/components/grid-editor';

export const GridEditor = ({
	clientId,
	attributes,
	isSelected,
	setAttributes,
	showGridGuides
}) => {

	return (
		<GridEditorComponent
			{...props('grid', attributes, {
				setAttributes,
			})}
			showGridGuides={showGridGuides}
			clientId={clientId}
			isSelected={isSelected}
		/>
	);
};

export default GridEditor;

Notice the default export at the end

import React, { useState, Suspense } from 'react';
import { InspectorControls } from '@wordpress/block-editor';

export const Grid = (props) => {
	const [showGridGuides, setShowGridGuides] = useState(true);

	const GridOptions = React.lazy(() => import('./components/grid-options'));
	const GridEditor = React.lazy(() => import('./components/grid-editor'));

	return (
		<>
			<InspectorControls>
				<Suspense fallback={<>Loading...</>}>
					<GridOptions {...props} showGridGuides={showGridGuides} setShowGridGuides={setShowGridGuides} />
				</Suspense>
			</InspectorControls>
			<Suspense fallback={<>Loading...</>}>
				<GridEditor {...props} showGridGuides={showGridGuides} />
			</Suspense>
		</>
	);
};

Pontentially we can build up some kind of helper for solving this, something like:

const lazying = (url) => {
  const Component = React.lazy(() => import(url));
  return (
  <Suspense fallback={<div>loading...</div>}>
    <Component />
  </Suspense>
  );
};

But in a way that it works(this solution doesn't work).

What you are suppose to get is something like this inside the editor part
Screenshot 2024-01-03 at 10 43 14
notice the additional file next to *Editor.js

@goranalkovic-infinum and I've tested it out, the savings for Grid block are measured in KBs but it might make a difference if everything is written in that way. I don't think this should be high on the priority list for testing, but potentially something to look into.

KR,
Karlo

@volfkarlo volfkarlo added the feature request New feature request label Jan 3, 2024
@iruzevic iruzevic assigned volfkarlo and unassigned iruzevic Jan 3, 2024
@mbmjertan
Copy link
Collaborator

I think this will make a noticeable impact on block editor performance on sites with a large number of blocks, given all blocks are imported by default.

I also considered something like this for the frontend with the intersection observer API, but I'm not sure whether the overhead of observers plus network requests will make it a net positive impact.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature request
Projects
None yet
Development

No branches or pull requests

3 participants