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: Lazyload islands components in the client side #565

Open
Khaledinges opened this issue Jul 31, 2022 · 3 comments
Open

Feature: Lazyload islands components in the client side #565

Khaledinges opened this issue Jul 31, 2022 · 3 comments
Labels
feat New feature or request

Comments

@Khaledinges
Copy link

Hello !

It comes to my mind that I wouldn't want to load all the islands JS chunks on the page load in the client side. Obviously, it would be for performance optimization reasons.

would it be possible to inject some islands JS components code when needed (lazyloaded) or on user interactions ?

@lucacasonato
Copy link
Member

This would be a good addition, I agree. Ideally one should be able to set when the island should be loaded via either an option in the island file (at declaration), or at the island instantiation (at island use).

I think "at declartion" would be an easier first step. Maybe something along the lines of:

// islands/Island.tsx

export default function Island() { ... }

// When to load the island. Defaults to "eager".
// Valid options are:
//   - "eager": load as soon as possible (use for above the fold islands).
//   - "viewport": load when the island is about to enter the viewport.
//   - "interaction": when an interactive event is encountered by the island. The event will be captured, the island loaded, and then the event is replayed.
export const load = "eager";

@lucacasonato lucacasonato added the feat New feature or request label Jul 31, 2022
@lucacasonato lucacasonato mentioned this issue Aug 1, 2022
11 tasks
@Khaledinges
Copy link
Author

Great looks perfect ! Thank you

@igorbrasileiro
Copy link
Contributor

igorbrasileiro commented Mar 28, 2023

It seems that islands already can lazy load to user interaction via Suspense. Take a look at this example:

import { Suspense, Lazy }  from "preact/compat"
import {useState} from "preact/hooks"

const Comp = lazy(() => import("../component/Comp.tsx"))
export default function Component() {
  const [render, setRender] = useState(false);
  return <div>{render && <Comp />} <button onClick={() => setRendeR(true)}>click</button></div>
}

In addition, IMO, islands can be rendered from fresh following the browser scheduler.postTask improving the UX and decreasing the TBT. I opened a PR doing this (#1019) for islands, this could be implemented for plugins too.

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

No branches or pull requests

3 participants