-
Notifications
You must be signed in to change notification settings - Fork 34
Container APIs #916
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Container APIs #916
Conversation
Co-authored-by: Matthew Phillips <matthew@skypack.dev>
Co-authored-by: Erika <3019731+Princesseuh@users.noreply.github.com>
Co-authored-by: Erika <3019731+Princesseuh@users.noreply.github.com>
Co-authored-by: Matthew Phillips <matthew@skypack.dev> Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
| } | ||
| ``` | ||
| The `astroConfig` object is literally the same object exposed by the `defineConfig`, inside the `astro.config.mjs` file. This very configuration |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you found use-case for passing the astroConfig yet? I'm worried about getting bug reports when people try to pass through vite config and other non-supported stuff.
What do you think about instead raising the relevant config values up to the AstroContainerOptions level. That would be stuff like trailingSlash.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about instead raising the relevant config values up to the AstroContainerOptions level. That would be stuff like trailingSlash.
I don't think that would work easily. These options, internally, are used to create a manifest. If we pass these options when rendering a component, it would mean generating a new manifest every time we attempt to render a component, and then discard that manifest. We have to be careful not to override the existing manifest, because the manifest is tight to the lifetime of the instance of the container.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not when rendering, AstroContainerOptions is the name of the options passed to AstroContainer.create().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I misunderstood what you meant. Yeah we should be able to provide the individual options.
d1a5400 to
a5e85d0
Compare
|
Can I use this in a Cloudflare project? It currently throws an error during build. I am using the MDX renderer specifically to render |
|
@universse it's hard to tell to give you an answer with this little information. Please use Discord. |
const { Content } = await entry.render()
const descriptionHtml = await astroContainer.renderToString(Content)I'm using Astro container to render Update: adding these to Astro config works for me. {
vite: {
ssr: {
external: ['astro/container', '@astrojs/mdx'],
},
},
}Update 2: when deployed, only pre-rendered page works. server-rendered page will give status 500. |
|
Will the proposed I’m wondering if it’ll be possible to create a custom framework on top of Astro, e.g. with one’s own pages setup; or say, rendering Astro as an endpoint alongside another Vite site. |
|
@alidcast If you run the container inside a Vite environment, then yes, all your |
|
Hi. I've been using the While script tags and scoped style tags appear to work correctly, the use of imported css modules will not be rendered via renderToString(), regardless if the existence of a head tag. This could be sometimes useful. For example when rendering html for injection into the content:encoded tag of RSS or copying strings into JS variables that only needs the rendered HTML. However, if rendering for the purposes of being placing on a page, the loss of styles is a showstopper. I didn't see any setting that controlled whether or not to inject module style tags in the container docs. |
|
This hasn't been implemented because we failed to find a valid use case. You just mentioned RSS, JS variables and such. Since we are evaluating a real use case, it would be amazing if you could go in detail and explain to me what you're looking for, and if you'd explain me very well you use case. |
|
@ematipico Scenario 1: Creating a page at
|
|
Yes @dwighthouse's post is excellent. You can see how we are forced to use |
|
I wanted to add my use case for accessing styles, I have a portfolio site with resume/CV component I've made and wish to display it on page differently than download versions only need component with styles while page has layout and supplies styles to show more pretty. |
|
I am working on porting my company's SDUI framework from mobile only to web. Astro looks like a great fit, as our SDUI framework is component based as well. SDUI controls the component tree (and properties). Each component can be mapped to an Unfortunately, without style support, it means we cannot style the components as they need to be. Each component, in isolation. The same is the case for scripts, as our SDUI framework has something called "handlers". These would be scoped to the component, in most cases. And so we would need to provide the scoped JS with the component when rendering. I am able to use the Container API to build the components from SDUI into Astro. But, without styles and scripts, the page isn't usable :-(. I would love to hear about the options to include styles and scripts. |
I have a discussion proposal for the Astro road map, |
|
It would be great if containers could be used in browser environments to enable things like live previews in CMSes. |
| import { getContainerRenderer as reactRenderer } from "@astrojs/react"; | ||
| import { getContainerRenderer as vueRenderer } from "@astrojs/vue"; | ||
| import { laodRenderers } from "astro:container"; | ||
| import { AstroContainer } from "astro/container"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's confusing having to import from both astro:container and astro/container. It might be nicer to re-export astro/container in the virtual astro:container module.
|
Running into this as well because I'm trying to render Markdown from my articles and grab the content to feed into an open graph description with cheerio. ---
import { getCollection } from "astro:content";
import Layout from "../../layouts/Layout.astro";
import og from "../../components/og";
import * as cheerio from 'cheerio';
export async function getStaticPaths() {
const writings = await getCollection("writings");
return writings.map((entry) => ({
params: { slug: entry.data.date.toString() },
props: { entry },
}));
}
const { entry } = Astro.props;
const { Content, remarkPluginFrontmatter } = await entry.render();
[...]
const content = cheerio.load(Content())
const description = content('p').toArray().map((el) => content(el).text()).join("\n")
---
<Layout title={entry.data.title}>
<meta property="og:description" content={description}>
[...]
</Layout> |
|
I want to share another possible use case: I'm currently working with email sending directly inside Astro API Endpoints, and I find it very useful to use Astro components as HTML templates. The However, I noticed that It would be great if either:
This would make Astro a very strong option for email templating use cases and take advantage of predefined variables and styles in layouts to maintain brand consistency across all email communications, without needing to duplicate markup or styling logic in separate tools. Thanks! |
|
What is going on? I don't like using experimental features! This one is important to me! |
|
Will all component features like nested components work? @ematipico I want to use renderToString like a templating library to render text in a ExpressJS not Astro app will that be possible? In this astroConfig is imported but it's not used in this code where will it go? |
For this use case, I think it might be best to use Astro in middleware mode with Express: https://docs.astro.build/en/guides/integrations-guide/node/#middleware You can’t import |
|
Why does container API renderToString have to depend on an Astro project? |
I also ran into this issue. My use-case is as follows: I want to build generated preview (open graph) images for blog posts using an HTML template and Puppeteer to take screenshots. So I need the full HTML with the styles to load into Puppeteer using |
Summary
An API for rendering components in isolation:
Links