How to make a button that updates when someone clicks it [duplicate] - javascript

I am trying to deploy a react app with the backend into the same repository on github pages. It all works fine until I add the backend code to it. I can deploy the front-end, but I cannot figure out how to deploy the backend to github.
In my package.json I have the followings:
....
"scripts": {
"start": "react-scripts start",
"server": "nodemon server.js",
"client": "npm run start --prefix client",
"dev": "concurrently \"npm run server\" \"npm run client\""
}
...
I have pushed it to github. Installed gh-pages package.
I have added
"homepage" : "https://[your-user-name].github.io/[your-repo-name]/"
“predeploy”:
“deploy”:
I think this is where I am going wrong. I know what predeploy and deploy should specify, but I have tried to enter a thousand different versions and I am getting error.

Github pages will not execute any serverside code. You may only upload static files (html,css,js, images, etc.).
In order to have a hosted backend you should look for another service like Google Cloud, AWS Lambda, Heroku, etc.

Related

Module not found on migration from 10 to 13 - nextjs

I've recently tried to migrate our old site running on next 10 to the latest version 13. The site has a custom backend written with express. While running the project via concurrently I keep getting Module not found error as soon as I run the client on 3000. This is the scripts section("npm run dev" below is used to run the project):
"start_dev": "nodemon --inspect -w ./src/server -w ./src/server.js -w ./src/start.js ./src/start.js",
"build": "next build",
"client": "next dev",
"server": "npm run build && npm run start_dev",
"dev": "npx concurrently -k \"npm run server\" \"npm run client\""
Things seem to be working fine if I build the project first and then run client and server on separate terminals. But with this also, if I make a change in server that doesn't get picked up by client on the other terminal.
I'd highly appreciate if someone can give this a look as I'm stuck on the same issue for quite some time now. Please let me know if any further information is needed from my end here.
the problem is if the .next folder is generated by nodemon change, client side webpack hot reload cannot read .next folder
"scripts": {
"client": "next dev",
"build": "next build",
"server": "npm run build && npm run nodemon",
"start": "next start",
"lint": "next lint",
"nodemon": "nodemon -w ./src/server.js -w ./src/server ./src/start.js",
"dev": "concurrently -k \"npm run server\" \"npm run client\""
},
When you run npm run dev and refresh the page, this is how .next/pages folder. home index.js exists:
if I make a change in ./src/server.js after I see the home page on screen, .next folder will be recreated without index.js by nodemon change and if I refresh the page I will get error.
when you reload your page after change in server.js file you get error because index.js page does not exist. It does not exist because .next folder was not generated by webpack
When you first start the app, before refreshing the browser, if you make a change on server.js file, .next folder will be created and if you refresh the page you will get the same error. for some reason if .next folder generated by the nodemon change, client side cannot communicate by .next folder.
concurrently runs scripts in parallel, with npm run server you are calling next build and with npm run client you are calling next dev. So, you are calling next dev and next build scripts concurrently. Even though it says it is concurrently, I think it is not exactly concurrently. if you install npm i npm-run-all and add this script
"dev": "npm-run-all --parallel client server"
your app will never load, you will see flickering in a loop.

Why is Next.js's compile indicator showing up in my production build?

I noticed after building and deploying a Next.js website that the black compile indicator still shows up in the bottom-right of my browser like it would locally.
This indicator: https://i.stack.imgur.com/FVWEU.gif
On Next.js's website:
This indicator is only present in development mode and will not appear when building and running the app in production mode.
Even locally when I run yarn build and yarn start, the indicator displays while the page loads.
During the build process, it says:
Creating an optimized production build
Done in 20.89s.
My concern isn't so much that the indicator is displaying, because I can disable it. I'm concerned that I'm not getting an optimized build since something is displaying that should only be displaying in development mode.
Note: I can't share a link to the website as it is work-related.
Has anybody seen this issue before?
Thanks in advance!
Technical information:
Next.js Version 12.1.1
Dockerfile:
FROM node:16.13.0-alpine
# Install packages.
WORKDIR /app
COPY package.json .
COPY yarn.lock .
RUN yarn install
# Copy all other files.
COPY . .
# Build the app.
RUN yarn build
# USER node
EXPOSE 3003
CMD ["yarn", "start"]
package.json (scripts block):
"scripts": {
"dev": "node ssr-server.js",
"build": "next build",
"test": "node_modules/.bin/jest",
"test:coverage": "node_modules/.bin/jest --coverage",
"test:watch": "node_modules/.bin/jest --watch",
"start": "node ssr-server.js"
},
In the custom server JavaScript file, there should be a line that check if the environment is development or production:
const dev = process.env.NODE_ENV !== 'production'
update the start script in package.json to set that environment variable:
"scripts": {
"dev": "node server.js",
"build": "next build",
"start": "NODE_ENV=production node ssr-server.js"
}

