-
Notifications
You must be signed in to change notification settings - Fork 31
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
Partials RFC #721
Partials RFC #721
Conversation
Can you elaborate on the meaning of this line from the Non-Goals section? My use case is that I want to include client side scripts defined by my fragments, and I'm unclear on if this will/won't be supported. |
I am interested in fragments coming from the micro-frontend perspective: include a fragment either server-side/edge-side via SSI/ESI or include a fragment client-side via Ajax. I started a roadmap discussion about these use cases at #713 The proposal solves some use cases: notably the HTMX/Hotwire-style approach where the server updates the markup for parts of the page. I want to think one step further about potential future micro-frontend Ajax support. If Astro were to support – in the future – that use case of delivering a fragment to a page rendered by another server, it would need to support styles and scripts for the fragment. |
@ZebraFlesh No, all I mean by that is that Astro is not opinionated about how you load a fragment in the client and are not getting involved in that. So Astro is not going to be adding scripts of its own. You can of course use your own scripts to manage fragment insertion. |
@philipschm1tt Scripts and styles are included without any config value, by default, for all pages. So if you want those things then you don't need this feature. Am I misunderstanding something here? |
If I understand the proposal correctly, with a fragment, you would not get any stylesheet or script links as part of the fragment, but you would rely on the page that includes the fragment (where Astro presumably already provided styles and scripts for that page). What I'm saying is that there is a fragment use case, where the page that includes the fragment does not come from Astro but from a different server. For that use case, it is common that the fragment that you include also contains a stylesheet and script. For that micro-frontend Ajax approach, I imagine the ideal behavior like this: Here is an excerpt from the book Micro-frontends in Action (with a manning account, you can "unscramble" it for a few minutes): https://livebook.manning.com/book/micro-frontends-in-action/chapter-3/ Their fragment (in this example without JS) looks like this – it includes a stylesheet link in the fragment:
Just to reiterate: I'm not arguing for perfectly solving micro-frontend scenarios as part of this RFC but to consider in this RFC what the impact might be if you addressed the micro-frontend use cases in the future. |
@philipschm1tt I understand, what i'm saying is that the thing you are describing is not a fragment (in the definition of this RFC). It's just a regular page. You might think of it as a fragment, from the perspective of how you're using it within another framework, but Astro's perspective a page which includes styles is not a fragment. Does that make sense? What I'm saying is, you should be able to create a page with just the content in your example and it will include styles, as you have shown. Without needing this feature. |
Yes, I understand what use case(s) you want to solve with this proposal.
These server-side integration use cases (like the Ajax client-side integration approach), still need styles and behavior as part of these kinds of fragment. The fragments in these contexts come from different servers. |
@philipschm1tt Can you explain why the existing pages folder behavior does not work for your use-case? |
I have not personally explored this as much as the commenters in #266 and some of my colleagues. What I'm looking for is a response like
So roughly the same behavior as for a page but without doctype and with the CSS/JS links for that "page" outside of a head tag. And unlike some of the commenters on that issue, I would like to use SSR and could not use some of the post-processing workarounds that were suggested for static files. |
@philipschm1tt If you don't include a The only part that is different is that there will also be a doctype, which you say that you do not want. I would then ask, how are you dealing with these scripts and styles? Are you not extracting them out and putting them into the head? If you do not, you will get FOUC. |
I am inlining those styles for the microfrontend use case. Today, I do the following:
And now I have a fragment. (I could skip the inlining, but since I'm doing ESI processing at edge on a streaming response, I can't "go back" and put the CSS in the head tag, because that content has already been streamed to the client.) My hope for this feature was removing the last 3 steps because Astro would be smart enough to not add them. |
Astro does not add a By (1) do you mean you take the link tags from the HTML and move them into the head? |
This feature can be tried out by installing the preview release: npm install astro@next--fragments |
Yes. I think that's why that was the focus of #266
For the Ajax case the fragment is requested client-side on user interaction, so you won't have the styles and scripts during page load. For the server-side integration case, this may depend on the specific approach you use. I think this is probably a classic SSI/ESI challenge with some options and trade-offs. |
Excellent point. It's been a while since I've looked at this problem, so I was quoting from out post-processing script, but this explanation makes sense.
No, I mean we find each |
Consider renaming this feature to partials |
@MrHBS definitely open to doing that. Should probably run a pole some where. |
I've renamed it to Partials in this RFC. I'm working to also update the code. Additionally I've added a section to the RFC describing why the proposal doesn't include scripts/styles: https://github.com/withastro/roadmap/blob/fragments/proposals/partials.md#including-head-content-but-not-doctype |
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 would be great if we could explain the result with an example. While it's explained that other tools have used partials, some people wouldn't know how they should be used. I am one of them.
I would like a few lines that say if a page opt-ins partial = true
, what's the result and what should the user expect from that page?
@ematipico Added that in 47ab4ba, let me know if that's what you were looking for! |
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.
Thank you, @matthewp, for addressing my comment. Happy with the RFC!
I'm moving for a call for consensus on this RFC. This will be the final comment period (3 days); if there are no objections this will be merged and the feature can be released in a future release (likely 3.4) |
I think my biggest concern is with the variable vs file convention. Having it as a variable could clash with existing pages, third party components/packages and it also makes it more difficult to know whether a certain page is used as a partial or not since you have to investigate all of the pages individually. Hugo handles partials by requiring them to be within a specific directory which is a little more elegant (albeit more work as mentioned in the RFC). Any file nested under |
Just to provide some flavor, my plan is to use this implementation to build only partials (no actual pages). Think of it as: a build for a set of ESI replacements. Provided that's still possible with a directory-based approach, that looks good from the consumer side. |
@silent1mezzo I will probably use a
|
That's fair
Won't adding the
Hmm, I actually disagree with this. I think the variable is more confusing than a folder. At best they're the same and you need to go to the docs to figure it out. At least the folder has examples from other frameworks people could lean on.
Totally fair |
No, exporting variables is not supported by Astro component outside of the documented ones (
A better explanation for my point here is that in Astro you can create any URL structure you want, and if we give special meaning to pages/partials that means that you cannot create a page with the URL Conversely we would be enforcing a URL structure for partials themselves. Some people might want to do RESTful URLs where |
Makes sense |
Just wanted to comment I find the name of the feature unclear (though I understand how the name was arrived at). Partials is already a fairly well used terminology to describe reusable templates or "views" particularly in MVC-like frameworks. These partials have little in common with this RFC. Essentially partials in this context are components. As a new user, I would be initially confused. Partial views in ASP.NET Core As the unit of abstraction in Astro is the component (not partials), and as I understand it the motivation for this RFC is for endpoints, perhaps Fragment Pages or Page Fragments is a better feature name? |
The section Including head content but not doctype lists a cons of injecting scripts/styles directly mid-page and concludes that it's never the right approach to include scripts/styles in the partial. In my case, I am working with an old PHP site with spaghetti code, which I'd like to gradually transition to Astro. During the transition, my plan is to have the Astro partials injected in the HTML. Developing code to manage both the PHP assets and Astro assets together is somewhere between not-worth-it and impossible. On the other hand, I can make sure I develop the Astro components in a manner that repeated injection of styles/scripts would not be an issue. If this RFC goes ahead as-is, I'll have to keep setting As there are many use cases for partials, it might be good to have the option to configure separately:
What do you think? |
@PeterDraex I'd recommend stripping out the doctype using middleware for your case. It should be very simple to write. We don't want a feature that immediate breaks down once you use a partial more than once. |
@lloydjatkinson thanks for the reference! We original did call it fragments but switched to partials after some discussion. Your points are very valid! Going to discuss it with the core team and see what they think. |
I would prefer "partials" over "fragments" in this case. "Partials" is not uncommon (also used for things like SCSS) and not strictly about re-using – just says that it's only a part of the markup. That usage is not far off from some of the linked examples: the MVC frameworks often amount to parts of only the markup – the content of an Astro partial and something like a Java JSP "partial" would not be big. By contrast, "fragment" is increasingly popular in the micro-frontend context (see https://engineering.zalando.com/posts/2018/12/front-end-micro-services.html or https://blog.cloudflare.com/better-micro-frontends/ ). Astro does not support that approach. The word refers more to the "outcome" of a piece of the rendered site, which also "implies" the behavior within that "fragment" and not just the markup. An alternative term that would work: "html fragment". |
We are going to stick with partials, but title it as Page Partials in our documentation and communication, to specify that this is about partials that are accessible via a URL. Naming is hard and there's no perfect name. In the future if this seems like a mistake we can always rename, so this is not a blocking sort of decision. This RFC has gone through the final comment period, I appreciate everyone's feedback! |
Summary
Add the ability to configure a page in the
pages
directory to be treated as a fragment. A fragment is a chunk of HTML that is not interpreted to be a page, so no head content ordoctype
are included.Links