feat(core)!: unctx
fallback context
#437
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
π Linked issue
#375, #362, #281
β Type of 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 fromunhead
with unctx.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.π Features
unheadCtx
tryUseUnhead()
useUnhead()
getActiveHead()
useHead*()
composables without a context available will throw an error instead of silently failingπ Checklist