question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] in styled jsx loaders not apply to images

See original GitHub issue

i have files /pages/_App.js /res/images/image.jpg

export default function App() {

  // language=Stylus
  return <style jsx global>{ `
	body {
		background-image: url("../res/images/image.jpg");
	}
  ` }</style>
}

Problem

url("../res/images/image.jpg") will not replace to /_next/static/images/lol-c8bbed083d312e8f1cccb6069d6f7919.png. styled jsx not applied webpack loaders, and not create optimized next.js assets

Temporary solution

export default function App() {

  // language=Stylus
  return <style jsx global>{ `
	body {
		background-image: url(${ require("../res/images/image.jpg") });
	}
  ` }</style>
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
MaxmaxmaximusGitHubcommented, Apr 24, 2020

temporary solution:

import css from 'styled-jsx/css'
import icon from 'res/images/icon.svg'

export default function Box() {
  return <div className='box'>
    <style jsx>{ style }</style>
  </div>
}

// language=Css
const style = css`
  .input-panel {
    background-image(${ icon })
  }
`
2reactions
MaxmaxmaximusGitHubcommented, Apr 24, 2020

@jamesmosier @timneutkens

if i use webepack base 64 loader, for svg icons, then render component with jsx styles generate 10kb+ style text? every render iteration? if i use 10 png icons by 10kb, i create 100kb style text by every render?

next js cool framework, and your recommendations are wonderful =)

or place the image in the public folder as mentioned in the docs

the images in this folder are source, and do not go through webpack processing and are not suitable for production. hashes are not added to their names, they are not stuck together into single sprites, etc. These are raw files not processed by webpack. such files are called sources.

it is unacceptable to use raw files directly; all assets must always be imported through the webpack system.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[BUG] in styled jsx loaders not apply to images #12158 - GitHub
i have files /pages/_App.js /res/images/image.jpg export default function App() ... [BUG] in styled jsx loaders not apply to images #12158.
Read more >
React.js styled-components importing images and using them ...
You should import images in the following manner (assuming that you have webpack configured for importing media assets).
Read more >
css-loader | webpack - JS.ORG
webpack is a module bundler. Its main purpose is to bundle JavaScript files for usage in a browser, yet it is also capable...
Read more >
styled-jsx - npm
Full CSS support for JSX without compromises. Latest version: 5.1.1, ... Start using styled-jsx in your project by running `npm i styled-jsx`.
Read more >
Next.js image optimization techniques | Uploadcare Blog
UPD: Uploadcare now has a Next.js Image Loader and Component. ... it's possible to import an image as a React component or from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found