trying to make "npm run dev" create 2 shell instances

I'm using json-server as my local server for a nuxt project and i want to automatically launch the server and then run the project on another shell instance using "npm run dev"
in the scripts tag in package.json this is what i came up with :
"dev": "json-server --watch db.json --port 3004 & nuxt"
but this script only starts the server
try concurrently
npm install -g concurrently
"dev": "concurrently \"json-server --watch db.json --port 3004\" \"nuxt""

How to use React Customize Environment Variables inside component

I used Create-React-App to create a react application, now I want to have 3 different envirement variable for build :
1 - Development
2 - Staging
3 - Production
I follow this article from Facebook. So based on this artile, now I have 3 .env files in my project :
.env.development
.env.staging
.env.production
in each one of this .env files I have my API address based on the stage we are building the app, like this :
API_URL=https://MYAPI.com/STAGE
in my package.json I have these scripts :
"scripts": {
"start": "env-cmd .env.development react-scripts start",
"build_development": "env-cmd .env.development npm run build",
"build_staging": "env-cmd .env.staging npm run build",
"build": "react-scripts build",
}
In my component which call this api URL, I use this to get the API_URL :
let apiUrl = process.env.API_URL
So once I run for example : npm run build_staging
It will build the app but once I debug it, apiUrl is always undefined.
Not sure what is wrong? anyone had such a problem?
I believe your environment variable names need to start with REACT_APP_ using create-react-app, as stated in the same GitHub page you linked. Changing your API_URL to REACT_APP_API_URL should fix the issue.

How to make create-react-app auto build?

I have been using create react app for a while. 'npm start' or 'yarn start' autoreloads works fine by itself but now I have an another problem. Currently I run the app on express server through the build folder, and I use 'npm run build' since express is serving the built files. There are many api calls which requires the app to be ran through this way. Now it become tedious to manually do 'npm run build' every time. Is there a simple way or work around to build automatically just like 'npm start' without eject the app(I know could eject and configure webpack to do that, but i don't want to go down that path)? Thanks
Unfortunately this is something you will have to do yourself. You can use a tool like npm-watch to accomplish what you want though:
Install npm-watch
npm i --save-dev npm-watch
package.json
{
"name": "react-app",
"version": "0.1.0",
"private": false,
"devDependencies": {
"npm-watch": "^0.1.8",
"react-scripts": "0.9.5",
},
"dependencies": {
"react": "^15.4.2",
"react-dom": "^15.4.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject",
"watch": "npm-watch"
},
"watch": {
"build": "src/"
}
}
Afterwards, just use npm run watch to start up npm-watch so it can rebuild your assets on changes.
Update:
React-scripts now includes a proxy option that proxies requests to a different host/port. For example, if your backend is running on localhost at port 9000 under the /api route, then you would add this line to your package.json: "proxy": "localhost:9000/api". You could then make requests as you normally would in production. (source: https://create-react-app.dev/docs/proxying-api-requests-in-development)
While this doesn’t really answer your question, you shouldn’t be using npm run build in development. Not only the rebuilds are slow, but it also skips important React warnings for performance and size, so you’ll end up scratching your head more and getting a lot less details in the warnings.
If you just need to do API requests with Express, use the proxy feature which lets you proxy API requests from npm start to your server. There is also a tutorial with a matching repository demonstrating how to do that.
In production, of course, you should use the build produced by npm run build. But you would only need to run it before deployment.
Run your backend on a different port. If your running on express modify the file bin/www
var port = process.env.PORT || 9000
and in your /src folder create a config file where you configure your api host,routes, params etc
//config/index.js
export const Config = {
protocol: 'http',
host: window.location.hostname, //or the environment variable
params: '/api/',
api: {post:'/posts/'}
}
and in your calling component or where ever your calling the api's
import {Config} from '../config'
axios.get(`${Config.protocol}${Config.host}${Config.params}${Config.api.posts}${some id i guess}`)
The easiest way that I found (as of 10/19/21) is to use cra-build-watch.
Works perfectly.
i am also using create react app, this is how i modified my scripts to run project for development(windows), build the production build and run the production build.
"scripts": {
"start": "set PORT=8080 && react-scripts start",
"build": "react-scripts build",
"deploy": "set PORT=8008 && serve -s build"
}
npm start : run project for development(windows)
npm run-script build : build the production build
npm run-script deploy: run the production build
npm install -g serve before run npm run-script deploy.
1> npm install create-react-app -g
2> create-react-app Your_Apps_Name

Categories

Resources