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.
For example let’s consider a GitHub repository which has Hacker News front end implemented. This is not a PWA yet. It’s made with React and Redux.
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.
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.
npm i -D ankeetmaini/now-travis
This will add now-travis as a dev dependency to your project.npm start
or npm run now-start
to start your application. It gives prepference to now-* commands so in this case now-start
would be executed instead of npm start
. This is useful because in dev mode also I’ll use npm start
and in production I may need to pass NODE_ENV=production. Or you may need to send something else entirely..travis.yml
to 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.
CI checks PR for lint, deploys and audits app performance
now-url
. We need to read this URL from the file and give it as an input to lighthouse-ci.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
./run-lighthouse.js --file=now_url
Congratulations!, you’ve successfully setup an awesome workflow. All the code used in the citations lives here.
[1] 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
[2] 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