Container API: render components in isolation #462
Replies: 18 comments 39 replies
-
I assume this will only work if the consumer has vite-plugin-astro applied? Is that Vite plugin already built to be able to work independently of the Astro CLI, our would that need work too? |
Beta Was this translation helpful? Give feedback.
-
Is the return value of const content = await astro.renderToString(Component, { props, slots, request, params })
document.getElementById('root').innerHtml = content; ? Have you considered a more friendly API for rendering to the DOM, by taking in a DOM element that it would render straight too? await astro.renderToDOM(
Component,
{ props, slots, request, params },
document.getElementById('root')
) This might just be syntax sugar for the above, so it might not be too valuable. I don't know if there are special cases where it needs to be handled differently, that |
Beta Was this translation helpful? Give feedback.
-
This might be a stupid question, but Astro components are inherently static right? They don't need to be reactive to props changes, because they only ever run on build? So if the props changes, it would be valid to just throw away the whole DOM tree and render a new string, we wouldn't need to special case it with keeping the Astro component state but with updated props? (I hope you don't mind I'm spamming multiple messages, I think each question deserves its own thread) |
Beta Was this translation helpful? Give feedback.
-
I like it overall. I even like this just as an incremental improvement over internal APIs that we use for testing, even if this was just internal. I think the Goals section could use some more use-cases. Testing is one that comes to mind for me, I don't know if this idea at all relates to the previous testing APIs that were discussed. |
Beta Was this translation helpful? Give feedback.
-
I'm thinking about components using the Astro global, and how that would work in an isolated context. You already talk about it in the description, describing the second parameter. Would that allow all globals to be stubbed out, or only the subset described? |
Beta Was this translation helpful? Give feedback.
-
If this means I could maybe have some Astro components pre-rendered in a reMARK plugin and then injected through the pipeline for a plain MD file and not only MDX then this would be amazing! |
Beta Was this translation helpful? Give feedback.
-
What can we expect in terms of components that import components from other frameworks, like React? Will that work out of the box? will the be interactive/hydrated or not? Or will it crash, or noop? Is that what you are referring to here:
|
Beta Was this translation helpful? Give feedback.
-
Could we possibly build on this and create a spec for Astro components (of course, it'll have to be in a seperate RFC)? |
Beta Was this translation helpful? Give feedback.
-
Hold up would this allow React, Vue, etc... components to import Astro components? And would those components then be able to also import other React, Vue, etc... components through the Astro component? Cause if true, we might just have a complete reveloutinary complete cross framework solution right here |
Beta Was this translation helpful? Give feedback.
-
Is there a difference between mode: |
Beta Was this translation helpful? Give feedback.
-
I tested this and apart from the issue that someone noted (that scripts and styles are not set, so I just get html with a lot of "astro-XXX" class names and no styles for those 😅) I also wasn't able to make it work with new document collection api. I.e.
I really like the idea behind this API, but it might be too simplistic :) My proposal would be something along these lines:
|
Beta Was this translation helpful? Give feedback.
-
Really like this proposal overall! It feels aligned with the |
Beta Was this translation helpful? Give feedback.
-
If this would let us render pages from requests other than GET that would help immensely. |
Beta Was this translation helpful? Give feedback.
-
+1 on this - we've been looking at "Components as a Service" for working with headless content and Edge-Side Includes - instead of a page renderer. I did a POC with it but because it's a page renderer it still needs all the possible available components. With this way we could instead render fragments on the fly instead in a function as we don't care about state. |
Beta Was this translation helpful? Give feedback.
-
🤔 This would also allow SPA-like navigation by generating API routes that can independently serve the page content without the layout Still the issue with scripts not being unloaded after the navigation, but for simple docs sites and alike it would prob be enough. |
Beta Was this translation helpful? Give feedback.
-
There's one part that is unclear to me, and the examples unfortunately don't help my brain. If the APIs are meant to render astro files in isolation and we're outside the astro pipeline, that means that import Foo from "Foo.astro" Unless there's another pipeline - vite, webpack, etc - under the hood. Am I wrong to assume that? |
Beta Was this translation helpful? Give feedback.
-
Would be nice to be able to just pass in the Astro global object if you have one. I have a use case for rendering components to string inside the context of an Astro page request. |
Beta Was this translation helpful? Give feedback.
-
This proposal has been accepted and moved into a Stage 2 proposal: #533 Thank you everyone for sharing your feedback on this first draft! |
Beta Was this translation helpful? Give feedback.
-
Body
Summary
Astro components are tightly coupled to
astro
(the metaframework). This proposal introduces a possible server-side API for rendering.astro
files in isolation, outside of the fullastro
build pipeline.Background & Motivation
Some of our own proposals, as well as many third-party tool integrations, are blocked until we expose a proper server-side rendering API for
.astro
components. Other frameworks have tools likepreact-render-to-string
orreact-dom/server
that make rendering components on the server straight-forward.We've avoided doing this because... it's very hard! Part of the appeal of
.astro
components is that they have immediate access to a number of powerful APIs, context about your site, and a rich set of framework renderers. In the fullastro
build process, we are able to wire up all this context invisibly. Wiring up the context manually as a third-party is prohibitively complex.Goals
.astro
components in isolationastro
parity, but abstract away internal APIs.astro
files on the server.astro
component output.mdx
compiledContent/html output?@astrojs/*
) if possibleExample
The container can be optionally constructed with some settings that are typically reserved for Astro configuration.
The second argument to
renderToString
optionally provides render-specific data that may be exposed through the Astro global, likeprops
,slots
,request
, and/orparams
.See
feat/container
for an exploration of implementation.Beta Was this translation helpful? Give feedback.
All reactions