Hooks for Astro runtime API
Closed this issue ยท 5 comments
Is there any interest in building the equivalent of the qwik-city hooks like useLocation but for the astro integration?
I am struggling to access the Astro runtime API in qwik components:
https://docs.astro.build/en/reference/api-reference/
Hey @grumpyTofu!
If there's a "Astro" way to do this, then that's always preferred since Astro is the meta-framework of choice here.
I do think that this would be important to have in the integration (if this functionality is missing or unintuitive). I'll be happy to review any PR's ๐
Thanks for the quick response @thejackshelton! I've had success getting the values from the Astro API and passing in values as props to my qwik components. This seems to be the most reliable, but ideally my qwik components would not have to have a dependency on an Astro wrapper. I would be down to write some utility hooks, but I have been running into some issues with what I have so far.
I have written an example hook that seems to work okay. I noticed that the astro:page-load
and astro:after-swap
events do not trigger for view transitions, but the document load
event seems to catch all of them. Have you heard of any similar issues related to view transition events with astro?
export const usePathname = () => {
const pathname = useSignal("");
const updatePathname = $(() => {
pathname.value = window.location.pathname;
});
useOnDocument("astro:page-load", updatePathname);
useOnDocument("astro:after-swap", updatePathname);
useOnDocument("load", updatePathname);
return pathname;
};
The final issue I noticed while testing was when I used this hook to toggle some other signal values with the following code:
const active = useSignal(false);
useVisibleTask$(({ track }) => {
track(() => pathname.value);
active.value = Boolean(href && pathname.value === "/");
});
On initial load, it works perfectly. However, if you navigate to a new page, I get the following errors:
![image](https://private-user-images.githubusercontent.com/30991516/332243179-e313b58f-656b-4b6f-a5d8-20116f91a274.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjU3MDY2MjUsIm5iZiI6MTcyNTcwNjMyNSwicGF0aCI6Ii8zMDk5MTUxNi8zMzIyNDMxNzktZTMxM2I1OGYtNjU2Yi00YjZmLWE1ZDgtMjAxMTZmOTFhMjc0LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA5MDclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwOTA3VDEwNTIwNVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWIyYzkxNmNjZmVkZjAxMTI0Mzg1M2Q0NGRkYzkzNjdlMmUxOWI0ZTQ0MTcxZWNhZWJjMjdlMDE1ZTY4MGQ3OTQmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.j53smhtH0sK1-0edINi6mwB9k8eQYpdvzJoLzX3ORyk)
I am not sure if this is because I am a qwik noob, or if there is an actual bug here. Any insight that you can provide?
Thanks for the quick response @thejackshelton! I've had success getting the values from the Astro API and passing in values as props to my qwik components. This seems to be the most reliable, but ideally my qwik components would not have to have a dependency on an Astro wrapper. I would be down to write some utility hooks, but I have been running into some issues with what I have so far.
I have written an example hook that seems to work okay. I noticed that the
astro:page-load
andastro:after-swap
events do not trigger for view transitions, but the documentload
event seems to catch all of them. Have you heard of any similar issues related to view transition events with astro?export const usePathname = () => { const pathname = useSignal(""); const updatePathname = $(() => { pathname.value = window.location.pathname; }); useOnDocument("astro:page-load", updatePathname); useOnDocument("astro:after-swap", updatePathname); useOnDocument("load", updatePathname); return pathname; };The final issue I noticed while testing was when I used this hook to toggle some other signal values with the following code:
const active = useSignal(false); useVisibleTask$(({ track }) => { track(() => pathname.value); active.value = Boolean(href && pathname.value === "/"); });On initial load, it works perfectly. However, if you navigate to a new page, I get the following errors:
I am not sure if this is because I am a qwik noob, or if there is an actual bug here. Any insight that you can provide?
If you're using useVisibleTask$ with view transitions it might be related to #80
Hey @grumpyTofu! Have you played with the new Astro container API?
I have been swamped lately. Trying to find time to improve the integration whenever possible.
The project now has continuous releases, so any commit or PR made to the project can be consumed in your own Astro project.
If you'd like to take a stab at something let me know ๐
Closed due to inactivity. Astro handles this use case.