Skip to content

Commit

Permalink
fix(webperf): add async on script mermaid (#1106)
Browse files Browse the repository at this point in the history
  • Loading branch information
fpasquet committed Apr 16, 2024
1 parent aff2882 commit 3ee93a4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 32 deletions.
8 changes: 0 additions & 8 deletions src/containers/PostPageContainer/PostPageContainer.tsx
@@ -1,4 +1,3 @@
import { useScript } from 'hoofd';
import React from 'react';
import { useLoaderData } from 'react-router-dom';

Expand All @@ -10,13 +9,6 @@ import { PostPageData } from '@/types';

export const PostPageContainer: React.FC = () => {
const postPageData = useLoaderData() as PostPageData;
useScript({
type: 'module',
text: [
`import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs';`,
'mermaid.initialize({ startOnLoad: true });',
].join('\n'),
});

if (!postPageData) {
return <NotFoundPageContainer />;
Expand Down
30 changes: 30 additions & 0 deletions src/entry-client.tsx
Expand Up @@ -35,3 +35,33 @@ if (searchPageContentContainer) {
</I18nextProvider>
);
}

const loadTwitterScript = (): void => {
const script = document.createElement('script');
script.src = 'https://platform.twitter.com/widgets.js';
document.body.appendChild(script);
};

const loadMermaidScript = (): void => {
const script = document.createElement('script');
script.async = true;
script.src = 'https://cdn.jsdelivr.net/npm/mermaid@10.9.0/dist/mermaid.min.js';
document.body.appendChild(script);
};

document.addEventListener('DOMContentLoaded', (event) => {
console.log(event);

const twitterTweetElements = document.getElementsByClassName('twitter-tweet');
const mermaidElements = document.getElementsByClassName('mermaid');

if (twitterTweetElements.length) {
console.log('twitterTweetElements', twitterTweetElements);
loadTwitterScript();
}

if (mermaidElements.length) {
console.log('mermaid', mermaidElements);
loadMermaidScript();
}
});
6 changes: 6 additions & 0 deletions src/helpers/markdownToHtmlHelper.tsx
Expand Up @@ -158,6 +158,12 @@ export const markdownToHtml = (content: string): string => {
},
});
},
script: ({ node, ...props }): React.JSX.Element | null => {
if (props.src === 'https://platform.twitter.com/widgets.js') {
return null;
}
return React.createElement('script', props);
},
},
})
.processSync(cleanMarkdown(content)).result;
Expand Down
25 changes: 1 addition & 24 deletions src/hooks/usePostPage.tsx
@@ -1,6 +1,5 @@
import { Box, PostPageProps } from '@eleven-labs/design-system';
import { useScript } from 'hoofd';
import React, { useEffect } from 'react';
import React from 'react';
import { useTranslation } from 'react-i18next';
import { useLocation } from 'react-router-dom';

Expand All @@ -20,28 +19,6 @@ export const usePostPage = (post: PostPageData): Omit<PostPageProps, 'variant' |
const location = useLocation();
const { getDateToString } = useDateToString();
useSeoPost(post);
useScript({
type: 'module',
text: [
`import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs';`,
'mermaid.initialize({ startOnLoad: true });',
].join('\n'),
});

useEffect(() => {
const script = document.createElement('script');
script.src = 'https://platform.twitter.com/widgets.js';
const twitterTweetElements = document.getElementsByClassName('twitter-tweet');
if (twitterTweetElements.length) {
twitterTweetElements[0].appendChild(script);
}

return () => {
if (twitterTweetElements.length) {
twitterTweetElements[0].removeChild(script);
}
};
}, []);

const contactCard = useContactCard();
const breadcrumb = useBreadcrumb({ categoryName: post.categories[0], withCategoryLink: true });
Expand Down

0 comments on commit 3ee93a4

Please sign in to comment.