drupal-canvas 0.5.1 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -379,6 +379,11 @@ export default function MyComponent({ photo }) {
379
379
  }
380
380
  ```
381
381
 
382
+ Drupal generates no derivative images for an image its image toolkit cannot
383
+ process, an SVG image for example. Such an image is rendered as-is, without a
384
+ `srcset`. An SVG image that specifies neither its dimensions nor a `viewBox` is
385
+ rendered without `width` and `height` too, and is then sized by the browser.
386
+
382
387
  ### Region / RegionsProvider
383
388
 
384
389
  Render Drupal Canvas global regions inside a layout component.
@@ -5,26 +5,25 @@ function Image(props) {
5
5
  const { src, loader } = props;
6
6
  const srcString = typeof src === "string" ? src : "default" in src ? src.default.src : src.src;
7
7
  if (!loader) {
8
+ const alternateWidths = new URLSearchParams(srcString.split("?")[1]?.split("#")[0]).get("alternateWidths");
9
+ if (!alternateWidths) return /* @__PURE__ */ jsx(NextImage, {
10
+ ...props,
11
+ loader: () => srcString,
12
+ unoptimized: true
13
+ });
8
14
  const defaultLoader = ({ width, imageProps }) => {
9
- try {
10
- let result = new URL(srcString, "https://example.com").searchParams.get("alternateWidths");
11
- if (!result) throw new Error("Responsive image generation requires an `alternateWidths` query parameter in the image URL.");
12
- result = result.replace("{width}", width.toString());
13
- if (result.includes("{height}")) {
14
- const intrinsicWidth = Number(imageProps.width);
15
- const intrinsicHeight = Number(imageProps.height);
16
- if (!intrinsicWidth || !intrinsicHeight) throw new Error("Height calculation requires intrinsic image dimensions.");
17
- const height = Math.round(width / (intrinsicWidth / intrinsicHeight));
18
- result = result.replace("{height}", height.toString());
15
+ let result = alternateWidths.replace("{width}", width.toString());
16
+ if (result.includes("{height}")) {
17
+ const intrinsicWidth = Number(imageProps.width);
18
+ const intrinsicHeight = Number(imageProps.height);
19
+ if (!intrinsicWidth || !intrinsicHeight) {
20
+ console.error("Height calculation requires intrinsic image dimensions.", { src });
21
+ return srcString;
19
22
  }
20
- return result;
21
- } catch (error) {
22
- console.error("Responsive image generation failed. To fix this:\n1. Provide a custom `loader` function, or\n2. Ensure your image `src` includes an `alternateWidths` query parameter\n Example: ?alternateWidths=/path/to/responsive/{width}/image.jpg", {
23
- src,
24
- error
25
- });
26
- return srcString;
23
+ const height = Math.round(width / (intrinsicWidth / intrinsicHeight));
24
+ result = result.replace("{height}", height.toString());
27
25
  }
26
+ return result;
28
27
  };
29
28
  return /* @__PURE__ */ jsx(NextImage, {
30
29
  ...props,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drupal-canvas",
3
- "version": "0.5.1",
3
+ "version": "0.5.2",
4
4
  "engines": {
5
5
  "node": ">=22.19.0 <23 || >=24.5.0"
6
6
  },