Skip to content

Commit

Permalink
fix(unhead): increase all weights by 10x
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Sep 10, 2023
1 parent 3d422d6 commit 03a5c28
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 26 deletions.
24 changes: 13 additions & 11 deletions docs/content/1.usage/2.guides/2.sorting.md
Expand Up @@ -20,24 +20,26 @@ Sorting is done in multiple steps:

The following critical tags have their own default `tagPriority`:

- **-2** - `<meta charset ...>`
- **-1** - `<base>`
- **-20** - `<meta charset ...>`
- **-10** - `<base>`
- **0** - `<meta http-equiv="content-security-policy" ...>`
- **1** - `<title>`
- **2** - `<link rel="preconnect" ...>`
- **10** - `<title>`
- **20** - `<link rel="preconnect" ...>`

All other tags have a default priority of 10: &lt;meta&gt;, &lt;script&gt;, &lt;link&gt;, &lt;style&gt;, etc
All other tags have a default priority of 100: &lt;meta&gt;, &lt;script&gt;, &lt;link&gt;, &lt;style&gt;, etc

## `tagPriority`

You can also set custom priorities for tags using the `tagPriority` attribute.
You can set custom priorities for tags using the `tagPriority` attribute.

The following aliases exist:
- `tagPriority: 'critical'` - **2**
- `tagPriority: 'high'` - **9**
- `tagPriority: 'low'` - **11**
Providing a number will set the priority to that number. The lower the number, the higher the priority.

Otherwise, you can either provide a number or a string beginning with `before:` or `after:` and target a tag key.
You can also make use of a string alias that adjusts the priority by a relative amount:
- `critical` - **-20**
- `high` - **-90**
- `low` - **20**

Lastly you can set the priority based on the position of another tag using `before:` or `after:`. This is useful when you need to ensure a tag is rendered before or after another tag.

### Sort by number

Expand Down
16 changes: 8 additions & 8 deletions packages/shared/src/sort.ts
Expand Up @@ -2,33 +2,33 @@ import type { HeadTag } from '@unhead/schema'

export const TAG_WEIGHTS = {
// tags
base: -1,
title: 1,
base: -10,
title: 10,
} as const

export const TAG_ALIASES = {
// relative scores to their default values
critical: -8,
high: -1,
low: 2,
critical: -80,
high: -10,
low: 20,
} as const

export function tagWeight<T extends HeadTag>(tag: T) {
let weight = 10
let weight = 100
const priority = tag.tagPriority
if (typeof priority === 'number')
return priority
if (tag.tag === 'meta') {
// charset must come early in case there's non-utf8 characters in the HTML document
if (tag.props.charset)
weight = -2
weight = -20
// CSP needs to be as it effects the loading of assets
if (tag.props['http-equiv'] === 'content-security-policy')
weight = 0
}
else if (tag.tag === 'link' && tag.props.rel === 'preconnect') {
// preconnects should almost always come first
weight = 2
weight = 20
}
else if (tag.tag in TAG_WEIGHTS) {
weight = TAG_WEIGHTS[tag.tag as keyof typeof TAG_WEIGHTS]
Expand Down
14 changes: 7 additions & 7 deletions packages/unhead/src/optionalPlugins/capoPlugin.ts
Expand Up @@ -18,30 +18,30 @@ const importRe = /@import/
const isLink = tag.tag === 'link'
if (isScript && isTruthy(tag.props.async)) {
// ASYNC_SCRIPT
tag.tagPriority = 3
tag.tagPriority = 30
// SYNC_SCRIPT
}
else if (tag.tag === 'style' && tag.innerHTML && importRe.test(tag.innerHTML)) {
// IMPORTED_STYLES
tag.tagPriority = 4
tag.tagPriority = 40
}
else if (isScript && tag.props.src && !isTruthy(tag.props.defer) && !isTruthy(tag.props.async) && tag.props.type !== 'module' && !tag.props.type?.endsWith('json')) {
tag.tagPriority = 5
tag.tagPriority = 50
}
else if ((isLink && tag.props.rel === 'stylesheet') || tag.tag === 'style') {
// SYNC_STYLES
tag.tagPriority = 6
tag.tagPriority = 60
}
else if (isLink && ['preload', 'modulepreload'].includes(tag.props.rel)) {
// PRELOAD
tag.tagPriority = 7
tag.tagPriority = 70
}
else if (isScript && isTruthy(tag.props.defer) && tag.props.src && !isTruthy(tag.props.async)) {
// DEFER_SCRIPT
tag.tagPriority = 8
tag.tagPriority = 80
}
else if (isLink && ['prefetch', 'dns-prefetch', 'prerender'].includes(tag.props.rel)) {
tag.tagPriority = 9
tag.tagPriority = 90
}
}
options?.track && tags.push({
Expand Down

0 comments on commit 03a5c28

Please sign in to comment.