#DOMParser Server Side
1 messages · Page 1 of 1 (latest)
Hi @summer bobcat. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
The error you're encountering is expected behavior in Astro. Web APIs like DOMParser are not available in server-side code, including the frontmatter of Astro components.
In Astro v3.0, partial shims for Web APIs in server code were completely removed. As mentioned in the documentation:
Astro v3.0 removes these partial shims entirely. Web APIs are no longer available in server-rendered code.
Upgrade to Astro v3
This is similar to the error you might see with other browser-specific objects:
This error occurs when trying to access
documentorwindowon the server.
Troubleshooting Guide
Solutions
You have a few options to work with DOMParser:
-
Move the code to a client-side script: Place your
DOMParsercode in a<script>tag outside the frontmatter. This will run on the client whereDOMParseris available. -
Use the
client:onlydirective: If you need to useDOMParserin a component, you can use theclient:onlydirective to ensure it only runs on the client. -
Use a server-compatible alternative: For parsing HTML on the server, you could use a Node.js compatible package like
jsdomornode-html-parserinstead ofDOMParser. -
Fetch and parse remote Markdown: If you're trying to parse Markdown content, consider using a dedicated Markdown parser as shown in the documentation:
import { marked } from 'marked';
const response = await fetch('<https://example.com/some-markdown.md'>);
const markdown = await response.text();
const content = marked.parse(markdown);
Remember that Astro components run on the server by default, so browser-specific APIs won't be available in the component frontmatter.