Introduction to Partials
Partials, inspired by htmx, operate by intercepting user interactions on button, anchor, and form elements. These interactions are automatically offloaded to our server, where they generate a new User Interface (UI) state. This fresh UI state is transmitted as pure HTML back to the user’s browser. Our runtime seamlessly replaces and hydrates the new UI state, creating the illusion of client-side interactivity. In reality, the entire computation process occurs in mere milliseconds on our Edge Servers. For more detailed information on Partials, please consult Fresh’s documentation.Simplifying Development
While Fresh Partials introduce a new realm of performance optimization, they also bring added complexity to the development cycle. Developers now need to consider multiple rendering modes, their impact on bundle and HTML sizes, interaction latencies, and how to properly navigate partially rendered pages. To streamline this process, Deco has integrated Partials into a higher abstraction layer of our framework, eliminating the need for developers to grapple with these complexities.Leveraging Partials for Sections
At Deco, all Sections are treated as partials. This means you can seamlessly incorporate client-side interactivity into any Section without compromising bundle sizes or initial page loads. This is particularly beneficial for creating common UI components such as SKU selectors, infinite scroll features, and tabs. In the following sections, we’ll delve into harnessing the full potential of Partials by creating a lightning-fast SKU selector.Example: SKU Selector
SKU selectors enable shoppers to explore different variations of a product, each with its unique set of prices, images, and availability. Consider the example below:Note: If you only isolate the buttons as islands, selecting an SKU will not update the displayed price or image, leading to an inconsistent UI.An alternative approach is to transform the buttons into anchor tags, triggering a new page load upon each SKU selection:
Enhancing UX and Performance with Partials
usePartial
hook combined with the button
tag.
This hook accepts an href
parameter and enhances the button tag. When the user
clicks the button, it triggers client-side navigation and applies HTML diff
updates. This approach eliminates the need for islands, boosting performance,
while maintaining the scroll position for an improved user experience.
Example: Tabs
While we’ve explored how to leverage Partials for SKU selectors, tabbed navigation presents a unique challenge. Tabs lack canonical URLs, making it challenging to manage state transitions. However, theusePartialSection
hook
allows us to override the props that a section uses for rendering, simplifying
the process:
activeIndex
prop is overridden with each
usePartialSection
call, simplifying the use of Partials and eliminating the
need to manage URLs and search parameters within our sections.