Skip to content

Fetch your Medium feed in 17ms in JSON format using Cloudflare Workers

License

Notifications You must be signed in to change notification settings

alekrumkamp/medium-feed-json

Repository files navigation

Leer versión en español

Medium Feed JSON

Fetch your Medium feed in 17ms in JSON format using Cloudflare Workers

17ms response request

What's inside this repository?

A facade that glues different Medium APIs to retrieve a clean and simple API interface to get a Medium user feed.

It contains all the code neccesary to be deployed using Cloudflare Workers.


How to get started

Using build script on cloudflareworkers.com

To get a glance of how easy is to fetch your own Medium Feed, you can simply copy the script.js build file and edit the following line const username = 'alekrumkamp' to your Medium username.

Then head to cloudflareworkers.com and paste your code in the editor and click the Update button.

After a few seconds you should be able to see your Medium feed.


Deploy it for free to a workers.dev subdomain

Now that we played around it's time to deploy our code to a Cloudflare Worker.

In order to do this you'll need a Cloudflare free account and get a worker namespace.

Once you have an account, you don't need to add a custom domain. Just head to cloudflareworkers.com and click login.

You should now be able to see this:

Workers

Click it to go to the next step; selecting your unique workers.dev subdomain:

Subdomain selection menu

Now that we have our name space we can create a Worker for it!

Create worker button

Here we will encounter a similar editor to the one present in cloudflareworkers.com. The difference this time is that instead of just toying around with the code, it can run and be deployed in seconds to your Worker subdomain.

Paste the worker/script.js code from the repository with your username into the editor.

Worker editor

To do so, just click the Save and deploy button:

Save and deploy button

Aaand we are done!

Your code will be live after a few seconds on https://{workerName}.{workersSubdomain}.workers.dev

If you'd like to change the name of the worker, at the top left corner you can do so:

Change Worker's name field


Retrieving all your posts

Each request brings up to 10 posts. However the next attribute can be use to retrieve the following 10 entries. Simply call your worker with the query param next with the value of it.

For example:

Initial Request:

https://medium-feed.alekrumkamp.workers.dev/

Follow up Request:

https://medium-feed.alekrumkamp.workers.dev/?next=1483371523050

Once all posts are fetched, the attribute next will not longer be present.


Can I use Wrangler to build, preview and publish this project to Cloudflare Workers?

You sure can!

In fact, this entire project was done using Wrangler.

Once you get over the hype of using Cloudflare Workers in the playground and need to use code versioning and other tools you'll find that Wrangler becomes really handy to test and deploy in this serverless environment.

You'll notice that I included a custom webpack.config.js file in the repository.

If you'd like to minify your bundle before deploying, you can do so by simply deleting this file.

However if you are debugging I recommend you don't do so, so you can receive meaningful Error on line X kind of debugging hints.


Using a custom domain

After you have added your custom domain to Cloudflare, you can go to the Workers section:

Workers menu

Having launched the editor, head towards Routes and add one.

Routes menu

Add route button

Now it's simply a matter of matching a route with its corresponding worker:

Create route modal


Troubleshooting

I'm receiving a Error 1101 Worker threw exception

Third-party rss2json may be rate limiting your requests. Consider creating a free account with them to obtain your API and then adding the api_key query param with your API key to the getUserPath method in UserController.js.

You may be trying to either fetch Medium account or an empty account. Neither of these cases are supported.

I'm only receiving three posts per request

Using Cloudflare workers for free lets you use up to 10ms CPU time. Since this service uses some parsing it may exceed it if the user you are fetching has lots of text content.

You may solve this problem by increasing the limit to 10 posts to be fetched on each request inside the object that is being return in graphqlRequestBody.js. However, be aware that if you don't have a Cloudflare Workers Unlimited account request may fail.

My request is taking considerably longer than 17ms

All requests have to be fetched from Medium the first time for each Cloudflare cache region. However after initial fetching these will be blazing fast.

About

Fetch your Medium feed in 17ms in JSON format using Cloudflare Workers

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published