
- Astro JS Tutorial
- Astro JS - Home
- Astro JS - Overview
- Astro JS vs Next JS
- Astro JS vs React JS
- Astro JS Setup
- Astro JS - Installation
- Astro JS - Project Structure
- Astro JS - Pages
- Astro JS Architecture
- Astro JS - Islands
- Astro JS - Islands Architecture
- Astro JS Routing
- Astro JS - Routing
- Astro JS - Dynamic Routing
- Astro JS - Redirecting Routes
- Astro JS - i18n Routing
- Astro JS Configuration
- Astro JS - Configuration
- Astro JS - Editor Setup
- Astro JS - TypeScript Configuration
- Astro JS - Environment Variables
- Astro JS Build UI
- Astro JS - Components
- Astro JS - Slots
- Astro JS - Layouts
- Astro JS - Fonts
- Astro JS - Scripts
- Astro JS Create Website
- Astro JS - Markdown Contents
- Astro JS - Add Images
- Astro JS - Manage Content
- Astro JS - Content Collections
- Astro JS - Data Fetching
- Astro JS Styling and CSS
- Astro JS - Styling
- Astro JS - CSS Integration
- Astro JS - CSS Cascading Order
- Astro JS Integrations
- Astro JS - React Integrations
- Astro JS - Svelte Integrations
- Astro JS - Solid Integrations
- Astro JS - Vue Integrations
- Astro JS Adapters
- Astro JS - Netlify Adapter
- Astro JS - Cloudflare Adapter
- Astro JS Testing and Deployment
- Astro JS - Testing
- Astro JS - Deployment
- Astro JS Advanced Topics
- Astro JS - State Management
- Astro JS - Prefetching
- Astro JS - Middleware
- Astro JS - Endpoints
- Astro JS - Authentication
- Astro JS - Bun Environment
- Astro JS - Docker
- Astro JS - View Transition
- Astro JS - Transition Directives
- Astro JS - Astro DB
- Astro JS - Bundling
- Astro JS Useful Resources
- Astro JS - Interview Questions
- Astro JS - Cheatsheet
Astro JS - Cloudflare Adapter
What is Cloudflare?
Cloudflare is a web security platform that provides CDN services, DDoS mitigation, and other security features for websites. The platform helps to optimize website performance, remain secure, and handle high traffic efficiently.
Key Features of Cloudflare
Cloudflare is used to protect and optimize websites. Some of the key features of Cloudflare are:
- DDoS protection: Cloudflare provides protection against denial-of-service attacks by filtering malicious traffic form legitimate traffic.
- CDN services: Cloudflare offers a global content delivery network (CDN) which helps to cache content closer to the user. This will will reduce latency and improve website performance.
- Serverless Computing: Cloudflare Workers is a serverless computing platform that allows developers to run code at the edge of the network.
- SSL/TLS encryption: Cloudflare provides free SSL/TLS certificates to encrypt data and secure website.
Cloudflare Adapter For Astro
The Cloudflare adapter in astro enhances the Astro build process and prepare the project for deployment through Cloudflare. The HTML pages and on-demand rendered routes will be deployed directly to Cloudflare. The astro will generate a Cloudflare Worker script that will serve the Astro application from the edge of the network. Now let's see how to set up Cloudflare adapter in Astro.
Step 1: Install Cloudflare Adapter
First, you need to install the Cloudflare adapter for Astro. You can do this using the following command:
>> npm install @astrojs/cloudflare
Step 2: Configure Cloudflare in Astro
Next step is to apply the integration to your astro.config.mjs. Add the adapter and your desired on-demand rendering mode to your astro.config.mjs file:
import { defineConfig } from 'astro/config'; import cloudflare from '@astrojs/cloudflare'; export default defineConfig({ output: 'server', adapter: cloudflare(), });
Cloudflare Adapter Options
The cloudflare adapter options are used inside the cloudflare() function of astro.config.mjs file. Each option is an object that can be passed to the cloudflare() function. The available options are:
The imageService Option
The imageService option determines which image service is used by the adapter. The adapter will default to compile mode when an incompatible image service is configured. Otherwise, it will use the globally configured image service. It can have following values:
- cloudflare: Uses the Cloudflare Image Resizing service.
- passthrough: Uses the existing noop service.
- compile: Uses Astroâs default service (sharp), but only on pre-rendered routes at build time.
- custom: Always uses the image service configured in Image Options./li>
import {defineConfig} from "astro/config"; import cloudflare from '@astrojs/cloudflare'; export default defineConfig({ adapter: cloudflare({ imageService: 'cloudflare' }), output: 'server' })
The platformProxy Option
The platformProxy option controls how Cloudflareâs system connects to Astroâs development server. It helps Astro mimic Cloudflareâs environment by:
- Connecting to local Cloudflare Workers (small serverless functions running on your computer).
- Simulating Cloudflareâs special features (like security tools and caching) inside Astroâs development setup
This allows you to test your Astro project locally, same as if it were running on Cloudflare, even though youâre using Node.js on your own computer.
Cloudflare Runtime
The Cloudflare runtime gives you access to environment variables and Cloudflare bindings. The runtime uses bindings found in the wrangler and .dev.vars configuration files.
Example
In the code below, we have an environment variable configuration set up in wrangler.toml:
// File - wrangler.toml [vars] ENV_VARIABLE = "test"
Now, you can access the environment variable in your Cloudflare Worker script using Astro locals.
// File - src/index.js --- const { env } = Astro.locals.runtime; const EnvVariable = env.ENV_VARIABLE; ---
Also, You can access the runtime from API endpoints through context.locals:
// File - src/api/hello.js export function GET(context) { const runtime = context.locals.runtime; return new Response('Some body'); }