-
Notifications
You must be signed in to change notification settings - Fork 27.2k
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
Extracted static stylesheet file into header #2534
Comments
Nice work! I have one question about non-production env. In your code,
|
We don't use styles in JS components. Every CSS file is separated to own file in directory buttons.css
inputs.css
lightgallery.css
.... other copies naming by independent components or composites: Contact.css
Search.css
Tabs.css
.... I think domain functionalities and design should be separated. Another advantage is we can use CSS variables, no need to use SASS. @import "_settings";
@import "global.css";
@import "grid.css";
@import "buttons.css"; |
I'm also in need of this setup and this is the best solution so far. Below is my
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const autoprefixer = require('autoprefixer');
module.exports = {
webpack: (config, { dev }) => {
config.module.rules.push({
test: /\.s?css$/,
loader: 'emit-file-loader',
options: {
name: 'dist/[path][name].[ext]',
},
});
if (!dev) {
config.module.rules.push({
test: /\.scss$/,
use: ExtractTextPlugin.extract({
use: [
{
loader: 'css-loader',
options: {
importLoaders: 2,
modules: false,
url: true,
sourceMap: false,
minimize: true,
localIdentName: false
? '[name]-[local]-[hash:base64:5]'
: '[hash:base64:5]',
},
},
{
loader: 'postcss-loader',
options: {
sourceMap: true,
plugins: () => [
autoprefixer(),
],
},
},
{
loader: 'sass-loader',
options: {
sourceMap: true,
includePaths: [
path.resolve(__dirname, 'scss'),
path.resolve(__dirname, 'pages'),
],
},
},
],
}),
});
config.plugins.push(new ExtractTextPlugin('app.css'));
} else {
config.module.rules.push({
test: /\.scss$/,
use: [
{ loader: 'raw-loader' },
{
loader: 'postcss-loader',
options: {
sourceMap: 'inline',
plugins: () => [
autoprefixer(),
],
},
},
{
loader: 'sass-loader',
options: { sourceMap: true },
},
],
});
}
return config;
},
}; |
Hi @radeno. This all looks great and is very close to something i am working on. However, in your non-production env, it looks like you don't have hot-reloading. is that correct? If so, I have an approach that will be able to have hot loading in dev and have a compiled css file in prod (i got that bit from your code above). I am thinking of doing a PR for this in the examples as it seems that there are many people that are asking for this kind of solution. |
@longlivedigital what do you mean by hot-reloading because next.js has hot-reloader in core. |
@longlivedigital any progress here? |
hai! , this is working thanks! , is there a way to import the app.css without copying it first to static directory ? |
I dont see any way, because default serving routes can't be redefined: |
@sheerun very very nice, great work! Hope next.js will implement native CSS bundling and packing. |
I can't get NEXT_DATA on server side.. any help?
this.props.__NEXT_DATA__.buildStats['app.js'].hash |
It is available only on custom document (in pages/_document.js), please see the README |
@sheerun Thanks a lot! |
Many issues are trying to solve extracting CSS file as separate link for production env. Eg: #1479 #1396 #1615
There is another solution what is works, and is quite elegant. (maybe isn't 😄 )
This solution works for Next v3 beta and Docker. My source directory is
src
and build directory is one level up.next
. We are using separated style files in /style directory with index.css where are all css files imported.Required Webpack plugins and loaders
next.config.js
File is emitted in build directory and then is processed with css-loader and postcss-loader. After processing is extracted next to app.js file.
Do not use babel-loader for global as in this example: https://github.com/zeit/next.js/tree/v3-beta/examples/with-global-stylesheet It is not neccessary and doesn't work well with postcss-calc.
Docker
In dockerfile, copy generated app.css to static directory
RUN mkdir src/static/styles && cp .next/app.css src/static/styles/app.css
Header link for production, style for development
pages/_document.js
we also used React-Helmet, so this example isLink to app.css is appended with querystring with same hash as is used in app.js path for disabling cache after redeploy.
What we can do better?
Next Server define route for production
app.js
file (https://github.com/zeit/next.js/blob/v3-beta/server/index.js#L151). This example should be more elegant if we can define route for app.cssThen this could be automated or at least we should have better links for app.css file. Ex:
The text was updated successfully, but these errors were encountered: