/** * @title Splitting a page into its own chunk with Component.lazy * * Load a page from its own chunk with `Component.lazy`, with a fallback and * preloading. */ import { Effect, Schema } from "effect" import { Command, Component, UiSchema } from "effect/ui" import { Html } from "effect/ui/html" import { NotesIndex } from "../fixtures/pages/NotesIndex.ts" import { AppRoute } from "../fixtures/routes.ts" // The `import()` must be the only reference to the page's module. A static // import of it anywhere in the app (`fixtures/App.ts` has one, so this shell // replaces it) pulls the page back into the entry chunk. A type-only import // is fine. Props, events, slots and requirements come from the page's type, // so the call below is checked like a direct one. // // The page provides its own Layers with `Component.provide` in its module, so // they travel into its chunk. `"SearchPage"` is the child's path segment, // needed before the module arrives; a `Scene` names the child by this lazy // value, as in `s.childEmit(SearchPage, event)`. const SearchPage = Component.lazy( "SearchPage", () => import("../fixtures/pages/search/SearchPage.ts").then((module) => module.SearchPage) ) // To prefetch queries too, add a handler to `Router.prefetch`: // `Search: (route) => [Router.page(SearchPage, { route })]`. Link intent loads // the module, then prefetches its queries. Provide their Layers at the // declaring component or above it; providing them only inside the lazy page // is insufficient. This example keeps the Layers in the page's chunk and // uses a Command to preload only the module. const PreloadSearch = Command.make( "PreloadSearch", {}, () => Effect.tryPromise(() => SearchPage.preload()).pipe(Effect.ignore) ) const ShellMessage = UiSchema.TaggedEnum({ PointedAtSearchLink: {} }) export const Shell = Html.component({ name: "Shell", props: Schema.Struct({ route: AppRoute }), model: Schema.Struct({}), message: ShellMessage }).make({ init: () => ({}), update: { PointedAtSearchLink: ($) => $.run(PreloadSearch.make().pipe(Command.asNoop)) }, view: (h) => h.div( h.nav( h.a.attr("href", AppRoute.Search.href()) .on("pointerenter", ShellMessage.PointedAtSearchLink()) .children("Search") ), AppRoute.match(h.props.route, { Search: (route) => // `fallback` shows while the module loads (`Initial`) and if the // load fails (`Failure`). The module stays loaded for the document's // life, so later visits mount at once. A failed load retries on the // next mount. SearchPage(h, { key: AppRoute.printPath(route), props: { route }, fallback: (result) => h.p(result._tag === "Failure" ? "Couldn't load search." : "Loading…") }), Index: (route) => NotesIndex(h, { key: AppRoute.printPath(route) }), Note: () => h.p("Open a note from the list."), NotFound: () => h.p("Page not found") }) ) }) // Bundling: // - A browser may cache a failed chunk request and fail every later load. // In the browser entry, reload on Vite's event: // `window.addEventListener("vite:preloadError", () => location.reload())`. // - Don't put `effect` into one vendor chunk with Vite's `manualChunks` or // `advancedChunks`. Framework modules only a lazy page uses, such as // `Query`, move back into the initial load, which can end up larger than // with no splitting at all. // - Nothing preloads the chunk when a link opens the page directly: the // entry chunk loads, then the page's. // - Only a component can be lazy. A headless component or a view function // can't; make a page written as a view function a component first.