Update individual snippet pages

Reduce data sizes and load times, improve performance
This commit is contained in:
Angelos Chalaris
2019-09-16 22:43:14 +03:00
parent e4569284eb
commit 31dee63aa3
3 changed files with 35 additions and 59 deletions

View File

@ -188,7 +188,7 @@ exports.createPages = ({ graphql, actions }) => {
const snippets = result.data.allSnippet.edges; const snippets = result.data.allSnippet.edges;
snippets.forEach(snippet => { snippets.forEach(snippet => {
if(!snippet.archived) { if (!snippet.node.archived) {
createPage({ createPage({
path: `/snippet${snippet.node.slug}`, path: `/snippet${snippet.node.slug}`,
component: snippetPage, component: snippetPage,
@ -210,7 +210,11 @@ exports.createPages = ({ graphql, actions }) => {
}); });
// Create tag pages. // Create tag pages.
const tags = [...new Set(snippets.map(snippet => snippet.tags.primary))].sort((a,b) => a.localeCompare(b)); const tags = [...new Set(
snippets.map(snippet => (snippet.tags || {primary: null}).primary)
)]
.filter(Boolean)
.sort((a, b) => a.localeCompare(b));
tags.forEach(tag => { tags.forEach(tag => {
const tagPath = `/tag/${toKebabCase(tag)}/`; const tagPath = `/tag/${toKebabCase(tag)}/`;

View File

@ -42,24 +42,18 @@ const CardCorner = ({ difficulty = 'intermediate' }) => (
// =================================================== // ===================================================
const FullCard = ({ snippetData, difficulty, isDarkMode }) => { const FullCard = ({ snippetData, difficulty, isDarkMode }) => {
const [examplesOpen, setExamplesOpen] = React.useState(false); const [examplesOpen, setExamplesOpen] = React.useState(false);
const tags = snippetData.tags;
let cardCodeHtml = `${optimizeAllNodes(
getCodeBlocks(snippetData.html).code,
)}`;
let cardExamplesHtml = `${optimizeAllNodes(
getCodeBlocks(snippetData.html).example,
)}`;
return ( return (
<div className='card'> <div className='card'>
<CardCorner difficulty={difficulty} /> <CardCorner difficulty={difficulty} />
<h4 className='card-title'>{snippetData.title}</h4> <h4 className='card-title'>{snippetData.title}</h4>
{tags.map(tag => ( {snippetData.tags.map(tag => (
<span className='tag' key={`tag_${tag}`}>{tag}</span> <span className='tag' key={`tag_${tag}`}>{tag}</span>
))} ))}
<div <div
className='card-description' className='card-description'
dangerouslySetInnerHTML={{ dangerouslySetInnerHTML={{
__html: `${getTextualContent(snippetData.html)}`, __html: snippetData.textHtml,
}} }}
/> />
<div className='card-bottom'> <div className='card-bottom'>
@ -83,12 +77,9 @@ const FullCard = ({ snippetData, difficulty, isDarkMode }) => {
aria-label='Copy to clipboard' aria-label='Copy to clipboard'
/> />
</CopyToClipboard> </CopyToClipboard>
{/* <button className="button button-b button-social-sh" aria-label="Share">
<ShareIcon />
</button> */}
<pre <pre
className={`card-code language-${config.language}`} className={`card-code language-${config.language}`}
dangerouslySetInnerHTML={{ __html: cardCodeHtml }} dangerouslySetInnerHTML={{ __html: snippetData.codeHtml }}
/> />
<button <button
className='button button-example-toggler' className='button button-example-toggler'
@ -99,7 +90,7 @@ const FullCard = ({ snippetData, difficulty, isDarkMode }) => {
{examplesOpen && ( {examplesOpen && (
<pre <pre
className='section card-examples language-js' className='section card-examples language-js'
dangerouslySetInnerHTML={{ __html: cardExamplesHtml }} dangerouslySetInnerHTML={{ __html: snippetData.exampleHtml }}
/> />
)} )}
</div> </div>

View File

@ -11,14 +11,11 @@ import BackArrowIcon from '../components/SVGs/BackArrowIcon';
// Individual snippet page template // Individual snippet page template
// =================================================== // ===================================================
const SnippetPage = props => { const SnippetPage = props => {
const post = props.data.markdownRemark; const snippet = props.data.snippet;
const postData = props.data.snippetDataJson.data.find(
v => v.title === post.frontmatter.title,
);
return ( return (
<> <>
<Meta title={post.frontmatter.title} description={post.excerpt} /> <Meta title={snippet.title} description={snippet.text.short} />
<Shell> <Shell>
<Link <Link
className='link-back' className='link-back'
@ -29,10 +26,13 @@ const SnippetPage = props => {
</Link> </Link>
<SnippetCard <SnippetCard
snippetData={{ snippetData={{
title: postData.title, title: snippet.title,
html: post.html, html: snippet.html.full,
code: postData.attributes.codeBlocks.code, codeHtml: snippet.html.code,
tags: postData.attributes.tags, exampleHtml: snippet.html.example,
textHtml: snippet.html.text,
code: snippet.code.src,
tags: snippet.tags.all,
}} }}
isDarkMode={props.isDarkMode} isDarkMode={props.isDarkMode}
/> />
@ -52,7 +52,7 @@ export default connect(
)(SnippetPage); )(SnippetPage);
export const pageQuery = graphql` export const pageQuery = graphql`
query BlogPostBySlug($slug: String!, $scope: String!) { query SnippetBySlug($slug: String!) {
logo: file(absolutePath: { regex: "/logo_reverse_md.png/" }) { logo: file(absolutePath: { regex: "/logo_reverse_md.png/" }) {
id id
childImageSharp { childImageSharp {
@ -61,42 +61,23 @@ export const pageQuery = graphql`
} }
} }
} }
allMarkdownRemark { snippet (slug: {eq: $slug }) {
edges { title
node { html {
fields { full
slug code
} example
fileAbsolutePath text
frontmatter {
title
}
}
} }
} code {
markdownRemark(fields: { slug: { eq: $slug } }) { src
id
fields {
slug
} }
excerpt(pruneLength: 160) tags {
html all
frontmatter {
title
} }
} title
snippetDataJson(meta: { type: { eq: "snippetArray" }, scope: {eq: $scope} }) { text {
data { short
title
id
attributes {
text
codeBlocks {
es6
example
}
tags
}
} }
} }
} }