Skip to content

Error while getting response from load balancer

1

I have deployed react application on Application load balancer and there are two ec2 instance in a target group,the issue is that the web page loads properly after multiple refresh or else gives blank screen because of the error "Uncaught SyntaxError: Unexpected token '<'". in the console and only loads the static index.html.

The application loads by directly accessing the instance without any error and even after multiple refreses. The issues get encountered in load balancer only. I have also tried changing the health check configuration but didn't worked and the checkups are also healthy. I also tried deploying the application with classic load balancer but issues were faced.

What might be the possible issue because it successfully responds sometimes and mostly reponds with errors?

  • The issue is most likely on the application side. Using Chrome developer tools, can you please capture the Request and Responses when the page loads and when you observe this error? This will help us better understand the issue.

2 Answers
0

This error may be related to React application also. Would recommend to use your browser dev tools or any available React Developer Tools to debug the same. This will provide some insights where things are failing.

AWS
EXPERT
answered 3 years ago
  • There is no issue in the application i have checked and also it works fine by directly accessing the instance. The link for load balancer:http://projectbalancer-1437849666.ap-south-1.elb.amazonaws.com/ You can have a look,and let me know what is causing the load balancer to show this error. Inorder to access the page you have to refresh multiple times and i have also mentioned to which server it is accessing either 1 or 2

0

This is an unfortunate issue with load balancers and static builds using hashed filenames.

Pre deploy you would have:
(old) index.html > fetches script-old.js

When you deploy a new build, it would have:
(new) index.html > fetches script-new.js

So during deployment, the old task can be running alongside the new task.

This is the problem; there’s no guarantee when either index.html loads, which task it will hit to retrieve the script file it needs.

The (new) index.html may hit the old task requesting script-new.js. But that doesn’t exist on the old task.

This is why you get the “Unexpected token” error. Because what’s returned is expected to be js, but ends up being html. Usually index.html is returned if a static file isn’t found, using express for example.

Hence “<“ is a “SyntaxError” for js. That’s the first character from the html file.

So, what’s the solution? There are a few, but easiest is to enable “Stickiness” on the Target Group associated with the front-end.

Similar problem / answer: https://stackoverflow.com/questions/73599420/how-to-update-react-apps-behind-an-aws-load-balancer-without-404-due-to-changing

answered 9 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.