Hey guys I am building a React app that is either:...
# suitescript
s
Hey guys I am building a React app that is either: • Deployed as a static bundle inside a NetSuite Suitelet, or • Running locally during development. My goal is to fetch data from a NetSuite backend (Suitelet or RESTlet) using AJAX/fetch from the React frontend. However, I keep running into CORS errors when making these requests, both from my local React dev server and from the deployed Suitelet version. Has anyone faced similar issues? Any guidance would be greatly appreciated!
m
Typically you would run into CORS issues when developing locally but trying to access real data from NetSuite. You can avoid that by using dummy data for local development but have your API automatically switch when you deploy to production. Here's my latest React Suitelet Demo. I haven't done a complete writeup on it yet, but here are some highlights around your question. https://github.com/mdrobbins/netsuite-react-demo • For local development, it uses JSON Server to provide dummy data in the form of a JSON file in the
data/db.json
file. I also have artificial delays here to make slow it down a bit. No CORS issues here. • For production, the Suitelet is in the same domain as the API RESTlet so no CORS issues here either. • There are two API files and it uses the correct one depending on if it's a production build or not. There's also a single NPM command to build and deploy the JS, CSS, Suitelet, and RESTlet (script records and files) using SDF to any environment. The README is not done yet, but it provides more details.
thankyou 1
❤️ 1
s
Hey @Mike Robbins thank you so much for this! I’ll take a look into the code to understand better how you’re calling from react to the backend.
m
There's an
api
folder which contains
api.dev.js
and
api.prod.js
depending on the build.
1
👀 1
s
This is really helpful thank you :)