Feature: Next.js-style image optimization for Experience Builder
Image Delivery Flow Architecture
The image optimization feature follows a well-structured flow for delivering optimized images:
- Component Rendering
When an Experience Builder image component is rendered:
- The component generates optimized URLs with transformation parameters
- The URLs include width, height, format, quality, and a security token
- Example URL: /system/files/xb-styles/public/path/to/image.jpg?w=800&h=600&f=webp&q=80&itok=SECURITY_TOKEN
- For responsive images, multiple sizes are generated in a srcset attribute
- The Twig template may use elements with different sources for various formats
- URL Processing
When a client requests an optimized image:
- The request is intercepted by PathProcessorResponsiveImages
- The processor extracts parameters from the URL path and query string
- The processor rewrites the path to a controller route pattern
- The ResponsiveImageRoutes service directs the request to the ResponsiveImageController
- Controller Processing
The ResponsiveImageController handles the image request by:
- Validating the security token using the ImageUrlManager
- Determining the original image path from the request
- Checking if a derivative already exists
- If not, using the ImageStyleManager to generate the derivative
- Setting appropriate caching headers (31536000 seconds, or 1 year)
- Serving the image with the correct Content-Type header
- Image Transformation
When a new derivative needs to be created:
- The ImageStyleManager determines the correct derivative path
- It then calls the ImageTransformationService to perform the transformations
- The transformation service:
- Opens the original image using the QualityAwareImageFactory
- Resizes the image to the requested dimensions
- Converts the image to the requested format (WebP, AVIF, etc.)
- Applies the requested quality setting using the QualityAwareImage extensions
- Saves the derivative to the file system
- File Storage
Derivative images are stored with a structured pattern:
- Base path: /[scheme]://xb-responsive/
- Dimensions and quality: /[width]x[height]-[quality]/
- Format: /[format]/
- Original path structure: /[original-directory]/[original-filename].[original-ext].[new-ext]
- Example: public://xb-responsive/800x600-80/webp/images/photo.jpg.webp
- Client Delivery
The optimized image is delivered to the client with:
- The correct Content-Type header for the image format
- Cache headers for efficient browser caching
- If supported, modern formats like WebP or AVIF
- Otherwise, fallback to the original format
- Quality Control
The quality settings work through:
- The QualityAwareImage class which extends Drupal's core Image class
- A custom JPEG quality application that bypasses Drupal's default quality settings
- Custom toolkit parameter handling that only applies to supported formats
Key Components
- ImageTransformationService: Performs actual image manipulations
- QualityAwareImage & QualityAwareImageFactory: Extend Drupal's image handling with quality controls
- ImageUrlManager: Generates and validates security tokens for images
- ImageStyleManager: Manages derivative creation and caching
- PathProcessorResponsiveImages: Handles URL routing for image requests
- ResponsiveImageController: Serves optimized images
- ResponsiveImageTwigExtension: Provides Twig functions for templates
The architecture leverages Drupal's existing image handling system while extending it with modern optimization techniques, creating a powerful and flexible image optimization system within the Experience Builder.
Edited by Matthew Grasmick