Three Frontend Review Findings: Canonical, Cache, and JSON-LD
A frontend review of an Astro app surfaced a canonical that could bake a dev origin into HTML, an unbounded Map cache, and an XSS hole via unescaped JSON-LD.
TL;DR
A code review on an Astro frontend found three production-risk patterns: prerendered pages baking dev origins into canonical URLs, now fixed by sourcing from PRAS_FRONTEND_URL or omitting the tag. An unbounded Map cache in SSR got an LRU cap of 200 entries using delete-and-set recency. JSON-LD rendered with set:html risked XSS, fixed by replacing < with \u003c after JSON.stringify.
A code review round on the Astro frontend of a public project just returned three specific findings. Code that looked fine in the development environment turned out to hide mechanisms vulnerable to production configuration mistakes, memory leaks, and a security hole. Each finding is not a static warning but a pattern you can test and fix systematically.
Canonical URLs and the Risk of a Baked-In Dev Origin
The first implementation used Astro.url.href as the canonical value on every page. The problem shows up on prerendered pages such as the home page, category listings, or the 404 page. These pages are produced entirely at build time, where no HTTP request object exists. As a result, the origin of the development environment risks being permanently baked into the generated HTML. Environment variables in this framework are statically replaced at build time, not at runtime [2].
The fix is to take the origin explicitly from the PRAS_FRONTEND_URL environment variable. If that variable is missing at build time, the canonical link is omitted from the markup entirely. This aligns with official search engine guidance: if the canonical URL cannot be set in the HTML source, leaving it out is better than shipping a wrong value. The Astro.url.href fallback is only used as a runtime mechanism on pages that are truly rendered on the server (SSR) [9].
A Hard Limit for the Map Cache in SSR
The second finding sits in the src/lib/wp.ts module. The cache used a Map object storing one unique key per URL, including every variation of search query parameters. In a long-running SSR process serving varied traffic, this pattern lets the Map grow without bound. Uncontrolled growth ends in steadily rising memory consumption until the process is killed.
The fix caps the cache at 200 entries with an LRU (Least Recently Used) eviction scheme. A JavaScript Map natively preserves key insertion order, which makes eviction cheap: on a cache hit, delete the key and set it again (delete + set). That moves the entry's recency to the end of the iteration order. The oldest entry always sits in the first position and can be dropped safely when the cap is reached [8].
Escaping the Script Tag in JSON-LD
The third finding concerns structured data rendering. The layout renders JSON-LD through a script tag of type application/ld+json using the set:html directive. By design, that directive does not auto-escape the value it receives [6]. If post content from the CMS contains a closing script tag string, that content can break out of the JSON block and run as JavaScript in the browser. This is the classic Cross-Site Scripting (XSS) vector, where untrusted data is treated as executable code [5].
The fix is to escape manually after JSON.stringify: every < character is explicitly replaced with \u003c. That transformation guarantees no closing-tag sequence survives into the payload. The JSON-LD script block that Google Search reads for rich results stays intact without compromising script safety [7]. These three patterns are now the standard for every static frontend build that serves CMS content.
Sources
- [2] Astro Docs, Using environment variables (docs.astro.build)
- [5] OWASP, Cross Site Scripting Prevention Cheat Sheet (cheatsheetseries.owasp.org)
- [6] Astro Docs, Template directives reference: set:html (docs.astro.build)
- [7] Google Search Central, Intro to how structured data markup works (developers.google.com)
- [8] MDN, Map (developer.mozilla.org)
- [9] Google Search Central, How to specify a canonical with rel=canonical (developers.google.com)