-
Notifications
You must be signed in to change notification settings - Fork 52
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
When charset is set inside the SEO component, it's placed after the title tag #91
Comments
@ttmc Thank you for this great report and thorough engagement with the issue. This is greatly appreciated and I think I agree that the best move here might be to deprecate the attribute. Another possibility would be to run some JS that would always put the tag as the first child of the head. What's your opinion on that? |
@jonasmerlin You wrote:
Yes, that might be a better solution. I was reluctant to suggest it because it's a bit weird for a component to be reaching outside its boundaries and adding a tag elsewhere in the document. Maybe also check if there's already a charset declaration before doing anything... |
If you do decide to use JavaScript to check for an existing charset declaration, and to inject one at the top of the head if necessary, you will have to be very careful to make sure that JavaScript code runs at the right time. If you run it at build time (inside the The JavaScript should run after-building but before-deploying the generated HTML documents. Good news: Astro integrations can do that, see https://docs.astro.build/en/reference/integrations-reference ... I've never done that but I guess you can look at existing integrations like |
@ttmc Thank you for your insights on this, these are very helpful! I wanted to sit down to implement this today, but I don't think I have enough time to turn this into an integration and see if this works. Might I ask if you would be willing to give this a try and then do a PR for this? |
I was thinking more about what it would take to insert the charset declaration after-the-fact, into a fully-built website (i.e. all generated HTML files) and realized that wow, this is nuts, way beyond what the user of an SEO component would expect it to do. Maybe the easiest solution is the following:
---
import { SEO } from "astro-seo";
---
<html lang="en">
<head>
<!-- (DELETE THIS COMMENT) Please note that the SEO component will insert
a meta charset declaration first (with default value UTF-8) and
a meta viewport declaration second; you don't need to do that yourself. -->
<SEO
title="A Very Descriptive Title"
... (Also, make sure the default charset is "UTF-8". Also I would have the default meta viewport declaration as I think these changes are much more straightforward to make, and they don't cause the SEO component to do anything surprising. |
I don't really like the idea of using client-side js to place I'm starting to think the best option would be NOT to support charset (warn if used) and let the user insert the meta charset where necessary. |
Regarding the SEO library for Astro, while the charset and viewport metadata is crucial for rendering, it's not directly tied to SEO. Let's prioritize optimizing SEO elements, including interesting meta tags and maintain best practices for usability. |
I think I found a nice solution: just tell users of " Among other things, |
These days, a typical HTML5 file will begin something like:
The meta charset tag is almost always the first tag inside the head tag, because the HTML5 specification suggests placing the charset declaration within the first 1024 bytes of the document.
Placing the charset declaration first clarifies the character encoding for the entire document, including the title, ensuring proper interpretation of characters. While modern browsers are robust, in rare cases, placing the title before the charset might lead to rendering issues, especially if the title includes non-ASCII characters.
The SEO component provided by astro-seo lets one set the value of charset. If one does that, the generated HTML has the title tag before the charset declaration. That is an issue.
Thoughts on How to Fix this Issue
The first thing you might be tempted to do is put the meta charset tag first, before the title tag. The problem with that is that there might be lots of other tags before the SEO component, inside the head tag. The SEO component has no control over those tags. They're put there by the user of the SEO component, not by the SEO component itself.
The final generated HTML should have the charset declaration first (inside the head tag), yet the SEO component can't do that.
Moreover, the charset declaration isn't really an SEO thing anyhow. It's more like the meta viewport tag.
Therefore, my conclusion is that the SEO component provided by astro-seo should be changed to no longer accept a value for charset, and it should no longer try to set it. Anyone who tries to use the SEO component to set charset should be told it's being deprecated and they should just set it manually, first thing inside the head tag. Then in the future, the charset prop could be removed from the SEO component completely.
The text was updated successfully, but these errors were encountered: