Search code examples
gruntjsnpmgrunt-contrib-connect

grunt-contrib-connect: grunt server stops when browser opens


I have Grunt file like this:

module.exports = function(grunt){

   grunt.loadNpmTasks('grunt-open');
   grunt.loadNpmTasks('grunt-contrib-connect');

   grunt.initConfig({
      pkg: grunt.file.readJSON('package.json'),

      connect: {
            dev: {
                options: {
                    port: 8999,
                    livereload: 35729,
                    hostname: 'localhost'
                },
                livereload: {
                    options: {
                        open: {
                             target: 'http://localhost:8999'
                        },
                        base: [
                            'src/main'
                        ]
                    }
                 }
             }

           },

            open: {
                dev: {
                   path: 'http://localhost:<%= connect.dev.options.port %>'
                }
            }
        });

        grunt.registerTask('default', ['connect:dev', 'open:dev']);
}

But my problem is, whenever the browseropens, the server stops prior to that.

Please help. Thanks in advance.


Solution

  • You can add a watch task that will watch for changed files and keep the server up:

    watch: {
            options: {
                nospawn: true
            },
            livereload: {
                options: {
                    livereload: LIVERELOAD_PORT
                },
                files: [
                    'src/main/*.js',
                    'src/main/*.html'
                ]
            }
    },
    

    and then change the task to be:

    grunt.registerTask('default', ['connect:dev', 'open:dev', 'watch']);