react on rails monorepo deploy on Heroku, API calls not working right

41 Views Asked by At

I'm having an issue and I think I'm in a little over my head, so I'd like to know if what I'm attempting is doable or a lost cause. I'm trying to deploy a React on Rails API to Heroku from a monorepo on my github. I have a backend folder, a frontend folder, and at the root a package.json alongside a Gemfile and a Procfile. The Procfile starts a script that starts the Rails api.

Procfile:

api: backend/bin/start_server.sh
web: cd frontend && pwd && npm run dev -- --port $PORT

The backend/bin/start_server.sh in question:

echo "running $0"

cd $(dirname $0)
cd ..
echo "running in $(pwd)"

# Precompile assets for the Rails backend
bundle install

bundle exec rails db:migrate
bundle exec rails db:seed

bundle exec rails server -p $RAILS_PORT

Everything deploys fine. I can see the React as normal, and I can interact with the frontend perfectly. However, it gets complicated when I try to use the API. I have my routes setup with the proper controllers for my TestData. When I make the call to the API via your garden variety fetch "/api/v1/test_data" in my React front, I get an error on the web console that reads:

Error fetching articles: SyntaxError: Unexpected token '<', "<!doctype "... is not valid JSON
(anonymous) @ index.jsx:10

The call goes through, but returns some random HTML. I have Cors properly set up, the Heroku console says the Rails is running on Puma, etc., and worst of all it works fine on local. Is it a doomed pursuit? Am I missing something on Heroku? Is it just not possible at all?

Thank you in advance for your help, and let me know if I can add anything else that might be of use. Sorry if it's missing crucial info!

0

There are 0 best solutions below