A way to not use <!DOCTYPE html> #266
Replies: 16 comments 39 replies
-
I'd also like this; I want to write an XSLT stylesheet to transform my RSS and Atom feeds into human-friendly versions, and I'd like to reuse my Astro components in the stylesheet. |
Beta Was this translation helpful? Give feedback.
-
I'm using Astro to generate parts of a page that are then used as a Server Side Include into a larger template. Having the appear halfway through the document isn't ideal. |
Beta Was this translation helpful? Give feedback.
-
I’ve solved it for a static build, where we produce regular pages plus some fragment files meant for inclusion. We used a If I recall correctly, Astro already has a duplicate-doctype-correction built-in (that is activated when the doctype is encountered but is different from |
Beta Was this translation helpful? Give feedback.
-
Hey y'all! There was a bit more movement on this in withastro/astro#6084, but we decided to bounce it back to this discussion since it's a larger change coordinated across our compiler and I totally agree that the ability to generate fragments would be very useful, but enabling this would be a breaking change. Do we use a config setting, some file-based pattern like a |
Beta Was this translation helpful? Give feedback.
-
A file name With our use case we do potentially have a curveball in the form of sitemaps. We have a folder structure like:
Both export default defineConfig({
...
integrations: [react(), sitemap({
serialize(item) {
if (item.url.endsWith(".head")) return undefined;
return getSiteMapEntry(cliConfig.site, item);
}
})],
...
}); I'm unsure if sitemaps have been considered so far in regards to fragments? I'd be keen to hear to thoughts regarding this. Arguably this isn't the correct approach as when using fragments there's actually something else at play which ultimately decides which URLs and pages get included where and it should be that thing that generates the sitemaps. But it sure is handy having it in Astro :) |
Beta Was this translation helpful? Give feedback.
-
For our use case — a micro frontends architecture that allows teams to deploy full pages as well as fragments meant to be used for page composition (e.g. header, footer, various reusable sections and interactive components) — I would still argue that a configuration option, such as the proposed
All that being said, the IMHO the choice comes down to whether Astro wants to endorse using fragments as a design pattern in the same way that component composition is the default today (my vote goes to component composition). Adding a config that allows for disabling the default inclusion of doctype is more subtle than introducing a new filename convention. For our use case, fragments are used to enable composition between modules that are built using different technologies, and not for internal consumption within the same module. (composition with fragments is handled with ESI – Edge Side Includes). |
Beta Was this translation helpful? Give feedback.
-
I have wanted to use fragments with astro, but I came across more issues than doctype. So if you're using them, I'm curious if you
|
Beta Was this translation helpful? Give feedback.
-
One other important thing I just ran into -- with no I'm going to include an empty |
Beta Was this translation helpful? Give feedback.
-
Curious how well this middleware works for you, it removes the doctype (the first 16 bytes) from the response if the request had the header export async function onRequest ({ request }: { request: Request }, next : () => Promise<Response>) {
const response = await next()
if (
response.ok &&
response.headers.get('Content-Type') === 'text/html' &&
request.headers.get('X-Accept') === 'fragment'
) {
const sourceReader = response.body!.getReader()
let removedDoctype = false
const fragmentResponseBody = new ReadableStream<Uint8Array>({
async pull(controller) {
const { done, value } = await sourceReader.read()
if (done) controller.close()
else if (removedDoctype) controller.enqueue(value)
else controller.enqueue(value!.slice(16))
removedDoctype = true
}
})
return new Response(fragmentResponseBody, {
status : response.status,
statusText : response.statusText,
headers : response.headers,
})
}
} and you would fetch fragments like so: const response = await fetch('/', { headers: { 'X-Accept': 'fragment' } }) |
Beta Was this translation helpful? Give feedback.
-
To keep it simple I created a new proposal based on @michrome's reply (#266 (reply in thread)): |
Beta Was this translation helpful? Give feedback.
-
Until #588 is implemented here is a simple workaround using an Astro middleware: export const onRequest = async (context, next) => {
const response = await next()
return new Response(
response.text ?
(
await response.text()
)
.replace('<!DOCTYPE html>', '')
:
response.body
,
response
)
} In an Astro component we can use a <Fragment set:html={'<!DOCTYPE HTML>'}/> |
Beta Was this translation helpful? Give feedback.
-
I suggested middleware access to the matched route module in #555 (comment) but it didn't gain any traction I'm on the outside looking in, but digging deeper it looks like all the invocations of callMiddleware already have |
Beta Was this translation helpful? Give feedback.
-
Eventually I preferred using (static) Astro (page) (file) endpoints (instead of (Astro) components): |
Beta Was this translation helpful? Give feedback.
-
Random idea I had about this, not sure if good or not: if a page ends with We could still provide the scripts/links via a Header. It would require that the frontend making the request does something with the header. I looked into htmx and I'm not sure that they have an API for providing styles/scripts in this way. |
Beta Was this translation helpful? Give feedback.
-
This feature has been accepted to Stage 2 and will be worked on by someone on the core team over the next few months. Please see the Stage 2 proposal here; a Stage 3 proposal is in the works which will include the API design. |
Beta Was this translation helpful? Give feedback.
-
Would it be possible to enable opting out of all generated tags like
or something |
Beta Was this translation helpful? Give feedback.
-
I'll like a way to stop the doctype from appearing on the page at the top of the response. 😺
https://github.com/reggi/my-astro/blob/main/src/pages/index.astro
Here I'm using an astro file as the endpoint and the component route 2 in one. I'd like to return just json when there's a POST request.
Update (10/17/2022) by @FredKSchott: For the exact use-case above, you can now use Endpoints to return JSON and non-HTML content.
Beta Was this translation helpful? Give feedback.
All reactions