Writing a Perfect Web Page Screenshot Microservice
January 12, 2020
Everyone needs to take a screenshot of a web page programmatically once in a while. Sometimes it must be an automated task. Or maybe you need to generate a pretty PDF out of your web application report?
A headless browser is your best friend in these cases. Which one should you use? You want something modern and well-supported with a nice API. That would be headless chrome with the Puppeteer. Not PhantomJS, please. It’s not supported anymore.
The next question is how you want to run it. On a separate machine or set of servers depending on uptime requirements? Probably not, so let’s make it serverless. I bet you won’t use it that often. And since nobody likes the idea of vendor-lock, use the Serverless Framework. It will make your microservice easier to transfer to other cloud providers or your own servers.
Should you add a cache? Maybe. It depends on your use-case. If you’re like us and take a lot of screenshots of user website pages and show them throughout an application, then yes. It probably makes sense to store them on something like AWS S3. It supports object expiration by default and it’s very cheap.
And that’s the microservice we wrote. We also made it available for everyone here. We didn’t include caching because it will depend on your use case and your environment. It’s a bit AWS-specific but shouldn’t take more than an hour to switch to any other cloud provider supported by the Serverless Framework.
We believe in reusable microservices.
Learn more at blocks.directory