Image not displayed correctly because image path is relative

Created on 17 April 2024, 2 months ago
Updated 7 May 2024, about 2 months ago

Problem/Motivation

I try to use it with Drupal's SDC and Twig Tweak module to create a Hero image component.
I use Twig Tweak because it's nicely printed <picture> tag for responsive image just be using file ID.

But when I check the Storybook, the image is not loaded because the image path is relative.
What would be the fix here? Can I set something like a base URL on the Storybook.js side? Or maybe I missed some configuration?

Steps to reproduce

I have a simple story like this:

{% story hero_tall with {
    name: 'Tall',
    args: {
      title: 'I am a header!',
      description: 'Example text for Tall Hero' }
    }
%}

  {{
    include('mytest:hero', {
      size: 'tall',
      title: title,
      description: description,
      background: {
        fid: 2,
        alt: 'Image text'
      }
    })
  }}

{% endstory %}

and hero.twig like this:

{{
  drupal_image(background.fid, 'hero_image', {alt: background.alt, loading: 'eager'}, true)
}}

Proposed resolution

N/A

Remaining tasks

N/A

User interface changes

N/A

API changes

N/A

Data model changes

N/A

💬 Support request
Status

Needs review

Version

1.0

Component

Storybook

Created by

🇮🇩Indonesia permanaj

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @permanaj
  • 🇮🇩Indonesia permanaj

    Apparently I missed some configuration on .storybook/main.js. After adding staticDirs: ['../../../../../web'] to config, the image displayed correctly

  • 🇬🇧United Kingdom khaled.zaidan

    Setting staticDirs to your web root still isn't ideal, as this means if you try to push storybook to Chromatic, it will attempt to push your entire website (which Chromatic won't allow).

    I would be nice to have something to dynamically push image derivatives and/or change image paths before the render to story book. That way you can target the exact asset files you need.

    I've only just run into this now, so I'm not sure what the best solution is or whether someone has already figured it out. I'll update here if I come across a better approach.

  • 🇬🇧United Kingdom khaled.zaidan

    I'm thinking something along the lines of:
    1. User manually sets a derivatives static directory in the files folder. E.g. staticDirs: ['../web/sites/default/files/storybook-derivatives']
    2. When rendering the story (in storybook's route), we make the code check for any image derivatives.
    3. Any image derivatives found, we first make ensure the derivative is generated and make a copy to the storybook derivatives dir.
    4. Then modify the markup to the image derivatives point their paths to the new copies we've created
    5. This could all be configured/enabled/disabled through the module configs, so it can accommodate different builds as needed.

  • 🇬🇧United Kingdom khaled.zaidan

    Here's a simple patch to do the alternative approach I mentioned. This might be enough on its own!

  • Status changed to Needs review about 2 months ago
  • 🇬🇧United Kingdom khaled.zaidan

    It would be very helpful if someone could review this patch.

Production build 0.69.0 2024