unjs / unhead

Unhead is the any-framework document head manager built for performance and delightful developer experience.
https://unhead.unjs.io
MIT License
547 stars 36 forks source link

Documentation on deduplication #205

Open 719media opened 10 months ago

719media commented 10 months ago

Describe the feature

Based on remarks in https://github.com/unjs/unhead/issues/165 You mention To clarify, sorting the tags is the step before deduplication However, in https://unhead.unjs.io/usage/guides/sorting, it says Once tags are [deduped](https://unhead.unjs.io/guide/guides/handling-duplicates), they will be sorted

I'm not sure which is true, but it would be nice to clarify and update as needed. I've created a PR to at least fix the broken link: https://github.com/unjs/unhead/pull/204/files

Thanks!

Additional information

harlan-zw commented 10 months ago

Hey, sorry my comment on the other issue wasn't correct.

Tag weight is calculated and used as part of the deduping process. The tags position with ctx.tags is only modified during the sort step which happens afterwards.

The logic can probably be simplified, it does seems like there's a gap between sorting with before: and after: only occurring after the deduping step, so it won't always work.

Are you still having issues with https://github.com/unjs/unhead/issues/165?

719media commented 10 months ago

Just trying to ensure I understand the behavior. It seems that tagPriority DOES have an effect on deduplicates. I created a stackblitz here: https://stackblitz.com/edit/vitejs-vite-umdqwq?file=src%2FApp.vue,src%2Fcomponents%2FTemplate1.vue,src%2Fcomponents%2FTemplate2.vue&terminal=dev

You can see that changing the value of titleTagPriority for template 2 (even though it comes later and is "changed" later) doesn't take effect, because titleTagPriority for template 1 is higher. This is desired behavior, from my perspective, but just hoping to see this behavior well documented.

Now, where it gets unexpected is if you change the priority of "appTitleTagPriority" to 0, because, at least according to https://unhead.unjs.io/usage/guides/sorting, 0 (and negative) values are valid, and should properly take priority. AND THEN if you make the following changes: const appTitleTagPriority = ref(1); const template1TitleTagPriority = ref(2); const template2TitleTagPriority = ref(3);

Then it DOES work. So ya, I could dig a little more, but because the documentation isn't really defined here, hard to know what the expected behavior is.

Once this is figured out, then you add in the additional caveat of (https://github.com/unjs/unhead/issues/179), and it would just be nice to get really clear documentation/expectation around this behavior.

For example, complex scenario considering #179 could be 1) head title tag defined "outside" of unhead (in index.html) as "Initial!" 2) app useHead defines title tag as null, tagPriority 0 3) template useHead defines title tag as "Hi!", tagPriority 1 (default)

Depending on if null means "passthru" vs. "remove", you either would wind up with "Initial!" or no title tag at all.

Anyway, perhaps we can start with solidifying the documentation/expected behavior around tagPriority and deduplication?

harlan-zw commented 9 months ago

Thank you for the detailed investigation!

Definitely, some improvements to be made to the documentation

Good catch, the null title with a separate priority is a unique edge case. I think this is something that can be considered a bug and should be fixed.