#Dynamic URL background image in tailwind fetched from local file via custom protocol not working

1 messages · Page 1 of 1 (latest)

novel wigeon
#

Hello,

I am trying to dynamically load an image file stored locally in the app.get('userData') (aka Application Support on macOS) directory, via a custom protocol, as a background image using Tailwind with React. Here is some simplified code of what I am trying to do:


export function Cover() {

const [coverImagePath, setCoverImagePath] = useState<string>('');

useEffect(() => {
    const fetchInitialValues = async () => {
      const path = `ocheeflow:///Users/xyz/Library/Application Support/ocheeflowxp/bg-images/sunset_wallpaper.jpg`;
      setCoverImagePath(path);
    };
    fetchInitialValues();
  }, []);

return (
    <>
      <div
        style={{'--image-url': `url(${coverImagePath})`} as React.CSSProperties} 
        className='bg-[image:var(--image-url)] bg-no-repeat bg-cover bg-center'
      >
        <p>Hello world</p>
      </div>
    </>
  );
}

There is no error in the console, and the background just appears black (see attached image). I have tested this code with a url from a public hosted image (e.g. const path = https://picsum.photos/id/17/2500/1667, and the background image is displayed, so I'm unsure if tailwind can even be used to fetch local files dynamically.

I know the issue is not with my custom protocol, as I am using it elsewhere in my app's UI successfully.

Here is my custom protocol code for reference:

app.on("ready", () => {
  ...

  // Custom protocol handler to show images in renderer
  protocol.handle('ocheeflow', (request) => {
    const filePath = request.url.slice('ocheeflow://'.length);
    const updatedPrefixPath = 'file://'+filePath;
    
    return net.fetch(updatedPrefixPath);
  });

  ...
});
cursive dagger
#

make sure that the image is in the path name in the snippet

#

also, go check the styles tab in devtools and check if the url is correct

#

and btw it’s app.getPath("userData")

novel wigeon
cursive dagger
#

try checking the element and styles in devtools

novel wigeon
#

looks like the url is not being passed for some reason

#

Hmm, if I do this, I get this error in the console:

<div
        style={{backgroundImage: "url('file:///Users/xyz/Library/Application Support/ocheeflowxp/bg-images/sunset_wallpaper.jpg')"}} 
        className='bg-[image:var(--image-url)] bg-no-repeat bg-cover bg-center min-w-[3840px] min-h-screen scale-110'
      >
#

:5173/#/cover:1 Not allowed to load local resource: file:///Users/xyz/Library/Application%20Support/ocheeflowxp/bg-images/sunset_wallpaper.jpg

#

Okay, progress, so this works:

<div
        style={{backgroundImage: "url('ocheeflow:///Users/xyz/Library/Application Support/ocheeflowxp/bg-images/sunset_wallpaper.jpg')"}} 
        className='bg-[image:var(--image-url)] bg-no-repeat bg-cover bg-center min-w-[3840px] min-h-screen scale-110'
      >