PWAs have been gaining popularity for quite sometime now. Crafting a good, performant experience is continuous journey.
So before even embarking on the PWA journey, we should invest time on setting up the development workflow.
This separates great apps from apps that were great once.
I want every Pull Request on this repository from now on to be tested and audited for performance issues.
How do I do that? Using a CI server, like Travis. With Travis we’ll add support for lint checks, automatic stage deployments, audit using lighthouse and would be in absolute control over the changes happening on the app at all times.
Step 1 — Adding Travis CI
Enable Travis CI for your repository by going to your profile page
Once you enable the switch, you’d then need to add a
.travis.yml which will instruct Travis on what to do at different stages of the build.
Above is a simple
.travis.yml which runs lint checks on PR.
Step — 2: Adding stage deployments
- Being able to see the code changes in action helps the reviewer a lot to merge your changes with confidence.
- I’ll use Now to deploy your code to a unique stage URL once a Pull Request is created. You can use Surge as well.
- Our challenge is to integrate Now deployments from Travis so that anytime a PR is created/updated the new changes are deployed and ready to be seen/audited by other reviewers.
- now-travis is an excellent utility to deploy to now. The thing with now deployments is — Every time you deploy a project, now will provide you with a new, unique URL.
- But now-travis doesn’t provide the ability to save the URL so that we can use it later to run lighthouse audits. I’ll cover this bit in a little while.
- So I added a change to save the deployed URL to a temporary file and created a Pull Request which hasn’t been merged as of now. You can use this fork for your setup: https://github.com/ankeetmaini/now-travis
npm i -D ankeetmaini/now-travisThis will add now-travis as a dev dependency to your project.
- Follow the instructions in the README to integrate now deployments with Travis.
- now uses
npm run now-startto start your application. It gives prepference to now-* commands so in this case
now-startwould be executed instead of
npm start. This is useful because in dev mode also I’ll use
npm startand in production I may need to pass NODE_ENV=production. Or you may need to send something else entirely.
- Update your
.travis.ymlto run now-travis after successful build. See this line of code in after_script `NOW_ALIAS=react-hn-ankeetmaini node_modules/.bin/now-travis — file=now_url`
With this setup now Travis will deploy every pull request¹. You can see in the below image that a Staging deployment was done.
Step — 3: Integrating Lighthouse
- Lighthouse is an audit tool by Google which checks your app against a number of points and scores it.
- It’s vital to run this audit continuously, so as to always keep our app fast and performant. We’ll use lighthouse-ci to integrate it with Travis.
- Add lighthousebot as a collaborator in your repository, so that it can update the status of your PR and post a comment with the Lighthouse score.
- Request an API key and add an ENV variable in the Travis. This isn’t necessary as of now, but will be done in the future.
- Since now deploys our app to a unique URL, we save it in a file named
now-url. We need to read this URL from the file and give it as an input to lighthouse-ci.
- To do this I created a file run-lighthouse.js at the root of the folder, with the following code. lighthouse-ci takes following options and we’re passing the same in the below file.
7. Lastly add an entry into
.travis.yml in the after_script section to run the above file after the stage deployment is done. — file is the argument which takes in the file name from which the deployed URL needs to be read. This will now evaluate your stage deployment and fail² the PR if you’ve not passed a minScore and also post a comment with your lighthouse score, see this PR https://github.com/ankeetmaini/react-hn/pull/9
Congratulations!, you’ve successfully setup an awesome workflow. All the code used in the citations lives here.
 since free OSS plan in now can only have three active deployments at a time, you might need to manually remove deployments using
now rm id
 right now the lightousebot will only post a comment and not fail your PR because of insufficient rights on the repository, the above screenshot which shows failing of my PR is because I’ve run a separate instance of lighthouse-ci for demo purposes. See this issue for more details