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.

SCSS absolute image urls are not transformed

See original GitHub issue

I’m really having a hard time making this plugin work with my SASS files, so to make sure it’s not my own complex configuration that is in the way, I created a new project from scratch just to test the plugin and try and make it work with CSS url.

The simple project

// pages/styles.scss
.test {
  background-image: url('/static/test.jpg'); // this is not transformed
}
// pages/index.js
import React from 'react';
import './styles.scss';

export default () => <div className="test">Welcome to next.js!</div>
// next.config.js
const withSass = require('@zeit/next-sass');
const withOptimizedImages = require('next-optimized-images');

module.exports = withOptimizedImages(withSass({
  assetPrefix: 'http://localhost:3000/',
}));

And the package.json

{
  "dependencies": {
    "@zeit/next-sass": "^1.0.1",
    "next": "^8.0.0",
    "next-optimized-images": "^2.3.3",
    "node-sass": "^4.11.0",
    "react": "^16.8.1",
    "react-dom": "^16.8.1"
  },
  "scripts": {
    "dev": "next",
    "build": "next build",
    "start": "next start"
  }
}

Note that our other project is on Next v7 (if that changes anything).

Expected behaviour

Since I’m using the assetPrefix Next config option, I’m hoping to see my image URL within the compiled CSS to change to something like this: http://localhost:3000/_next/static/test-hashHere.jpg.

Current behaviour

It’s not changing at all. I’m seeing the image since the assetPrefix is the same as my default dev server and Next is correctly parsing the image since if I change the path, it fails to compile.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

3reactions
emilebercommented, Feb 12, 2019

To make it work with absolute paths using aliases in our SCSS, I needed to add the aliases configuration to the webpack configuration, something like that:

const path = require('path')

module.exports = {
  webpack (config, options) {
    config.resolve.alias['static'] = path.join(__dirname, 'static')
    return config
  }
}

So it would work like this in our SCSS:

.test {
  background-image: url('~static/test.jpg'); // notice the tilde
}
0reactions
emilebercommented, Feb 12, 2019

In my example above, I mainly changed my styles.scss to:

.test {
  background-image: url('../static/test.jpg');
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't use relative paths in url() in scss files #12797 - GitHub
I would like to be able to use the relative file path in the scss file. I know this is solved by using...
Read more >
url() does not refer to correct file when using relative paths in ...
Considering a CSS import resolves the URL files relative to the file they are found in and SCSS does not adjust the URL...
Read more >
url() - CSS: Cascading Style Sheets - MDN Web Docs
The url() CSS function is used to include a file. The parameter is an absolute URL, a relative URL, a blob URL, or...
Read more >
'Combine CSS Files' breaking image URLs - WordPress.org
1. When we combine the CSS, we check for full URLs and if we find such, we leave them as is. 2. When...
Read more >
Absolute / domain-relative path for asset URLs in CSS - bud
However, the asset url(...) s inside the CSS are still relative to their stylesheet (e.g. url(images/water-sand.9a11c3.jpg) . This is completely valid and works ......
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