Skip to content

Commit

Permalink
lazy load code highlight and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
phartenfeller committed Feb 16, 2024
1 parent 46e92a8 commit d0b7746
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 22 deletions.
6 changes: 3 additions & 3 deletions src/components/blog/BlogImagePopup.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React, { Fragment, Suspense } from 'react';
import React, { lazy, Fragment, Suspense } from 'react';
import { Dialog, Transition } from '@headlessui/react';
import useImagePreview from '../../state/useImagePreview';

const TransformWrapper = React.lazy(() =>
const TransformWrapper = lazy(() =>
import('react-zoom-pan-pinch').then((module) => ({
default: module.TransformWrapper,
}))
);
const TransformComponent = React.lazy(() =>
const TransformComponent = lazy(() =>
import('react-zoom-pan-pinch').then((module) => ({
default: module.TransformComponent,
}))
Expand Down
54 changes: 44 additions & 10 deletions src/components/blog/SyntaxHighlighter.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
import PropTypes from 'prop-types';
import React from 'react';
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import React, { Suspense, lazy } from 'react';
// import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import { oneDark } from 'react-syntax-highlighter/dist/esm/styles/prism';
import waitMs from '../../util/waitMs';

const SyntaxHighlighter = lazy(async () => {
await waitMs(0.2 * 1000);
return import('react-syntax-highlighter').then((module) => ({
default: module.Prism,
}));
});

// const SyntaxHighlighter = lazy(async () => {
// await waitMs(1.5 * 1000);
// const obj = import('react-syntax-highlighter');
// return { default: obj.Prism };
// });

const SyntaxH = ({ lang, code }) => {
let l;
Expand All @@ -28,15 +42,35 @@ const SyntaxH = ({ lang, code }) => {
}

return (
<SyntaxHighlighter
language={l}
style={oneDark}
showLineNumbers
// wrapLongLines
// lineProps={{ style: { wordBreak: 'break-all', whiteSpace: 'pre-wrap' } }}
<Suspense
fallback={
<pre
className="loading-code"
style={{
fontFamily: `"Fira Code", "Fira Mono", Menlo, Consolas, "DejaVu Sans Mono", monospace`,
fontSize: '14px',
lineHeight: '22px',
margin: '10px 0',
backgroundColor: 'rgb(40, 44, 52)',
color: 'rgb(171, 178, 191)',
backgroundClip: 'border-box',
padding: '12px',
}}
>
{code}
</pre>
}
>
{code}
</SyntaxHighlighter>
<SyntaxHighlighter
language={l}
style={oneDark}
showLineNumbers
// wrapLongLines
// lineProps={{ style: { wordBreak: 'break-all', whiteSpace: 'pre-wrap' } }}
>
{code}
</SyntaxHighlighter>
</Suspense>
);
};

Expand Down
2 changes: 1 addition & 1 deletion src/styles/blog.css
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
@apply text-xs md:text-sm;
}

.code-figure pre {
.code-figure pre:not(.loading-code) {
line-height: 1 !important;
padding: 8px !important;
margin: 0 !important;
Expand Down
20 changes: 12 additions & 8 deletions src/templates/blog-page-template.jsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import { ArrowUpIcon, ClockIcon, RssIcon } from '@heroicons/react/outline';
import { MDXProvider } from '@mdx-js/react';
import { graphql, Link } from 'gatsby';
import { Link, graphql } from 'gatsby';
import { GatsbyImage } from 'gatsby-plugin-image';
import { MDXRenderer } from 'gatsby-plugin-mdx';
import PropTypes from 'prop-types';
import React from 'react';
import { ArrowUpIcon, ClockIcon, RssIcon } from '@heroicons/react/outline';
import React, { Suspense } from 'react';
import LinkButton from '../components/LinkButton';
import AuthorShowcase from '../components/blog/AuthorShowcase';
import getComponents from '../components/blog/blogComponents';
import BlogImagePopup from '../components/blog/BlogImagePopup';
import Comments from '../components/blog/Comments';
import OtherPosts from '../components/blog/OtherPosts';
import ScrollTracker from '../components/blog/ScrollTracker';
import TagsDisplay from '../components/blog/TagsDisplay';
import BlogAPEXSidebar from '../components/blog/blogAPEXSidebar';
import getComponents from '../components/blog/blogComponents';
import Layout from '../components/layout';
import LinkButton from '../components/LinkButton';
import SEO from '../components/seo';
import '../styles/blog.css';
import BlogAPEXSidebar from '../components/blog/blogAPEXSidebar';
import lazyLoad from '../util/lazy';

const Comments = lazyLoad(() => import('../components/blog/Comments'), 1);

export const query = graphql`
query ($id: String!, $relativeDirectory: String!) {
Expand Down Expand Up @@ -251,7 +253,9 @@ const BlogPageTemplate = ({ data }) => {
Comments
</h2>

<Comments ghCommentsIssueId={ghCommentsIssueId} />
<Suspense fallback={<div>Loading...</div>}>
<Comments ghCommentsIssueId={ghCommentsIssueId} />
</Suspense>
</>
)}
</div>
Expand Down
12 changes: 12 additions & 0 deletions src/util/lazy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { lazy } from 'react';
import waitMs from './waitMs';

function lazyLoad(componentImportFn, delaySeconds = 0) {
return lazy(async () => {
await waitMs(delaySeconds * 1000);
const obj = await componentImportFn();
return typeof obj.default === 'function' ? obj : obj.default;
});
}

export default lazyLoad;
7 changes: 7 additions & 0 deletions src/util/waitMs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function waitMs(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}

export default waitMs;

0 comments on commit d0b7746

Please sign in to comment.