Astro.url

Astro.url is a URL object built from the current request's URL.

Since Astro 1.0 Spec ↗

Syntax

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

Returns

URL — A standard URL instance for the current page request.

Examples

---
const q = Astro.url.searchParams.get('q') ?? '';
const canonical = new URL(Astro.url.pathname, Astro.site);
---
<link rel="canonical" href={canonical} />
<p>Searching for: {q}</p>
Output
Reads a query parameter and builds an absolute canonical link from the path.

Notes

pathname and origin are reliable everywhere. searchParams are only meaningful for SSR routes since static pages are prerendered without a query string. Combine with Astro.site for absolute URLs.

See also