LocalStorage is cleared after running yarn build - javascript

I have a React app and I am saving some simple data to LocalStorage. All works well across refreshes, etc.
But when I run yarn build to refresh the built web after pushing a change, this will clear somehow the local storage.
I have a server setup with nginx and a reverse proxy, my web app being served through pm2.
Is this something expected to happen? Why does it happen and can it be avoided?
Thanks!

Actually when you use yarn to run your code on browser it basically create a fresh session on browser on every refresh. therefore you loosing your data.

Thanks to #Aslam_khan reply I found the solution:
Use npm run build instead of yarn build if you want to prevent LocalStorage to be cleared up on every new build.

Related

React JS forcing js files from server only on new build

I have this React JS application to which critical patches are being pushed every now and then and I cannot ask my users to do a hard refresh every time. The current method I'm using to bust the cache and reload from the server is I build the app and then manually go to the index.html and append "?v=number". Also I see several chunks of js being generated, I'm assuming all of them are cached too.
Is there a more elegant solution to automate this process and get the js files from server ONLY when there is a new build?
I'm using react-scripts to build my project.
Quoting from this article:
https://dev.to/flexdinesh/cache-busting-a-react-app-22lk
TL;DR - SemVer your app and generate a meta.json file on each build
that won't be cached by the browser. Invalidate cache and hard reload
the app when there's a version mismatch.
And you can use this npm package to manage your meta.json:
https://www.npmjs.com/package/react-clear-cache

How to redeploy my github project in aws to see the changes?

I recently deployed my GitHub project in aws using Amazon Linux 2 AMI.
I installed npm, MongoDB, node and cloned the Github repo into the new instance that I created by sshing into it.
I am running the server with the forever package of npm.
Now, I made some changes in the code and pushed it to my Github repo but it's not being reflected in my project when I try to access it from outside.
So, how to redeploy it so that I see my changes when I access it from outside world?
I found this video which shows how to redeploy but it's not feasible in my case to do it.
Now, I made some changes in the code and pushed it to my Github repo but it's not being reflected in my project
Pushing to GitHub is one step.
But you still need to ssh to your execution environment (aws), and pull from GitHub in order to get the latest.
Then your npm application would have a chance to display your changes.
As an alternative to forever restart, you also can use PM2, as recommended here.
Another option would be to add a GitHub Action, like one of the deployment actions, in order to automate that step.
AWS proposes dedicated GitHub actions.
Full example: "Github Actions for web apps" from Luke Boyle.
From what I'm able to figure out you need to do the following steps:
Do a git pull after logging into your server via ssh.
cd toTheFolder where your git repo is located
git pull origin yourBranchName
forever restart or forever restartall should restart your server and your changes should reflect there.
And as VonC suggested you should go for PM2 instead but for now you can continue with forever. PM2 is very similar to forever but with a lot more features available.

Heroku / Node - how to add git commit on server

I'm learning Node and Git and I have a Heroku app that is reading and writing to a local file on the server (a very simple JSON database).
If I add the file to my gitignore locally, it disappears from my Heroku app and causes the app to error. But if I don't add it to my gitignore, it overwrites the latest version (on the server) with an old one I have locally.
Obviously the issue is because the changes on the server file aren't being committed. However, I don't know how to do that remotely, or if it's even possible. I can run heroku git:clone locally, but I can't run heroku:git add.
How do I handle this?
Generally, you should not commit a file that will be modified by the server.
It seems not a good idea because, as you said, this file will be overwrited by next push.
Usually you do not want to commit from your deployment branch, so it is not a good idea either to use git from server (and I doubt you can with Heroku).
Instead you could make your app check if the file exists and if not create that file on server.
That will work in a dedicated server you manage yourself, but Heroku doesn't work the same. Each push you make to your Heroku repository will in fact bundle your application before launching it on a dyno, and this process overwrite all the file, including your database JSON file, which will be no more persistent.
So I think you have no choice than switch to another storage method, for exemple subscribe a free Heroku postgreSQL plan or another database you prefer.

Config files editable after `npm run build`

I'm deploying a web app using react-redux soon and I was wondering if it was possible to let the user using the production version to modify a config file so that he can set his own initial settings.
Is it possible, after I run npm run build, to have a config.js file in the build folder that the user can go and directly modify?
If not, is there any better way to accomplish that using another strategy?
Thank you
If I understand correct you want to expose a API to change some of settings in config for end user and then somehow restart your API servers to apply those changes.
Always a dangerous way of doing things. You can expose functionality to change few of fields of config for end user and when applied restart the node process. There are few ways to restart node process from code itself or writing a hot script which monitors changes in source directory.
But beware its risky thing to do. Hope this helps.

Update file without restart of server in Node.js

Is it possible to update a route,model or controller.js file without restarting the Node.js Server?.
I'm currently dealing with a client who wants constant changes to the application in a very frequent event. And the application deals with user session etc.. Whenever we make any changes to the application it requires a restart for the update to get reflect, which is very expensive in-terms of an high traffic situation.
I have seen some server application providing a feature called Rolling Restart but again I'm not sure whether it is a good way to maintain the user session across the restart event. Or do we have any other solution to deal with this kind of situation.
You can restart a server without downtime yes, I recommend you take a look at PM2 https://github.com/Unitech/pm2
You can have multiple instances of node running and when you set a restart it does it gradually, making that you don't have downtime, it also distributes load to the different instances running so it speeds up your app, hope this helps :-)
Nodemon is what I have used before and I was very happy with it.
Install
npm install -g nodemon
then run your app with
nodemon [your node app]
Done

Categories

Resources