Skip to content

Replacing a WordPress Theme with Astro: Typed REST Client

Adityo Guni Waluyo

Migration from a WordPress theme to Astro with a typed REST client, caching, and hybrid rendering for predictable performance.

TL;DR

The team replaced a legacy WordPress theme with Astro, using the WordPress REST API for a cleaner headless setup. Typed interfaces, null-safe fetches with timeouts, and a 60-second in-memory cache keep the frontend resilient when WordPress fails. Static pages prerender at build time, while post and search routes render on demand for dynamic content.

Replacing WordPress Themes with Astro: Typed REST Client and Hybrid Rendering

When opening a legacy WordPress theme repository in a public project migrating from a WordPress theme, I realized that modifying the PHP structure that had piled up over the years actually slowed down new feature development. Every small layout change required a heavy development environment reload and was prone to plugin conflicts. The decision to separate the frontend and backend became inevitable. I chose Astro as the presentation layer that communicates directly with WordPress via the REST API, creating a more predictable and maintainable Astro headless foundation.

Initially, I assumed that environment variables would be uniformly available throughout the application, both during the build process and execution. However, official documentation confirms that import.meta.env is statically replaced at build time [2]. These variables will not change according to server conditions at runtime. For dynamic server-side rendering (SSR) needs, a fallback to process.env remains necessary so that the configuration does not fail when executed in a production environment. This understanding changed how I structured the connection configuration module to remain flexible.

Resilient REST Client Contract

The core of this migration lies in the src/lib/wp.ts file. This file defines a strict data contract through the WpPost, WpCategory, PostMeta, JsonLdNode, and RelatedPost interfaces. The JsonLdNode interface ensures that structured data for search engines remains valid, while RelatedPost facilitates internal navigation without complex additional queries. The WordPress REST API schema states that properties like title, content, and excerpt are objects containing rendered HTML, not plain strings [3]. Additionally, using the _embed parameter allows fetching taxonomy and featured media data in a single network request, reducing the number of required round-trips.

System resilience is tested through a clear degradation contract. Every data fetching function is designed to return a null value or an empty list when WordPress is unreachable or returns a 5xx status code. This function never throws an error into the page render process, so users still see an intact interface even if the latest data is unavailable. To prevent hanging requests from consuming resources, I implemented AbortSignal.timeout(10_000), which will automatically abort the fetch operation with a TimeoutError reason after the time limit is reached [4].

On the performance side, an in-process caching layer using a Map data structure with a 60-second time-to-live (TTL) is applied to reduce the load on the backend server. This implementation stores parsed responses based on route keys, ensuring that repeated requests within that time window never reach the WordPress server. The base URL configuration is taken from the WP_INTERNAL_URL environment variable. Optional authorization using Basic auth can be enabled via the WP_REST_USER and WP_REST_PASS variables. Credentials only live in environment variables and are never written into the code.

Rendering Mode Split per Route

Astro prerenders pages by default at build time, producing highly fast static HTML files. However, on-demand rendering can be enabled per route by declaring export const prerender = false, which requires an appropriate server adapter configuration [1]. This strategy allows for a logical split of rendering modes based on data characteristics and update frequency.

The index and kategori/[slug] pages are processed via prerendering because their content is relatively static and benefits from initial access speed and CDN efficiency. Conversely, the post/[slug] and search routes are rendered on-demand (SSR) to handle data dynamics, recent comments, or specific user queries that cannot be predicted at build time.

Search engine optimization needs are met without relying on heavy traditional WordPress plugins. The custom pras/v1 endpoints provide meta and JSON-LD data that is fed directly into the SEO head component in the Base.astro file. This approach keeps structured data integrity intact at the frontend layer, ensuring search engines receive accurate signals.

This architectural separation proves that a single codebase can serve both static and dynamic needs simultaneously. The system continues to function gracefully even if the backend experiences temporary disruptions, providing the certainty that has long been missing in monolithic theme development.

Sumber [1] Astro Docs, On-demand rendering, https://docs.astro.build/en/guides/on-demand-rendering/ [2] Astro Docs, Using environment variables, https://docs.astro.build/en/guides/environment-variables/ [3] WordPress REST API Handbook, Posts, https://developer.wordpress.org/rest-api/reference/posts/ [4] MDN, AbortSignal: timeout() static method, https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/timeout_static

Sources

Related articles