We are using the create-react-app to create our client React app. We are deploying the app several times a week. We'd like to version every build so we can check which version is online.
I'm thinking about adding a meta tag with the version to the index.html file. So currently the header has:
<head>
<meta charset="utf-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta name="description" content="Web site created using create-react-app" />
<link rel="apple-touch-icon" href="logo192.png" />
<link rel="manifest" href="/manifest.json" />
<title>React App</title>
<link href="/static/css/main.b100e6da.chunk.css" rel="stylesheet">
</head>
What do you think about adding a meta like this one:
<meta name="build-version" content="2019-10-08-15-42" />
With the content using the format: YYYY-MM-DD-HH-MM
What do you think about this idea? Or is there another way to version every build.
If it is a good idea, can you help me with some ideas on how to do it?
Thanks to the help of @Bhojendra Rauniyar I got this solution:
public/index.html
:<meta build-version="%REACTBUILDVERSION%"/>
REACTBUILDVERSION=$(date +%Y-%m-%d)-$(date +%T)
sed -i -- 's/%REACTBUILDVERSION%/'$REACTBUILDVERSION'/g' build/index.html
echo React Build Version = $REACTBUILDVERSION
To make it comfortable, these 3 commands can be added to the build in package.json
:
Original build in package.json
:
"build": "react-scripts build",
NEW build in package.json
:
"build":
"react-scripts build &&
REACTBUILDVERSION=$(date +%Y-%m-%d)-$(date +%T) &&
sed -i -- 's/%REACTBUILDVERSION%/'$REACTBUILDVERSION'/g' build/index.html &&
echo React Build Version = $REACTBUILDVERSION",
After the build you can see this meta tag in the build/index.html
:
<meta build-version="2019-10-16-16:31:43"/>
I follow the following approach (will add git branch name in the version):
# First add '<!-- version: %VERSION% -->' to your public/index.html file
$ yarn run build # npm run build (if using npm)
$ VERSION=`git rev-parse --abbrev-ref HEAD`
$ sed -i -- "s/%VERSION%/$VERSION/g" build/index.html
Now your build/index.html
should contain <!-- version: name-of-the-current-branch -->
.
source
To make it comfortable, you may add version command in package.json:
"build": "VERSION=`git rev-parse --abbrev-ref HEAD` &&
react-scripts build &&
sed -i -- 's/%VERSION%/$VERSION/g' build/index.html"
Now, you can simply run:
yarn build # or, npm run build
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With