Thanks to visit codestin.com
Credit goes to github.com

Skip to content

SEO tags prioritised below style tags #693

Description

@aethr

Environment

node: v22.22.1

tested on both:
unhead: v3.0.0-beta.12 (main)
unhead: v2.1.12

Reproduction

This test, added to unhead/test/unit/server/tagPriority.test.ts will currently fail:

  it('seo meta above inline style', async () => {
    const head = createServerHeadWithContext()
    head.push({
      style: [{
        textContent: 'body { background: red; }',
      }],
    })
    head.push({
      meta: [
        {
          property: 'og:title',
          content: 'test title',
        },
        {
          name: 'description',
          content: 'desc',
        },
      ],
    })

    const { headTags } = await renderSSRHead(head)
    expect(headTags).toMatchInlineSnapshot(`
      "<meta property="og:title" content="test title">
      <meta name="description" content="desc">
      <style>body { background: red; }</style>"
    `)
  })

Instead, the headTags are rendered in the order they were added:

<style>body { background: red; }</style>
<meta property="og:title" content="test title">
<meta name="description" content="desc">

Describe the bug

Many Open Graph crawlers will only parse the first 1MB of a document (for example, see Meta Web Crawlers documentation) looking for og meta tags. However, nuxt sites often inline <style> tags which can end up being quite large, often exceeding 1MB. When <meta property="og:..."> tags are not rendered before <style> tags in the header, this results in Open Graph previews breaking, and degraded SEO scoring.

Since this module is used by all nuxt sites to provide meta/SEO tags, in my opinion it would be good a good default to place Open Graph and meta tags above style tags by default, preventing this issue.

Additional context

This bug has been reported several times, some of which have been closed, although as far as I can tell the issue has never been resolved:

Automatic tagPriority is assigned to some <meta> tags, but only CSP, charset and viewport meta tags are included. While not as critical, I believe SEO meta tags such as description and Open Graph tags should always be prioritised above inline styles. The performance impact to CSS rendering should be negligible, but the impact for SEO and Open Graph seems like a clear net positive.

I've opened a related issue in nuxt-seo-utils for this in case this change would be too opinionated for unhead.

Edited: some minor corrections and link to nuxt-seo-utils issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions