diff --git a/webp.module b/webp.module
index fc09081..37be852 100644
--- a/webp.module
+++ b/webp.module
@@ -22,3 +22,25 @@ function webp_help($route_name, RouteMatchInterface $route_match) {
     default:
   }
 }
+
+/**
+ * Implements template_preprocess_responsive_image().
+ */
+function webp_preprocess_responsive_image(&$variables) {
+  $webp_sources = [];
+
+  foreach ($variables['sources'] as $source) {
+    /** @var \Drupal\Core\Template\Attribute $source */
+    $original_url = $source->offsetGet('srcset')->value();
+    $webp_url = str_ireplace(['.png', '.jpg', '.jpeg'], '.webp', $original_url);
+
+    // Create a new source pointing to the webp URL.
+    $webp_source = clone $source;
+    $webp_source->offsetSet('srcset', $webp_url);
+    $webp_source->offsetSet('type', 'image/webp');
+    $webp_sources[] = $webp_source;
+  }
+
+  // Add the new sources at the top of the list.
+  $variables['sources'] = array_merge($webp_sources, $variables['sources']);
+}
\ No newline at end of file
