Skip to content

Commit

Permalink
feat(core)!: unctx fallback context (#437)
Browse files Browse the repository at this point in the history
* feat!: Unhead Context

* chore: prefer exports from `unhead`

* fix: set context client-side

* fix: non-union returns

* chore: include updated tests

* chore: sync lock

* chore: migrate

* chore: sync lock
  • Loading branch information
harlan-zw authored Jan 7, 2025
1 parent b5ba59d commit eb6ff33
Show file tree
Hide file tree
Showing 126 changed files with 1,135 additions and 1,259 deletions.
1 change: 0 additions & 1 deletion docs/content/1.setup/2.ssr/0.installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ For this you will need the `@unhead/ssr` dependency.
import { renderSSRHead } from '@unhead/ssr'

// head is from createHead()
// if you need access to it you can also use getActiveHead()
const payload = await renderSSRHead(head)
```

Expand Down
22 changes: 0 additions & 22 deletions docs/content/api/0.core/get-active-head.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { InferSeoMetaPlugin } from '@unhead/addons'
import { renderSSRHead } from '@unhead/ssr'
import { createHead } from 'unhead'
import { describe, it } from 'vitest'
import { InferSeoMetaPlugin } from '../src/plugins/inferSeoMetaPlugin'

describe('inferSeoMetaPlugin', () => {
it('simple', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { parse } from 'acorn-loose'
import { describe, expect, it } from 'vitest'

import { TreeshakeServerComposables } from '../../packages/addons/src/unplugin/TreeshakeServerComposables'
import { TreeshakeServerComposables } from '../src/unplugin/TreeshakeServerComposables'

async function transform(code: string | string[], id = 'some-id.js') {
const plugin = TreeshakeServerComposables.vite() as any
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { parse } from 'acorn-loose'
import { describe, expect, it } from 'vitest'

import { UseSeoMetaTransform } from '../../packages/addons/src/unplugin/UseSeoMetaTransform'
import { UseSeoMetaTransform } from '../src/unplugin/UseSeoMetaTransform'

async function transform(code: string | string[], id = 'some-id.js') {
const plugin = UseSeoMetaTransform.vite() as any
Expand Down
2 changes: 1 addition & 1 deletion packages/schema-org/src/nodes/AggregateOffer/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest'
import { defineAggregateOffer, useSchemaOrg } from '../..'
import { injectSchemaOrg, useSetup } from '../../../.test'
import { injectSchemaOrg, useSetup } from '../../../test'

describe('defineAggregateOffer', () => {
it('can be registered simple', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/schema-org/src/nodes/Article/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Article } from '../..'
import { describe, expect, it } from 'vitest'
import { defineArticle, defineOrganization, defineWebPage, useSchemaOrg } from '../..'
import { findNode, injectSchemaOrg, useSetup } from '../../../.test'
import { findNode, injectSchemaOrg, useSetup } from '../../../test'

const mockDate = new Date(Date.UTC(2021, 10, 10, 10, 10, 10, 0))

Expand Down
2 changes: 1 addition & 1 deletion packages/schema-org/src/nodes/Book/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'vitest'
import { defineBook, defineBookEdition, definePerson, useSchemaOrg } from '../..'
import { injectSchemaOrg, useSetup } from '../../../.test'
import { injectSchemaOrg, useSetup } from '../../../test'

describe('defineBook', () => {
it('can be registered', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/schema-org/src/nodes/Breadcrumb/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'vitest'
import { defineBreadcrumb, useSchemaOrg } from '../../'
import { injectSchemaOrg, useSetup } from '../../../.test'
import { injectSchemaOrg, useSetup } from '../../../test'

describe('defineBreadcrumb', async () => {
it('can be registered', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/schema-org/src/nodes/Comment/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'vitest'
import { defineComment, useSchemaOrg } from '../../'
import { injectSchemaOrg, useSetup } from '../../../.test'
import { injectSchemaOrg, useSetup } from '../../../test'

describe('defineComment', () => {
it('can be registered', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/schema-org/src/nodes/Course/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'vitest'
import { defineCourse, useSchemaOrg } from '../../'
import { injectSchemaOrg, useSetup } from '../../../.test'
import { injectSchemaOrg, useSetup } from '../../../test'

describe('defineCourse', () => {
it('can be registered', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/schema-org/src/nodes/Event/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest'
import { defineEvent, defineOrganization, useSchemaOrg } from '../../'
import { injectSchemaOrg, useSetup } from '../../../.test'
import { injectSchemaOrg, useSetup } from '../../../test'

describe('defineEvent', () => {
it('can be registered', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest'
import { defineFoodEstablishment, useSchemaOrg } from '../..'
import { injectSchemaOrg, useSetup } from '../../../.test'
import { injectSchemaOrg, useSetup } from '../../../test'

describe('defineLocalBusiness', () => {
it('can be registered', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/schema-org/src/nodes/HowTo/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'vitest'
import { defineHowTo, useSchemaOrg } from '../../'
import { injectSchemaOrg, useSetup } from '../../../.test'
import { injectSchemaOrg, useSetup } from '../../../test'

describe('defineHowTo', () => {
it('can be registered', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/schema-org/src/nodes/Image/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'vitest'
import { defineImage, useSchemaOrg } from '../../'
import { injectSchemaOrg, useSetup } from '../../../.test'
import { injectSchemaOrg, useSetup } from '../../../test'

describe('defineImage', () => {
it('can be registered', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/schema-org/src/nodes/ItemList/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest'
import { defineItemList, defineListItem, defineMovie, useSchemaOrg } from '../..'
import { injectSchemaOrg, useSetup } from '../../../.test'
import { injectSchemaOrg, useSetup } from '../../../test'

describe('defineItemList', () => {
it('movie example', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/schema-org/src/nodes/JobPosting/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'vitest'
import { defineJobPosting, useSchemaOrg } from '../..'
import { injectSchemaOrg, useSetup } from '../../../.test'
import { injectSchemaOrg, useSetup } from '../../../test'

describe('defineJobPosting', () => {
it('can be registered', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/schema-org/src/nodes/LocalBusiness/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest'
import { defineLocalBusiness, useSchemaOrg } from '../../'
import { injectSchemaOrg, useSetup } from '../../../.test'
import { injectSchemaOrg, useSetup } from '../../../test'

describe('defineLocalBusiness', () => {
it('can be registered', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/schema-org/src/nodes/Movie/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'vitest'
import { defineMovie, useSchemaOrg } from '../../'
import { injectSchemaOrg, useSetup } from '../../../.test'
import { injectSchemaOrg, useSetup } from '../../../test'

describe('defineMovie', () => {
it('can be defined', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/schema-org/src/nodes/Organization/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'vitest'
import { defineOrganization, useSchemaOrg } from '../../'
import { injectSchemaOrg, useSetup } from '../../../.test'
import { injectSchemaOrg, useSetup } from '../../../test'

describe('defineOrganization', () => {
it('can be registered', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/schema-org/src/nodes/Person/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Article } from '../Article'
import { describe, expect, it } from 'vitest'
import { defineArticle, defineOrganization, definePerson, useSchemaOrg } from '../../'
import { findNode, injectSchemaOrg, useSetup } from '../../../.test'
import { findNode, injectSchemaOrg, useSetup } from '../../../test'
import { PrimaryArticleId } from '../Article'

describe('definePerson', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/schema-org/src/nodes/Product/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { WebSite } from '../WebSite'
import { expect } from 'vitest'
import { defineAggregateRating, definePerson, defineProduct, defineWebSite, useSchemaOrg } from '../../'
import { findNode, injectSchemaOrg, useSetup } from '../../../.test'
import { findNode, injectSchemaOrg, useSetup } from '../../../test'
import { IdentityId, idReference } from '../../utils'
import { PrimaryWebSiteId } from '../WebSite'

Expand Down
2 changes: 1 addition & 1 deletion packages/schema-org/src/nodes/Question/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest'
import { defineQuestion, defineWebPage, useSchemaOrg } from '../../'
import { injectSchemaOrg, useSetup } from '../../../.test'
import { injectSchemaOrg, useSetup } from '../../../test'

describe('defineQuestion', () => {
it('can be registered', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/schema-org/src/nodes/Recipe/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'vitest'
import { defineRecipe, useSchemaOrg } from '../../'
import { injectSchemaOrg, useSetup } from '../../../.test'
import { injectSchemaOrg, useSetup } from '../../../test'

describe('defineRecipe', () => {
it('can be defined', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/schema-org/src/nodes/SoftwareApp/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'vitest'
import { defineSoftwareApp, useSchemaOrg } from '../../'
import { injectSchemaOrg, useSetup } from '../../../.test'
import { injectSchemaOrg, useSetup } from '../../../test'

describe('defineSoftwareApp', () => {
it('can be defined', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/schema-org/src/nodes/Video/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'vitest'
import { defineVideo, useSchemaOrg } from '../../'
import { injectSchemaOrg, useSetup } from '../../../.test'
import { injectSchemaOrg, useSetup } from '../../../test'

describe('defineVideo', () => {
it('can be registered', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/schema-org/src/nodes/WebPage/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { WebPage } from './index'
import { expect } from 'vitest'
import { defineImage, defineOrganization, defineReadAction, defineWebPage, defineWebSite, useSchemaOrg } from '../../'
import { findNode, injectSchemaOrg, useSetup } from '../../../.test'
import { findNode, injectSchemaOrg, useSetup } from '../../../test'
import { IdentityId, idReference, prefixId } from '../../utils'
import { PrimaryWebSiteId } from '../WebSite'
import { PrimaryWebPageId } from './index'
Expand Down
2 changes: 1 addition & 1 deletion packages/schema-org/src/nodes/WebSite/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { WebSite } from './index'
import { expect } from 'vitest'
import { defineOrganization, definePerson, defineSearchAction, defineWebPage, defineWebSite, useSchemaOrg } from '../../'
import { findNode, injectSchemaOrg, useSetup } from '../../../.test'
import { findNode, injectSchemaOrg, useSetup } from '../../../test'
import { IdentityId, idReference, prefixId } from '../../utils'
import { PrimaryWebSiteId } from './index'

Expand Down
8 changes: 3 additions & 5 deletions packages/schema-org/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import type {
WebSite,
} from './nodes'
import type { Arrayable, Thing } from './types'
import { getActiveHead, useHead } from 'unhead'
import { useHead, useUnhead } from 'unhead'
import { UnheadSchemaOrg } from './plugin'

function provideResolver<T>(input?: T, resolver?: string) {
Expand Down Expand Up @@ -160,11 +160,9 @@ export function defineBookEdition<T extends Record<string, any>>(input?: BookEdi

export type UseSchemaOrgInput = Arrayable<Thing | Record<string, any>>

export function useSchemaOrg(input: UseSchemaOrgInput, options?: Pick<HeadEntryOptions, 'tagDuplicateStrategy'>) {
export function useSchemaOrg(input: UseSchemaOrgInput, options?: HeadEntryOptions) {
// lazy initialise the plugin
const head = getActiveHead()
if (!head)
return
const head = options?.head || useUnhead()
if ((Array.isArray(input) && input.length === 0) || !input) {
return
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { renderDOMHead } from '@unhead/dom'
import { defineImage, defineOrganization, defineQuestion, defineWebPage, defineWebSite, SchemaOrgUnheadPlugin, useSchemaOrg } from '@unhead/schema-org'
import { renderSSRHead } from '@unhead/ssr'
import { createHead, useHead } from 'unhead'
import { useHead } from 'unhead'
import { describe, expect, it } from 'vitest'
import { useDom } from '../../fixtures'
import { useDom } from '../../../../test/fixtures'
import { createHeadWithContext } from '../../../../test/util'

describe('schema.org e2e', () => {
it('basic hydration', async () => {
const ssrHead = createHead({
const ssrHead = createHeadWithContext({
plugins: [
SchemaOrgUnheadPlugin(),
],
Expand Down Expand Up @@ -43,7 +44,7 @@ describe('schema.org e2e', () => {

const dom = useDom(data)

const csrHead = createHead()
const csrHead = createHeadWithContext()
await renderDOMHead(csrHead, { document: dom.window.document })
expect(dom.serialize()).toMatchInlineSnapshot(`
"<!DOCTYPE html><html><head>
Expand Down Expand Up @@ -72,7 +73,7 @@ describe('schema.org e2e', () => {
})

it('hierarchy', async () => {
const ssrHead = createHead({
const ssrHead = createHeadWithContext({
plugins: [
SchemaOrgUnheadPlugin({
path: '/about',
Expand Down Expand Up @@ -113,7 +114,7 @@ describe('schema.org e2e', () => {

const dom = useDom(data)

const csrHead = createHead()
const csrHead = createHeadWithContext()
await renderDOMHead(csrHead, { document: dom.window.document })
expect(dom.serialize()).toMatchInlineSnapshot(`
"<!DOCTYPE html><html><head>
Expand Down Expand Up @@ -145,7 +146,7 @@ describe('schema.org e2e', () => {
})

it('linking', async () => {
const ssrHead = createHead({
const ssrHead = createHeadWithContext({
plugins: [
SchemaOrgUnheadPlugin({
path: '/about',
Expand Down Expand Up @@ -192,7 +193,7 @@ describe('schema.org e2e', () => {
})

it('faq', async () => {
const ssrHead = createHead({
const ssrHead = createHeadWithContext({
plugins: [
SchemaOrgUnheadPlugin({
path: '/about',
Expand Down Expand Up @@ -258,7 +259,7 @@ describe('schema.org e2e', () => {
})

it('canonical', async () => {
const ssrHead = createHead({
const ssrHead = createHeadWithContext({
plugins: [
SchemaOrgUnheadPlugin(),
],
Expand Down Expand Up @@ -300,7 +301,7 @@ describe('schema.org e2e', () => {
})

it('empty', async () => {
const ssrHead = createHead({
const ssrHead = createHeadWithContext({
plugins: [
SchemaOrgUnheadPlugin(),
],
Expand All @@ -316,7 +317,7 @@ describe('schema.org e2e', () => {
expect(data.bodyTags).toMatchInlineSnapshot(`""`)
})
it('#441', async () => {
const ssrHead = createHead({
const ssrHead = createHeadWithContext({
plugins: [
SchemaOrgUnheadPlugin(),
],
Expand Down
Loading

0 comments on commit eb6ff33

Please sign in to comment.