4 min read
The power of cloudflare tunnels feat. Contentful

Ever had one of those moments where a seemingly simple task turns into an unexpected adventure? That’s exactly what happened when our team decided to implement a caching system for our website. Spoiler alert: Cloudflare Tunnels came to the rescue, but let me take you through the whole journey.

The Backstory

Picture this: we had just implemented this elegant caching layer for our website data. Our content delivery was blazing fast, our servers were happy, and our metrics were looking beautiful. Life was good… until we hit a classic developer roadblock.

You see, we needed to keep our cache in perfect sync with our content team’s updates in Contentful. Our solution seemed straightforward enough – a Node.js worker that would spring into action whenever Contentful sent a webhook notification, quietly refreshing our cache in the background. Clean, efficient, and completely automated. On paper, it was perfect.

The Challenge: Local Development vs. Reality

But here’s where things got interesting. Anyone who’s worked with webhooks knows the classic local development conundrum: how do you test a webhook endpoint that only exists on your machine? Contentful, like any respectable service, needs a public URL to send those webhook calls to.

There I was, starting at my terminal running localhost:3000, scratching my head and thinking, “How am I supposed to verify this cache-busting worker without pushing to production and praying it works?” We’ve all been there, right?

Cloudflare Tunnels: The Game Changer

That’s when I discovered Cloudflare Tunnels, and it felt like finding a secret passage in a complex maze. Let me walk you through how I got it working, because this solution might save you hours of frustration.

Step 1: Setting Up Your Tunnel

First things first, you’ll need cloudflared, Cloudflare’s CLI tool. After installation, you’ll need to authenticate:

cloudflared tunnel login

This command opens a portal (quite literally, it opens your browser) to the Cloudflare dashboard where you authorize the connection. Your credentials get safely stored in your home directory, like storing your secret key in a secure vault.

Step 2: Creating Your Secret Passage

Next, we create our tunnel. I named mine contentful-webhook:

cloudflared tunnel create contentful-webhook

This generates a unique UUID for your tunnel and creates a credentials file under ~/.cloudflared. Think of this as your tunnel’s unique identifier in the vast network of the internet.

Step 3: Configuring the Gateway

Here’s where we set up the rules of engagement. Create a configuration file (I keep mine in the project root for easy access):

tunnel: <YOUR_TUNNEL_ID>
credentials-file: /path/to/your/.cloudflared/<TUNNEL_ID>.json

ingress:
  - hostname: your-chosen-subdomain.your-domain.com
    service: http://localhost:3000
  - service: http_status:404

This configuration is like a traffic controller, directing incoming requests from your public subdomain to your local development server.

Step 4: Creating the Bridge

Now, we need to create a CNAME record to connect your chosen subdomain to the tunnel:

cloudflared tunnel route dns contentful-webhook your-chosen-subdomain.your-domain.com

Step 5: Opening the Gateway

Finally, we bring our tunnel to life:

cloudflared tunnel --config path/to/your/config.yml run contentful-webhook

With the tunnel running, my local development environment was suddenly accessible to the outside world – but in a controlled, secure way. Contentful could now successfully send webhook notifications to my local server, and I could test and debug my cache-busting implementation in real-time.

The Takeaway

Sometimes the best solutions aren’t about writing more code, but about finding the right tools to bridge the gaps in your development workflow. Cloudflare Tunnels turned what could have been days of deployment-test-fix cycles into a smooth, local development experience. Remember, every developer’s journey is filled with these moments where a seemingly simple requirement leads us to discover new tools and better ways of working. That’s what makes our craft so interesting, isn’t it?