Skip to content

Commit

Permalink
feat: added optional nonce prop to OdysseyCacheProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinGhadyani-Okta committed Nov 7, 2022
1 parent 52ec2de commit f264b50
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions packages/odyssey-react-mui/src/OdysseyCacheProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,34 @@
* See the License for the specific language governing permissions and limitations under the License.
*/

declare global {
interface Window {
cspNonce: string;
}
}

import createCache from "@emotion/cache";
import { CacheProvider } from "@emotion/react";
import { memo, ReactElement, useMemo } from "react";

import { useUniqueAlphabeticalId } from "./useUniqueAlphabeticalId";

const OdysseyCacheProvider = ({ children }: { children: ReactElement }) => {
const OdysseyCacheProvider = ({
children,
nonce,
}: {
children: ReactElement;
nonce?: string;
}) => {
const uniqueAlphabeticalId = useUniqueAlphabeticalId();

const emotionCache = useMemo(
() =>
createCache({
key: uniqueAlphabeticalId,
// @ts-expect-error ts(2345)
nonce: window.cspNonce,
nonce: nonce || window.cspNonce,
}),
[uniqueAlphabeticalId]
[nonce, uniqueAlphabeticalId]
);

return <CacheProvider value={emotionCache}>{children}</CacheProvider>;
Expand Down

0 comments on commit f264b50

Please sign in to comment.