1

I followed this tutorial for creating a Spring-boot+ ReactJs app: https://spring.io/guides/tutorials/react-and-spring-data-rest/

The application starts fine with ./mvnw spring-boot: run, and I have an NPM script "watch": "webpack --watch -d" that runs the following configuration

var path = require('path');

module.exports = {
    entry: './src/main/js/app.js',
    devtool: 'sourcemaps',
    cache: true,
    mode: 'development',
    output: {
        path: __dirname,
        filename: './src/main/resources/static/built/bundle.js'
    },
    module: {
        rules: [
            {
                test: path.join(__dirname, '.'),
                exclude: /(node_modules)/,
                use: [{
                    loader: 'babel-loader',
                    options: {
                        presets: ["@babel/preset-env", "@babel/preset-react"]
                    }
                }]
            }
        ]
    }
};

app.js gets compiled into bundle.js as expected but if I don't restart the server with ./mvnw spring-boot:run when I refresh the page I can't see the changes.

I might be missing something, is maybe spring-boot not using my bundle.js file but a copy of some sort?

neviovalsa
  • 127
  • 2
  • 11

2 Answers2

0

I can guess embedded tomcat does not hot reload static files. You can try some of these Refreshing static content with Spring MVC and Boot

But will also share my solution, which is using webpack-dev-server to hot reload and web development in general.

const path = require('path');
...
devServer: {
    contentBase: path.join(__dirname, 'src'),
    publicPath: '/build',
    port: 8000,
    proxy: {
        "**": "http://localhost:8080"
    }
},

and then run webpack-dev-server command to run it. It compiles you app on the fly and your changes are reflected immediately, definitely give it a try.

yvoytovych
  • 871
  • 4
  • 12
  • I tried with `webpack-dev-server`, it compiles , but still doesn't reflect the changes. I also tried with spring's hot reload that requires an additional dependency called `spring-boot-devtools`, which supposedly should do exactly what I want, but it's still not working. I'll let you know if I find a working solution. Thanks anyway – neviovalsa Apr 11 '19 at 12:58
0

I got the same question and solve it today.

The key is to run npm run watch and mvn spring-boot:run at the same time.

make sure you have

    "scripts": {
        "watch": "webpack --watch -d --output .//classes/static/built/bundle.js"
    }

in your package.json and

    <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-devtools</artifactId>
         <optional>true</optional>
    </dependency>   

in your pom.xml