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
How can I externalize the css to a separate file for browsers to cache? #1479
How can I externalize the css to a separate file for browsers to cache? #1479
Comments
add a plain old |
You might find the example in nextjs-starter helpful. In index.js it uses express to add a route like this:
The header.js component of the web page then has something like this:
Of course you don't need express just do this but it just happens to as the rest of the example uses it. The actual example is a bit more sophisticated as it also does inlining and live reloading in development mode and only adds it as a link in production, but you can see how that's done (with webpack config in next.config.js) in the example. |
I am using a local scoped styled-components |
You could adapt that same approach and use next/head to inject the link route to the stylesheet in your page (and have CSS files that correspond to your pages). e.g. "pages/about" and "css/about.scss" And inject a link like If you wanted be clever about it you could have this done in a layout file which takes a props that has the CSS file(s) you want to load for that page. |
I've got a similar issue. I'm trying to port our relatively simple React app to next.js 2.0 and it's going really well so far, but I got stuck on CSS. Currently we bundle all CSS (Sass actually) together and use Is there a working example how to do something like this? |
Yes, the nextjs-starter project does that. |
@iaincollins Thanks for the tip! I'll check it out. |
@iaincollins In nextjs-starter you're using version from package.json file for versioning assets. Do you know if there's any way to use hashes generated by webpack, so that I don't have to manually bump version in package.json every time I change an asset? |
@szimek No but that's a great question and a better way of doing it. If you create a BUILD_ID file (in .next/BUILD_ID) then you could read from and use that, but I'm not sure if/how the current BUILD_ID is exposed if you don't explicitly set one. It would be great if anyone on the nextjs team can suggest how to access that hash (either at run time or by reading from a path). I think exposing those hashes was discussed fairly recently in an open issue but not sure what the resolution to it was. If I go digging and find out anything will update here (and the demo). UPDATE: I guess exposing a var with a SHA sum of the compiled CSS could work just as well; may consider doing that if it's easier. Will explore hooking into the existing build process first though. |
@timneutkens Is there any official way to do load external CSS that works properly with browser caching (i.e. will output |
There is another way than express route handle. More in #2534 |
@Ridermansb css imports is going to be supported in next v5 |
The default behavior is to inject all styles to the HTML in a style tag.
How can I make sure all styles are cached by browsers separately when I update my app?
Is there an example using extract-text-webpack-plugin in a next.config.js or is there another recommended way to achieve that?
The text was updated successfully, but these errors were encountered: