-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4275962
commit 182be6f
Showing
9 changed files
with
259 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@headstartwp/core": minor | ||
--- | ||
|
||
Introducing the `useSeo` hook. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
slug: /data-fetching/useSeo | ||
sidebar_position: 7 | ||
--- | ||
|
||
# The useSeo hook | ||
|
||
:::info | ||
This hook was introduced in `@headstartwp/[email protected]` | ||
:::info | ||
|
||
The `useSeo` hook returns the SEO data for the current page. | ||
|
||
## Basic Usage | ||
|
||
```javascript | ||
// by default it returns the json object | ||
const yoast_json = useSeo(); | ||
const yoast_json = useSeo('json'); | ||
// but you can also get the plain html markup for the metadata | ||
const yoast_head = useSeo('html'); | ||
``` | ||
|
||
If there's no seo information for the current route, this hook will return `null`. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { DataFetchingProvider } from '@headstartwp/core/react'; | ||
import { renderHook } from '@testing-library/react'; | ||
import * as React from 'react'; | ||
import { useSeo } from '../useSeo'; | ||
|
||
describe('useSeo', () => { | ||
it('returns seo data', () => { | ||
const wrapper = ({ children }) => { | ||
return ( | ||
<DataFetchingProvider | ||
swrConfig={{}} | ||
data={{ | ||
'@seo': { | ||
yoast_head_json: { title: 'test' }, | ||
yoast_head: '<meta name="title" value="test" />', | ||
}, | ||
}} | ||
> | ||
{children} | ||
</DataFetchingProvider> | ||
); | ||
}; | ||
|
||
const { result } = renderHook(() => useSeo(), { | ||
wrapper, | ||
}); | ||
|
||
expect(result.current).toMatchObject({ title: 'test' }); | ||
|
||
const { result: result2 } = renderHook(() => useSeo('html'), { | ||
wrapper, | ||
}); | ||
|
||
expect(result2.current).toBe('<meta name="title" value="test" />'); | ||
|
||
const { result: result3 } = renderHook(() => useSeo('json'), { | ||
wrapper, | ||
}); | ||
|
||
expect(result3.current).toMatchObject({ title: 'test' }); | ||
}); | ||
|
||
it('returns null if seo data is not set', () => { | ||
const wrapper = ({ children }) => { | ||
return ( | ||
<DataFetchingProvider swrConfig={{}} data={{}}> | ||
{children} | ||
</DataFetchingProvider> | ||
); | ||
}; | ||
|
||
const { result } = renderHook(() => useSeo(), { | ||
wrapper, | ||
}); | ||
|
||
expect(result.current).toBeNull(); | ||
|
||
const { result: result2 } = renderHook(() => useSeo('html'), { | ||
wrapper, | ||
}); | ||
|
||
expect(result2.current).toBeNull(); | ||
|
||
const { result: result3 } = renderHook(() => useSeo('json'), { | ||
wrapper, | ||
}); | ||
|
||
expect(result3.current).toBeNull(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { useSWRConfig } from '@headstartwp/core/react'; | ||
|
||
export const seoKey = '@seo'; | ||
|
||
/** | ||
* The useSeo hook. Returns the current SEO object | ||
* | ||
* ## Usage | ||
* | ||
* ```tsx | ||
* const seo = useSeo(); | ||
* ``` | ||
* | ||
* @param format how to return the seo object. Defaults to 'json' | ||
* | ||
* @category Data Fetching Hooks | ||
*/ | ||
function useSeo(format: 'json'): Record<string, any> | null; | ||
function useSeo(format: 'html'): string | null; | ||
function useSeo(): Record<string, any> | null; | ||
function useSeo(format?: 'json' | 'html') { | ||
const { fallback } = useSWRConfig(); | ||
|
||
if (typeof fallback[seoKey] === 'undefined') { | ||
return null; | ||
} | ||
|
||
if (format === 'json' || typeof format === 'undefined') { | ||
return fallback[seoKey]?.yoast_head_json as Record<string, any>; | ||
} | ||
|
||
return fallback[seoKey]?.yoast_head as string; | ||
} | ||
|
||
export { useSeo }; |