-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
e4377dc
commit 91e744f
Showing
5 changed files
with
84 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
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,10 @@ | ||
import * as React from 'react'; | ||
import HeadManagerProvider from './head-manager-provider'; | ||
|
||
export const HeadManagerDecorator = (Story: React.FC): React.ReactNode => { | ||
return ( | ||
<HeadManagerProvider> | ||
<Story /> | ||
</HeadManagerProvider> | ||
); | ||
}; |
22 changes: 22 additions & 0 deletions
22
code/frameworks/nextjs/src/head-manager/head-manager-provider.tsx
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,22 @@ | ||
import React from 'react'; | ||
import { HeadManagerContext } from 'next/dist/shared/lib/head-manager-context'; | ||
import initHeadManager from 'next/dist/client/head-manager'; | ||
|
||
type HeadManagerValue = { | ||
updateHead?: ((state: JSX.Element[]) => void) | undefined; | ||
mountedInstances?: Set<unknown>; | ||
updateScripts?: ((state: any) => void) | undefined; | ||
scripts?: any; | ||
getIsSsr?: () => boolean; | ||
appDir?: boolean | undefined; | ||
nonce?: string | undefined; | ||
}; | ||
|
||
const HeadManagerProvider: React.FC = ({ children }) => { | ||
const headManager: HeadManagerValue = initHeadManager(); | ||
headManager.getIsSsr = () => false; | ||
|
||
return <HeadManagerContext.Provider value={headManager}>{children}</HeadManagerContext.Provider>; | ||
}; | ||
|
||
export default HeadManagerProvider; |
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
34 changes: 34 additions & 0 deletions
34
code/frameworks/nextjs/template/stories_default-js/Head.stories.jsx
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,34 @@ | ||
/* eslint-disable no-undef */ | ||
import { expect } from '@storybook/jest'; | ||
import Head from 'next/head'; | ||
import React from 'react'; | ||
import { within, userEvent, waitFor } from '@storybook/testing-library'; | ||
|
||
function Component() { | ||
return ( | ||
<div> | ||
<Head> | ||
<title>Next.js Head Title</title> | ||
<meta property="og:title" content="My page title" key="title" /> | ||
</Head> | ||
<Head> | ||
<meta property="og:title" content="My new title" key="title" /> | ||
</Head> | ||
<p>Hello world!</p> | ||
</div> | ||
); | ||
} | ||
|
||
export default { | ||
component: Component, | ||
}; | ||
|
||
export const Default = { | ||
play: async ({ canvasElement }) => { | ||
await waitFor(() => expect(document.title).toEqual('Next.js Head Title')); | ||
await expect(document.querySelectorAll('meta[property="og:title"]')).toHaveLength(1); | ||
await expect(document.querySelector('meta[property="og:title"]').content).toEqual( | ||
'My new title' | ||
); | ||
}, | ||
}; |