< all posts

Migrating to Astro and Workers

About a week after proudly publishing the portfolio site, with my first post (Working with Fresh), I was surprised to read that Deno and much of its supporting systems were either being dismantled or left with an uncertain future as the team was absorbed into Cloudflare (read more). This left me with a conundrum, do I stick with using the runtime and the tooling, both were at present possible though in any event DenoDeploy, that was service hosting the site, was being taken down completely in six months so I had to make a change there. The framework Fresh was absent from the commentary from Deno themselves and at that point I could see no commentary on their site as to its future but the abrupt nature of the removal of most of Deno's systems made me hesitate.


So I decided that it was best to make the move to a new framework and a new hosting system ASAP whilst the logic for the site's operation was still fresh in my head and would aid the transfer.

Why Astro, Why Cloudflare

The second of those is much easier to answer, in fact was hinted at by Deno themselves, the size and scale of Cloudflare would provide the flexibility and resilience in future such that I shouldn't need to undertake this exercise again for any reason other than personal choice (the open source nature of Workers would make that easier in future anyway if I like the system enough). As Fresh had a guide for hosting with Workers I thought this a good place to start.


It was, though the Fresh docs were light on information and Workers had none where it concerned Fresh, nor could I find much online. What began was a series of trial and error experiments as I worked to configure Workers and Fresh to play nicely. I did get some functionality to work however each step became a painful exercise. This was in part due to my limited understanding of Workers and that was something I wanted to correct but Fresh was seeming not to be the framework to do it with as the pain points were taking away from my overall understanding (I did improve my understanding a little by having the vanilla JS Elf Exchange project run smoothly through Workers). At this point I questioned whether Fresh was the right fit going forwards, I liked it yes, but its future was uncertain and its backer vanishing, so I decided to turn elsewhere.


I shopped around briefly and landed on Astro, for four reasons: it is a project with greater interest and a longer history to offer stability, it intrigued me to its ability to utilise other front end frameworks within its system (this would allow me to use some of my fresh preact components), its documentation and community reasources were extensive (there was even an introduction on my training provided Scrimba, which only served to whet my appetite further), and it was not a React like framework, which would provide an enjoyable challenge.

My experience with Astro

Converting my JSX to Astro with its frontmatter and output sections was actually very straightforward, doing this early in my site's life was ideal as I was yet to introduce too much complexity. This meant static pages were already set up and ready to go, links between them worked and even styling was fairly straightforward to get right. I like the layout of the Astro file, part of me felt uncomfortable with it, it seems to want to you write JS but also not to, however the delineation of inputs and 'workings' followed by output, whilst not written as a purely functional component, seemed to make it easier to understand some of the components, time will tell but it was different enough to be useful.


Astro has the ability to be very extensible, that came across from the off, and felt a little different due to that in the presentation of its docs when compared to Fresh. Fresh felt a little more opinionated in its selection of tools to facilitate sending minimal JS to the clients browser, Astro less so. I was happy to continue with a multipage application and whilst Astro offered a way to do HTML partials similar to Fresh it was not tightly integrated and instead offered alternatives, its own clientrouter or just a suggestion to make use of the viewtransitions API that was now mature and continuing to improve in functionality. This exploration of other methods and how they supported the ethos of server side rendering, with minimal JS to browser, encouraged me to start simple by sticking with viewtransitions only and see where it got me. Happily, they have worked very well and I very pleased with the outcome.


Active links and theme switching took the longest to re-enable in Astro, this wasn't surprising and proved a great way to explore Astro's features and its compatibility with frontend frameworks.

Lets start with active links, I tinkered around with Astro itself on the frontend but was finding this a challenging feature to implement (Fresh built in a aria attribute when rendering, Astro does not). As I didn't want to move to dynamically rendering every page I decided to ship a small preact Nav component, in part as I already had one and secondly to test the interface. It worked seamlessly, I simply made sure Astro had the correct dependencies (they have a simple CLI for this) and I was good to go. Active links up, less than 30 lines JSX.

Theme switching was harder until I realised I was overthinking the problem. Partials in Fresh had allowed a multipage app to feel single page but there was a cost, it had to manage that DOM insertion, all I had to manage was picking a CSS style from a HTML attribute. Astro themselves did help with this, much like with Fresh I investigated what they suggested to address the problem. Local storage! Admittedly it has been a while since I had thought of local storage for much seeing as most frontend frameworks like React help state persist during use, however the solution was simple. Write the default/users preference to local storage and then have a component running on the client to collect that information, set up the HTML data-theme to pull the correct styles and then run the switcher (changing both the data-theme and local storage value). Here it became apparent that using a frontend framework, maybe due to my familiarity, was easiest to implement the correct behavior on the client (useEffects proved more reliable than listening for different stages of document loadings).


Conclusion

Whilst an unexpected bump, my experience having to switch hosting platform and frameworks was engaging and useful. Astro feels like a versatile and progressive approach that I have only scratched the tip of whilst the Cloudflrare product suite and Workers does indeed feel like it can cover all of my needs for the foreseeable future (both framework and host had clear documentation integrating with one another rendering the upload to Workers straightforward with little else to say). Until those behind Astro decide to hang up their keyboards I will happily turn to this framework in the future.

| return to all posts |