Repository navigation
Replies: 2 comments
|
This is one of the biggest misses in Astro IMO. Writing an endpoint, being able to list pages, but not render any of their content is very annoying. Even inside a component, trying to render say a summary of page content for use in SEO metatags is almost impossible without resorting to using a separate Astro component to re-render the slot, convert the HTML to text then truncate it (which doesn't quite work due to some other issues). The embedded stylesheets / JS is an interesting one, so perhaps much like |
|
This is covered by #533, so closing this discussion. Thank you for the suggestion! For further feedback, please post there. |
Uh oh!
There was an error while loading. Please reload this page.
astro.renderToStringmethod?Β astro#4372 Astro + Sanity Portable Text - Render Astro component to string?Β astro#5494Summary
Expose a method to render an astro component to a string.
Example
and/or
Background & Motivation
Most of the use cases of rendering components are covered with the features of Astro sfc's. However, there are cases where you'd want to 'break out' of the sfc, and render an Astro component to a string.
I'll show two use cases; one within an Astro component, and one within an endpoint.
Within an Astro component
Sanity CMS has a custom format for rich text called Portable Text. To render this they provide a couple of packages, of which I'll be using
@portabletext/to-html.The gist of Portable Text is that it's an array of
blocksthat render differently based on their properties. There are preconfigured block types (egh1,h2,p), but it's also possible to define custom blocks, which you'd render like this:Within an endpoint
Unresolved Questions
I do not have a clear idea yet about what this means for CSS and JS included in the component that's rendered to string. My first intuition is to include the CSS and JS in the string, but I'm not sure if that's the best approach.
Another option could be to return the HTML, CSS and JS separately, but that would complicate the solutions to both the provided use cases above.
I'd love to hear your thoughts on this.
All reactions