Skip to content
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

feat(core)!: unctx fallback context #437

Merged
merged 11 commits into from
Jan 7, 2025
Merged

feat(core)!: unctx fallback context #437

merged 11 commits into from
Jan 7, 2025

Conversation

harlan-zw
Copy link
Collaborator

@harlan-zw harlan-zw commented Jan 6, 2025

πŸ”— Linked issue

#375, #362, #281

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

When using a framework like Vue or React we can inject the Unhead instance to the render context, this works great and allows us to access to add tags in nested components. However, for meta frameworks where we may want to call these functions outside of a direct render, such as in plugins or middleware we need to rely on an alternative context source.

This PR replaces the fallback SSR implicit shared context used when using any useHead*() composables from unhead with unctx.

import { useHead } from 'unhead'

// Where does unhead get its context? 
// Previously this would used a shared global instance
useHead({ title: 'foo' })

The previous shared context architecture was dodgy and was leading to potential cross-request leakage of Unhead tags when SSR. For Vue, this issue was patched in a hacky way without breaking changes. For vanilla Unhead this was still a present issue and it was assumed the end user would handle this themselves if / when it became a problem.

Additionally, having an exported let activeHead was causing issues with tree-shaking so this PR should improve the package weight when bundling.

As this PR includes breaking changes related to the setup of Unhead, it's recommended to follow the updated setup guides provided for higher level frameworks (coming soon).

SSR Setup Guide

Unhead provides several useHead*() composables which allow you to interact with Unhead without having to keep a reference of the main instance.

When using Unhead client-side, we don't need to worry about context, but as Unhead is for SSR head tags, we do to avoid cross-request instance sharing.

When setting up Unhead for SSR in a vanilla JS app, you should use the unheadCtx function to manage the context state.

import { createHead, unheadCtx } from 'unhead'

const head = createHead()

unheadCtx.set(head)
// -> render your app
unheadCtx.unset()

πŸš€ Features

  • unheadCtx
  • tryUseUnhead()
  • useUnhead()

⚠️ Breaking Changes

  • Removes getActiveHead()
  • Using any useHead*() composables without a context available will throw an error instead of silently failing

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@harlan-zw harlan-zw changed the title feat!: @unhead/context feat!: unctx context, remove getActiveHead() Jan 6, 2025
@harlan-zw harlan-zw changed the title feat!: unctx context, remove getActiveHead() feat!: unctx context, remove getActiveHead() Jan 6, 2025
@harlan-zw harlan-zw changed the title feat!: unctx context, remove getActiveHead() feat!: unctx context, removes getActiveHead() Jan 6, 2025
@harlan-zw harlan-zw changed the title feat!: unctx context, removes getActiveHead() feat!: unctx context, remove getActiveHead() Jan 6, 2025
@harlan-zw harlan-zw added the v2 label Jan 6, 2025
@harlan-zw harlan-zw changed the title feat!: unctx context, remove getActiveHead() feat!: unctx fallback context, remove getActiveHead() Jan 6, 2025
@harlan-zw harlan-zw changed the title feat!: unctx fallback context, remove getActiveHead() feat!: unctx fallback context Jan 6, 2025
@harlan-zw harlan-zw mentioned this pull request Jan 6, 2025
16 tasks
@harlan-zw harlan-zw changed the title feat!: unctx fallback context feat(core)!: unctx fallback context Jan 7, 2025
@harlan-zw harlan-zw merged commit eb6ff33 into main Jan 7, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant