Watch a live product demo 👀 See how Sanity powers richer commerce experiences

Does the URL Builder Add-on Only Work for image src=[]

16 replies
Last updated: Jul 28, 2020
I'm just wondering if this url builder add-on only works for <image src=[] or can it also be used to place the url inside a background image inline css?
Jul 28, 2020, 3:25 PM
It looks to me like you have forgot to wrap the url in
url()
?
Jul 28, 2020, 3:25 PM
ahdamnit!
Jul 28, 2020, 3:26 PM
i think your right
Jul 28, 2020, 3:26 PM
lol
Jul 28, 2020, 3:26 PM
even so it still doesnt workl
Jul 28, 2020, 3:31 PM
this is what i get:
url(options{project-id:5qjt9zfj;dataset:production;source{asset{_ref:image-3896734…-2250x1500-png;}}}with-options:function withOptions(options) { var baseUrl = options.baseUrl || this.options.baseUrl; var newOptions = { baseUrl:baseUrl }; for (var key in options) { if (options.hasOwnProperty(key)) { var specKey = rewriteSpecName(key); newOptions[specKey] = options[key]; } } return new ImageUrlBuilder(this,_extends({ baseUrl:baseUrl },newOptions)); };
Jul 28, 2020, 3:32 PM
and that is with the exact code I placed inside the src
Jul 28, 2020, 3:33 PM
that works fine
Jul 28, 2020, 3:33 PM
I was curious and did som quick testing, I got it working.
Don't know what your
imageUrl
&
buildImageObj
are returning, and can't say why src is working and not the inline css.
Jul 28, 2020, 3:46 PM
ok, cool. that's all i wanted to know. thank you
Jul 28, 2020, 3:47 PM
i'll figure it out
Jul 28, 2020, 3:47 PM
const builder = imageUrlBuilder(config);
const urlFor = (source: string) => builder.image(source);

const Image: React.FunctionComponent<Props> = ({source, alt}) => {
    const url = source?._id && urlFor(source._id).width(1152).auto('format').url();

    if (url === null) {
        return null;
    }
    
    return (
        <figure
            css={css`
                background-image: url(${url});
                width: 500px;
                height: 500px;
            `}
        />
    );

    // return <Img src={url} alt={alt} />;
};

Jul 28, 2020, 3:49 PM
Sorry for the ugly code, but just try to get the url as a string from one of your functions as I did I think you should got it working
Jul 28, 2020, 3:50 PM
great! thank you
Jul 28, 2020, 3:50 PM
i see what i was doing wrong. i wasn't calling the url() function at the end. although it didn't matter when building the image (for some reason) it did matter when applying it to the background image
Jul 28, 2020, 7:40 PM
thank you for your help!
Jul 28, 2020, 7:41 PM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Categorized in

Related answers

Get more help in the community Slack

TopicCategoriesFeaturedRepliesLast Updated
After adding the subtitle and running this code npm run graphql-deploy It does nothingSep 15, 2020
how to limit a reference to just one entry in Studio reference input side versus the default as-many-entries-as-you-fill-in-an-array...Sep 18, 2020
Is it possible to fetch more than one "_type" using GROQ?Nov 2, 2020
I want to add a view with the Structure builder (S.view.component) where I list similar documents based on the title. What...Sep 23, 2020
Is there a structure builder example where the format of each preview for the document list is modified?Feb 3, 2021
I have an array of references to a country schema type but it always just returns NULL values for meJan 30, 2021
Hi, I need help with a query for getting the url of an image asset. Here is what I've been trying, but I only get the _ref...Dec 1, 2020
Sanity UI looks brilliant :smiley: Is something like the current date picker possible at the moment? I’m not sure if anicon...Dec 21, 2020
Hey everyone. I have been coding and may have potentially accidentally deleted something. Does anyone know how to resolve...Dec 26, 2020
Hello everyone and happy new year :raised_hands::skin-tone-2:, I have a problem with outputting Portable Text :disappointed:...Jan 1, 2021

Related contributions

Clean Next.js + Sanity app
- Template

Official(made by Sanity team)

A clean example of Next.js with embedded Sanity ready for recomposition.

Cody Olsen
Go to Clean Next.js + Sanity app

Blog with Built-in Content Editing
- Template

Official(made by Sanity team)

A Sanity-powered blog with built-in content editing and instant previews.

Go to Blog with Built-in Content Editing