Deploy your frontend projects with CloudFlare Pages

Software Engineer โข TypeScript โข JavaScript โข Angular โข VueJS โข WordPress โข Ex-founder of ng-enious
Search for a command to run...

Software Engineer โข TypeScript โข JavaScript โข Angular โข VueJS โข WordPress โข Ex-founder of ng-enious
No comments yet. Be the first to comment.
Hello good people! In this short article, we're going to convert KML files to the GeoJSON format. The main takeaway aside from discovering something new is that I'll tell you about the popular solution, and then I'll show you the new solution I had t...

By the time I'm writing this article, you surely may have heard of ChatGPT. This "new AI" made so many waves that I couldn't resist digging through it and writing something about it. But rather than jumping into the hype train of ChatGPT and OpenAI, ...

Hello good people! In this article, we're going to create an npm package step by step. By the end of this article you will be able to: Build your own package Use it on other projects Publish it on npm [Bonus] Publish your TypeScript types with your ...

Hello good people! In this article, I want to shed light on a very interesting console function. It's called console.timer() and might help you understand how your code runs and optimize it. This will be a rather short article. We'll go through two s...

Hello good people! In this article, I want to deep dive with you into a usually overlooked file in a project. The package.json is crucial to a lot of frontend/backend NodeJS projects. Yet a lot of developers don't really know what it can really do. W...

Tarek's /dev/log
10 posts
Everyday blogs about software engineering, Frontend Web development and everything in between.
Hello good people! In this article, I'll present to you a new CloudFlare feature called Pages. This new feature lets you deploy a Frontend Web app easily and fast.
First I'll be introducing CloudFlare and CloudFlare Pages. Next, I'll quickly compare it to the current alternatives. Then, I'll walk you through the process of deploying your own project. Finally, we'll explore more advanced settings with this tool.
CloudFlare is mostly known for its Content Delivery Network (CDN) services. While CDNs are an interesting subject I'll consecrate an article for them soon. For now, the main highlight is that CF has a distributed network of servers that boosts the loading of websites worldwide.
In a nutshell, CF is trying to make the Web better by providing us with a robust CDN, DDoS mitigation, easy to set up HTTPS, and much more. The next step they took in this direction is the Pages feature. It's a Jamstack platform in other terms, it lets you easily deploy a Frontend app or static Website.
Let's get this out of the way, CF Pages is not a new concept. For instance, Github Pages is basically the same thing. There's also the excellent Netlify or GitLab Pages. So now you might ask yourself "Why bother with yet another tool?"
This is a legitimate question, let me list few reasons:
Finally just one last clarification: I'm not trying to sell you CloudFlare services. I highly advise you to do your own research and choose the service that covers most of your needs.
Start by heading to CloudFlare and create yourself an account. Next head to your email and confirm your account this will land you on a page as follows, just click the " Continue to the dashboard." link.

The GIF below shows:

For this example, I've selected my "personal-cv" repository and clicked the button "Begin setup". You will be redirected to the following configuration page.

Let's break down all those settings:
npm run build will build my project and copy everything in a /dist folder. If you didn't use a preset or you have a specific command, you can enter whatever you like./dist folder. It might be different so pay attention to that.Once you've configured everything, hit "Save and deploy" and wait for the build and deploy process. Meanwhile, you can go fetch your favorite drink because it might take some time.

Once done, you should have your page up and running on, for example, your-project-name.pages.dev in my case since I've set it to personal-cv my page is at personal-cv.pages.dev. And that's it, your project is online! You can start building your awesome app โจ.
If you go back to the Pages section of your CF dashboard, you'll see your project. Nothing stops you from deploying more projects. However, for this last section, I just want to show you a few more powerful settings you can work with for your projects. Just click on your project's name and you'll see 3 tabs; deployments, custom domains, and settings.
You'll notice a deploy status and under it a section called "All deployments", it lists a list of deployed snapshots for each commit you will push from now on. This works even for other branches than your main/master:

As you can see, I have a branch called feature/add-blog-link with the corresponding preview URL next to it. This is really handy if you want to share a preview of your work with friends and colleagues for example or see if everything is working in order.
If you don't like the preset URL CF Pages attributes to your project, you can bind a custom domain to your project, it's really straightforward and I invite you to do it! Mine is just bound to tarekjellali.com.
Settings is the last tab that's a little more advanced. In the sub-section "General" you can:
Next, in the "Builds & deployments" sub-section:
Finally, "Environment variables":
I'd like to touch on this last setting about environment variables just to explain more its purpose and how it works.
What are environment variables? Environment variables (or env) let you set values that your project can read and act accordingly to those values. Suppose your project has two modes, a production mode, and a development mode. When in production, your build will create an optimized and minified bundle of your app. If it's in development mode, it will generate a non-optimized bundle so you can debug it easily.
This is how lots of apps work. But you can also use your environment variables as some information you don't want to have on your GitHub project. Take for example Google's Analytics tracking code. You don't want that in your code, anyone who would fork your project might end up using it. To get around this problem, you can configure your app to take the Google Analytics code from the environment variables.
Say you define an environment variable called GA_ANALYTICS, you can read that information in your build script via process.env.GA_ANALYTICS pretty neat huh? ๐
I've done exactly that for my GitHub personal-cv project.
And that's it! Now you know as much as I know about CloudFlare Pages. What excites me really about this product is that it empowers developers who want to start small. The fact that it's free and accessible for everyone proves again how much CF wants to make the Web a better place.
If you feel that I've forgotten something or didn't explain something, please let me know.
Cheers!
Photo by @kenrickmills from Unsplash.com.