Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the querystring parameters with Astro

I'm using quite a new technology called Astro (https://astro.build/) to build a completely static, server side rendered page, shipping zero JS.

I have a page with a form that is a simple text input, when the user fills this in and clicks the submit button, it sends a GET request to an astro page. The url will look something like this ....

/?search=1234

What I want to be able to do is get access to that querystring parameter in order to redirect my user to another static page /1234.

I am trying to access the quesrystring parameter with Astro.request, but the object, including the parameters attribute is completely empty.

Is there anyway to access the querystring parameters from a .astro page/file?

like image 459
DrLazer Avatar asked Sep 06 '25 16:09

DrLazer


2 Answers

you can use Astro.url (available since v1.0.0-rc).

e.g.

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

{search ? <p>you searched for: {search}</p> : null}

<!-- rest of your template markup -->
like image 100
widyakumara Avatar answered Sep 08 '25 10:09

widyakumara


I reached out to the developers of Astro and eventually had an email back. This functionality is not possible with Astro, it is not a bug, rather a misunderstanding from me about how Astro works. The pages in Astro are rendered up-front and are entirely static.

like image 28
DrLazer Avatar answered Sep 08 '25 11:09

DrLazer