ALM ALM - 5 months ago 56
Node.js Question

ReactJS and Spring Data Rest caching issue possibly related to websockets

I seem to be having a weird caching problem.

I recently blew away my database. After I started to create some new

users
within my system I noticed that a old user showed up. This user has not been recreated and currently does not exist in the database.

I am not sure where to look for a caching issue, either from
Spring Data REST
or from
ReactJS
. Does anyone know where this might be from?

I have
web sockets
setup as well.

Steps:

Blow away db:
Created a new user
Web socket does the creation
I then use
reactjs
to make a call to get a page of users from
Spring Data Rest
this is where the weirdness happens.

Call to get the users

Headers:

Request URL:http://localhost:8081/api/users?size=4
Request Method:GET
Status Code:200 OK
Remote Address:[::1]:8081

Response Headers
view source
Content-Type:application/hal+json;charset=UTF-8
Date:Wed, 18 May 2016 22:25:29 GMT
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked

Request Headers
view source
Accept:application/hal+json
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Type:text/plain
Host:localhost:8081
Referer:http://localhost:8081/
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36
Query String Parameters
view source
view URL encoded
size:4

BODY:

{
"_embedded" : {
"users" : [ {
"firstName" : "Aaron",
"lastName" : "Magi",
"userName" : "amagi",
"description" : "coding up a storm",
"_links" : {
"self" : {
"href" : "http://localhost:8081/api/users/1"
},
"user" : {
"href" : "http://localhost:8081/api/users/1"
}
}
}, {
"firstName" : "john",
"lastName" : "smith",
"userName" : "jsmith",
"description" : "mr smith",
"_links" : {
"self" : {
"href" : "http://localhost:8081/api/users/3"
},
"user" : {
"href" : "http://localhost:8081/api/users/3"
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8081/api/users"
},
"profile" : {
"href" : "http://localhost:8081/api/profile/users"
}
},
"page" : {
"size" : 4,
"totalElements" : 2,
"totalPages" : 1,
"number" : 0
}
}


Internally I then make a call to the endpoint for the user details (http://localhost:8081/api/users/3). At this point it returns the old user details not the new details

Headers:

Request URL:http://localhost:8081/api/users/3
Request Method:GET
Status Code:304 Not Modified
Remote Address:[::1]:8081

Response Headers
view source
Date:Wed, 18 May 2016 22:25:29 GMT
ETag:"0"
Server:Apache-Coyote/1.1

Request Headers
view source
Accept:application/hal+json
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Type:text/plain
Host:localhost:8081
If-None-Match:"0"
Referer:http://localhost:8081/
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36


BODY

{
"firstName" : "Stephen",
"lastName" : "Mann",
"userName" : "smann",
"description" : "Wizard",
"_links" : {
"self" : {
"href" : "http://localhost:8081/api/users/3"
},
"user" : {
"href" : "http://localhost:8081/api/users/3"
}
}
}


webpack.config

var path = require('path');

var node_dir = __dirname + '/node_modules';

module.exports = {
entry: './app.js',
devtool: 'sourcemaps',
cache: false,
debug: true,
resolve: {
alias: {
'stompjs': node_dir + '/stompjs/lib/stomp.js',
}
},
output: {
path: __dirname,
filename: './built/bundle.js'
},
module: {
loaders: [
{
test: path.join(__dirname, '.'),
exclude: /(node_modules)/,
loader: 'babel-loader'
// TODO remove for production
//loaders: ['react-hot','babel-loader']
}
]
}
};


Application.properties

# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

# Show or not log for each sql query
spring.jpa.show-sql = true

# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update

# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager)

# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

# Web server
spring.data.rest.base-path=/api
server.port=8081

# TODO FOR DEBUG ONLY
spring.thymeleaf.cache=false
spring.freemarker.cache=false
spring.groovy.template.cache=false
spring.velocity.cache=false
spring.mustache.cache=false
server.session.persistent=true
spring.h2.console.enabled=true
spring.resources.cache-period=0


Question

Where is the caching happening? I can continue to create users and have them show up incorrectly with the old data. Is there a way to correctly turn this off for your application?

I noticed while posting this question that I had cache:true in In my webpack.config.js. But even setting this to false did not solve the issue. After reading it sounds like this is to cache components not data.

Thank you

ALM ALM
Answer

It turns out that the caching was a IE related problem. After further reading I determined I needed to setup the cache-control

'Pragma': 'no-cache', 

'Expires': '-1', 

'cache-control': 'no-cache'
Comments