-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Relative URLs to assets no longer work in CSS now that CSS no longer gets inlined #4645
Comments
Do you have any custom webpack.config? |
@igor-dv Ah, yes, sorry - I'm using TypeScript. The config in the case of the working images:
and the config in the case where images do not load:
The difference between the two is that the loader changed from ts-loader to awesome-typescript-loader. |
So the difference in the |
Yes, as far as the Webpack config is concerned. Though i don't think they do anything with image imports, especially when done from CSS? |
Did you try to use SB v4 with the |
Well, I wanted to, but unfortunately I couldn't get it to work - I ran into this error message:
Since the official docs also use |
BTW, how does your tsconfig look like? Also, if you could share a public reproduction, that'd be great. |
Haha, I was afraid so :P The
I don't have a minimal reproduction unfortunately, but the project this occurs on luckily is open source: with Storybook 3, with Storybook 4. (Note that there are several projects in that repo - the one with Storybook is located in |
You can exclude stories from |
@igor-dv Hehe, that's what I was doing already - I just mashed them together here for easier display :) Setting |
Looks like I need to clone the project 😴 |
Ah don't bother, that's far too much effort! I'll probably be diving into this again tomorrow; who knows, I might get some fresh insights :) |
I see that you've migrated to react-scripts as well. We have a feature that integrates styling from the CRA2. CC: @chadfawcett , could it be somehow related to this? |
I believe this is because you're switching to CRA. I believe the file paths need to be relative. So change https://facebook.github.io/create-react-app/docs/adding-images-fonts-and-files |
@chadfawcett Actually, it used to be like that - I just changed it to see whether that fixes it. I'll try changing it back, but I think Note that it does work locally (i.e. unbuilt) for me as well, because the CSS is still inline at that point, so the relative import path still works. It just doesn't work in the built version because the CSS file is then in a subfolder, and imports are resolved by the browser relative to that. Edit: Just tried it with a Thanks for your help both, though, you're really going above and beyond :) |
Sorry @Vinnl, I missed the fact that this was specific to built version. Looks like it's a bug in the storybook cra webpack preset. I'll try to get a PR in tonight. |
That'd be fantastic @chadfawcett. If you have any pointers I'd be happy to take a shot at it myself, if need be :) |
Some more observations: CRA successfully turns it into an absolute URL, as can be seen here. However, it knows how to do so because the developer configures at what location the app will be deployed. Given that that location is different for the built Storybook application, I'm not sure how to deal with that other than providing a similar configuration option for Storybook itself. Though perhaps an alternative is to always inline images as data URLs, given that data use is probably (?) less of an issue for Storybooks. Or, alternatively, not to extract the CSS into separate files at all, like in Storybook 3. Anyway, I'm not sure if that is desirable from a Storybook point of view, so that's probably best for you to judge. |
@Vinnl Apologies that this took longer than expected. I got caught up with some testing issues. Take a look at #4695. Would you be able to clone my branch and test it with your repo? I've already done so, but it would be good if you could confirm the fix resolves your issue. Here's how to include the local build in your project https://github.com/storybooks/storybook/blob/master/CONTRIBUTING.md#working-with-your-own-app |
No problem @chadfawcett, I'm already indebted to you for even trying to fix it, let alone actually doing so :) Anyway, I saw the PR and was already trying it out - I've just confirmed that it does, indeed, fix the issue :) |
Just upgraded to 4.0.4, and it works flawlessly. Thanks! |
I am getting exactly same issue with css is trying to look for a font file in but the font is at following url: |
I am also facing same issue |
Same "5.3.9", |
@mrmckeb did something change in the CRA preset that might affect this? |
This looks like a conflict between Storybook and CRA's configurations, which is odd. I can take a look at this. Has anyone got a reproduction they can share with me? Thanks! |
Same for the version 5.3.13 |
Hello.
Hopefully it could help you. |
Hello. This is the most minimal repro I could come up with. Steps:
|
in my project - i was able to remedy this by adding the not sure if this helps anyone^ seems like the relevant lines in CRA look something like this (https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/config/webpack.config.js): // common function to get style loaders
const getStyleLoaders = (cssOptions, preProcessor) => {
const loaders = [
isEnvDevelopment && require.resolve('style-loader'),
isEnvProduction && {
loader: MiniCssExtractPlugin.loader,
// css is located in `static/css`, use '../../' to locate index.html folder
// in production `paths.publicUrlOrPath` can be a relative path
options: paths.publicUrlOrPath.startsWith('.')
? { publicPath: '../../' }
: {},
}, and public path is set below: // webpack uses `publicPath` to determine where the app is being served from.
// It requires a trailing slash, or the file assets will get an incorrect path.
// We inferred the "public path" (such as / or /my-project) from homepage.
publicPath: paths.publicUrlOrPath, |
@mrmckeb any chance you can look at @cherouvim 's repro? |
@cherouvim, I downloaded your project and ran it - and I see "Rumba" loading as expected. You also have a I then migrated the project to use the new preset for CRA, and everything was still working correctly. Can you please provide some more info? Thanks! |
@YDundie, I'd like to find the root cause of this and fix it in the preset. Are you able to share an example of what isn't working for you? Even just code snippets here. |
@mrmckeb it does work on |
Thanks @cherouvim, I didn't understand that it was build only. Found and hopefully fixed. Thanks for the solution @maniax89! |
@maniax89's solution to add |
This is still happening on non-CRA apps. In CSS.
After Storybook.
|
For anybody still having this issue down the road on a non-CRA app, please reference this SO thread The key of this is in specifying { url: false } in the CSS Module rule of your webpack config. Here is the specific configuration I used for my CSS Module rule: config.module.rules.push({
test: /\.module\.css$/,
use: [
require.resolve('style-loader'),
{
loader: require.resolve('css-loader'),
options: {
modules: {...}, // project-specific module naming conventions, config, etc.
url: false,
importLoaders: 1,
},
},
],
}) Hope this can save some time for anybody else! |
Describe the bug
I just upgraded to Storybook 4, and I think this issue is caused by that (it required a lot of other moving parts as well).
Basically, in my React component, I
import './Widget.css';
. Then inWidget.css
, I reference a background image located in the same folder as such:background-image: url(orcid_16x16.png);
.With Storybook 3, this just worked even using
build-storybook
: the image URL was updated tostatic/media/orcid_16x16.2dddb203.png
, and since the CSS was inline, this path was correct.Now with Storybook 4, however, the URL is still updated to that value, but because it is now located in a CSS file that itself located in
static/css
, the browser is now trying to find the image atstatic/css/static/media/
, and hence cannot find it.To Reproduce
Steps to reproduce the behavior:
Expected behavior
The image to also be present in Storybook 4 - presumably by making sure that URL imports are rewritten to be relative to the CSS file they are in.
Screenshots
In Storybook 3:
In Storybook 4:
System:
The text was updated successfully, but these errors were encountered: