How to avoid grunt build not loading bootstrap glyphicons using Jhipster

Problem:

Once you deploy your application in production mode, the browser can not load neither bootstrap icons nor pictures (like your logo picture) included into the css files.

This is what I was getting after deploying my test application in production mode:
jhipsterBug
 

Solution:

1) Copy the bootstrap fonts:
 
First of all, you need to copy the bootstrap fonts which includes the bootstrap glyphicons into the dist/assets/fonts folder. Therefore, you have to add it into your copy task, inside the gruntfile, like:
{expand: true, cwd: ‘src/main/webapp/bower_components/bootstrap/dist’, src: ‘fonts/*’, dest: ‘<%= yeoman.dist %>/assets/’}
Then your copy task should look like this:

copy: {
            dist: {
                files: [{
                    expand: true,
                    dot: true,
                    cwd: 'src/main/webapp',
                    dest: '<%= yeoman.dist %>',
                    src: [
                        '*.html',
                        'scripts/**/*.html',
                        'assets/images/**/*.{png,gif,webp}',
                        'assets/fonts/*'
                    ]
                }, {
                    expand: true,
                    cwd: '.tmp/assets/images',
                    dest: '<%= yeoman.dist %>/assets/images',
                    src: [
                        'generated/*'
                    ]
                },{
                    expand: true,
                    cwd: 'src/main/webapp/bower_components/bootstrap/dist',
                    src: 'fonts/*',
                    dest: '<%= yeoman.dist %>/assets/'
                }]
            },
...

2) Remove cssmin task
By default the Jhipster Gruntfile includes the cssmin task. Cssmin with root options replaces all relative paths by absolute paths. Let’s use an example to clarify this point. For instance, imagine that I have created a test application and then, I have generated a war packaged called test.war.
Then I have deployed my test application into my local tomcat server. Thus I can access my web application on localhost:8080/test.
Nevertheless, the logo picture (which is specify in the main.css file) is not loading because the browser is trying to get your picture from localhost:8080/assets/images/logo.png instead of using the current location which is localhost:8080/test/assets/images/logo.png.

There are different ways to remove your cssmin task, either you could remove the task or commented out. In this example, I have just commented out the whole task:

        //cssmin: {
        //    // By default, your `index.html` <!-- Usemin Block --> will take care of
        //    // minification. This option is pre-configured if you do not wish to use
        //    // Usemin blocks.
        //    // dist: {
        //    //     files: {
        //    //         '<%= yeoman.dist %>/styles/main.css': [
        //    //             '.tmp/styles/**/*.css',
        //    //             'styles/**/*.css'
        //    //         ]
        //    //     }
        //    // }
        //    options: {
        //        root: 'src/main/webapp' // Replace relative paths for static resources with absolute path
        //    }
        //},

3) Remove the useminAutoprefixer

If you have followed the steps 1 and 2, and you have reloaded you application, now you should be able to see the bootstrap glyphicons and the logo picture. Nevertheless, there are still styles that has not been loaded properly.

Jhipster by default use an autoprefixer for the css minification task, so you will need to remove it.

There are two points in the gruntfile which refers to the auntoprefixer, one is on top:

// usemin custom step
//var useminAutoprefixer = {
//    name: 'autoprefixer',
//    createConfig: require('grunt-usemin/lib/config/cssmin').createConfig // Reuse cssmins createConfig
//};


And the other one is inside the useminPrepare task. As you can see I have commented out the css: [‘cssmin’, useminAutoprefixer] line and replace it by css: [‘cssmin’]
useminPrepare: {
            html: 'src/main/webapp/**/*.html',
            options: {
                dest: '<%= yeoman.dist %>',
                flow: {
                    html: {
                        steps: {
                            js: ['concat', 'uglifyjs'],
                            //css: ['cssmin', useminAutoprefixer] // Let cssmin concat files so it corrects relative paths to fonts and images
                            css: ['cssmin'] 
                        },
                            post: {}
                        }
                    }
            }
        },

That’s it! If you have followed the 3 steps now your applications should look like this:

jHipsterSolved

You may also like...

3 Responses

  1. Marty Springer says:

    Thank you! I’ve been struggling with this.

  2. Sergio says:

    Thanks for the post.

    Might need to comment autoprefixer in the grunt task list, did not work without this change for me.

  1. January 29, 2018

    […] For more information, check this post which includes a deeply explanation: [http://ignaciosuay.com/how-to-avoid-grunt-build-not-loading-bootstrap-glyphicons-using-jhipster/ […]

Leave a Reply

Your email address will not be published. Required fields are marked *