Skip to content

Images in css files do not get assetsPrefix in production build #37654

@marishkaSunrise

Description

@marishkaSunrise

Preliminary Checks

Description

When add static background images to css files, Gatsby 5 doesn't add assetPrefix (added to gatsby-config.js) to them in production build. It makes it impossible to use cdn, because not all images can be added to js files as img HTML tag or imported to components where assetPrefix is being applied correctly (with withPrefix function).

Reproduction Link

https://codesandbox.io/s/wandering-rgb-hx2pp4

Steps to Reproduce

Create a css file
.container { background-image: url("/logo.svg") }

Add container class name to html

Add to gatsby-config.js:
assetPrefix: https://cdn.example.com

Build the app for production, see pages source code , inlined styles do not have assetsPrefix added to image paths.

Expected Result

In prod output inlined css styles should look like

<style>.container{backgrround-image:url('https://cdn.example.com/logo.svg')}</style>

Actual Result

<style>.container{backgrround-image:url('/logo.svg')}</style>

Environment

System:
    OS: macOS 13.1
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.12.1 - ~/.nvm/versions/node/v18.12.1/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v18.12.1/bin/yarn
    npm: 8.19.2 - ~/.nvm/versions/node/v18.12.1/bin/npm
  Browsers:
    Chrome: 109.0.5414.119
    Firefox: 108.0.2
    Safari: 16.2
  npmPackages:
    gatsby: ^5.6.0 => 5.6.0
    gatsby-link: ^5.6.0 => 5.6.0
    gatsby-page-utils: ^3.6.0 => 3.6.0
    gatsby-plugin-canonical-urls: ^5.6.0 => 5.6.0
    gatsby-plugin-env-variables: ^2.3.0 => 2.3.0
    gatsby-plugin-eslint: ^4.0.3 => 4.0.3
    gatsby-plugin-manifest: ^5.6.0 => 5.6.0
    gatsby-plugin-preconnect: ^1.4.0 => 1.4.0
    gatsby-plugin-purgecss: ^6.2.1 => 6.2.1
    gatsby-plugin-root-import: ^2.0.9 => 2.0.9
    gatsby-plugin-sass: ^6.4.0 => 6.6.0
    gatsby-plugin-sitemap: ^6.4.0 => 6.6.0
    gatsby-plugin-split-css: ^2.0.3 => 2.0.3
    gatsby-plugin-styled-components: ^6.4.0 => 6.6.0
    gatsby-source-filesystem: ^5.6.0 => 5.6.0
  npmGlobalPackages:
    gatsby-cli: 5.6.0

Config Flags

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: mediaRelated to gatsby-plugin-image, or general image/media processing topicstopic: webpack/babelWebpack or babeltype: bugAn issue or pull request relating to a bug in Gatsby

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions