Medium15 minAstro Fundamentals
UpdatedAug 3, 2026
Edit

The Astro Global & Request Context

CONCEPTS:The Astro Global Object

Question Variations

  • "How do you read a query parameter from the URL in an Astro component?"
  • "Explain how to set a cookie in an Astro page."
  • "What information is available in `Astro.request`?"
  • "How do you perform a server-side redirect?"

Why This Is Asked

The Astro global is the primary way to interact with the web request. Understanding how to use it is essential for handling cookies, redirects, and dynamic routing logic.

Key Concepts

  • Cookies: Reading and setting cookies for personalization or auth.
  • URL API: Parsing search parameters or checking the current path.
  • Request Headers: Detecting user-agent or language preferences.
  • Redirects: Handling conditional logic (e.g., redirecting if not logged in).

Question Variations

  • “How do you read a query parameter from the URL in an Astro component?”
  • “Explain how to set a cookie in an Astro page.”
  • “What information is available in Astro.request?”
  • “How do you perform a server-side redirect?”

Answers by Technology

+ Add Variant
AstroImprove this answer ✏️

Expected Answer

The Astro global provides a wide range of utilities for the current request.

Reading Search Params:

const search = Astro.url.searchParams.get('q');

Handling Cookies:

const userSession = Astro.cookies.get('session')?.value;
Astro.cookies.set('theme', 'dark', { path: '/' });

Redirecting:

if (!userSession) {
  return Astro.redirect('/login');
}

Why It Matters

The Astro global is the “Connective Tissue” of your application. It allows you to build dynamic, context-aware pages. Because it follows standard Web APIs (like Request and URL), it makes the framework feel consistent with modern web development standards.

Common Mistakes

  • Mutating Astro.props: Props should be treated as read-only.
  • Using Astro.cookies in Static mode: This will work during development but will return empty/default values during a static build since there is no visitor cookie at build time.
  • Redirecting too late: Astro.redirect must be returned or called before any HTML is sent. In the frontmatter, it effectively acts as a short-circuit for the rest of the component.

Follow-up Questions

  • What is the difference between Astro.url and Astro.request.url? (Answer: Astro.url is a convenient URL object helper, while Astro.request.url is the raw string from the Request object).
  • How do you get the client’s IP address? (Answer: In SSR mode, it’s often available via Astro.clientAddress, depending on your adapter).