If you have already created a Next.js application and want to deploy it online immediately without using traditional hosting, you can deploy your Next.js app to Cloudflare Workers. This tutorial will walk you through the process.
The advantage of using Cloudflare Workers is that you do not need to purchase a domain or hosting right away, and you can perform build processes just like you would with Vercel.
How to Deploy Next.js to Cloudflare Workers
Before we begin, this tutorial uses a Next.js project stored on GitHub to simplify CI/CD. If your project will require frequent script updates, we recommend using GitHub or GitLab.
1. Add Dependencies to Package
Before you push your code to Git and deploy to Cloudflare, make sure you have added OpenNext and Wrangler to your package.json file.
npm install @opennextjs/cloudflare@latestnpm install --save-dev wrangler@latestIf the OpenNext and Wrangler dependencies are already in your package.json, you can push your code to Git. If they are already in your Git repository, you can proceed to the next step.
2. Create an Application in Cloudflare
Now you can log in to Cloudflare, then open the menu Build > Compute > Workers & Page, and click the button Create Application.
Next, you can connect your GitHub or GitLab account. If you have already connected it, you can select the Continue with GitHub/GitLab option and choose the repository you want to use.
3. Set Up the Application
Now you can set up your application with some basic options like build and deploy commands. If you are using Next.js, you can use npm run build for the build command, and you can leave the Deploy Command as the default.
If your project has a .env file, you need to add it by clicking the Advancedoption, then fill in the Variable Name and Variable Valuesections. For sensitive variables, make sure to enable the Encrypt.
option. Once everything is filled out and complete, you can click Deploy and wait for the process to finish.
4. Access the Website
After the build process is finished and successful, you can access your website using the subdomain provided by Cloudflare. You can check or add a domain from the Settings menu in the Workers application you created.
That is how easy it is to deploy your Next.js project to Cloudflare Workers.
COMMENTS
Loading...