Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(v2): simplify blog metadata to minimize number of request #1908

Merged
merged 3 commits into from
Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG-2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Changed the way we read the `USE_SSH` env variable during deployment to be the same as in v1.
- Add highlight specific lines in code blocks.
- Fix accessing `docs/` or `/docs/xxxx` that does not match any existing doc page should return 404 (Not found) page, not blank page.
- Simplify blog metadata. Previously, accessing `/blog/post-xxx` will request for next and prev blog post metadata too aside from target post metadata. We should only request target post metadata.

## 2.0.0-alpha.31

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ describe('loadBlog', () => {
},
);
const {blogPosts} = await plugin.loadContent();
const noDateSource = path.join('@site', pluginPath, 'no date.md');
const noDateSourceBirthTime = (await fs.stat(
noDateSource.replace('@site', siteDir),
)).birthtime;
const noDatePermalink = `/blog/${noDateSourceBirthTime
.toISOString()
.substr(0, '2019-01-01'.length)
.replace(/-/g, '/')}/no date`;

expect(
blogPosts.find(v => v.metadata.title === 'date-matter').metadata,
Expand All @@ -41,6 +49,14 @@ describe('loadBlog', () => {
description: `date inside front matter`,
date: new Date('2019-01-01'),
tags: [],
nextItem: {
permalink: '/blog/2018/12/14/Happy-First-Birthday-Slash',
title: 'Happy 1st Birthday Slash!',
},
prevItem: {
permalink: noDatePermalink,
title: 'no date',
},
});
expect(
blogPosts.find(v => v.metadata.title === 'Happy 1st Birthday Slash!')
Expand All @@ -56,24 +72,25 @@ describe('loadBlog', () => {
description: `pattern name`,
date: new Date('2018-12-14'),
tags: [],
prevItem: {
permalink: '/blog/2019/01/01/date-matter',
title: 'date-matter',
},
});

const noDateSource = path.join('@site', pluginPath, 'no date.md');
const noDateSourceBirthTime = (await fs.stat(
noDateSource.replace('@site', siteDir),
)).birthtime;
expect(
blogPosts.find(v => v.metadata.title === 'no date').metadata,
).toEqual({
permalink: `/blog/${noDateSourceBirthTime
.toISOString()
.substr(0, '2019-01-01'.length)
.replace(/-/g, '/')}/no date`,
permalink: noDatePermalink,
source: noDateSource,
title: 'no date',
description: `no date`,
date: noDateSourceBirthTime,
tags: [],
nextItem: {
permalink: '/blog/2019/01/01/date-matter',
title: 'date-matter',
},
});
});
});
29 changes: 21 additions & 8 deletions packages/docusaurus-plugin-content-blog/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
import {
LoadContext,
PluginContentLoadedActions,
RouteModule,
ConfigureWebpackUtils,
} from '@docusaurus/types';
import {Configuration} from 'webpack';
Expand Down Expand Up @@ -138,6 +137,25 @@ export default function pluginContentBlog(
(a, b) => b.metadata.date.getTime() - a.metadata.date.getTime(),
);

// Colocate next and prev metadata
blogPosts.forEach((blogPost, index) => {
const prevItem = index > 0 ? blogPosts[index - 1] : null;
if (prevItem) {
blogPost.metadata.prevItem = {
title: prevItem.metadata.title,
permalink: prevItem.metadata.permalink,
};
}
const nextItem =
index < blogPosts.length - 1 ? blogPosts[index + 1] : null;
if (nextItem) {
blogPost.metadata.nextItem = {
title: nextItem.metadata.title,
permalink: nextItem.metadata.permalink,
};
}
});

// Blog pagination routes.
// Example: `/blog`, `/blog/page/1`, `/blog/page/2`
const totalCount = blogPosts.length;
Expand Down Expand Up @@ -267,10 +285,7 @@ export default function pluginContentBlog(
}),
);

blogItems.forEach((blogItem, index) => {
const prevItem = index > 0 ? blogItems[index - 1] : null;
const nextItem =
index < blogItems.length - 1 ? blogItems[index + 1] : null;
blogItems.map(blogItem => {
const {metadata, metadataPath} = blogItem;
const {source, permalink} = metadata;

Expand All @@ -281,9 +296,7 @@ export default function pluginContentBlog(
modules: {
content: source,
metadata: aliasedSource(metadataPath),
prevItem: prevItem && aliasedSource(prevItem.metadataPath),
nextItem: nextItem && aliasedSource(nextItem.metadataPath),
} as RouteModule,
},
});
});

Expand Down
7 changes: 7 additions & 0 deletions packages/docusaurus-plugin-content-blog/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ export interface MetaData {
date: Date;
tags: (Tag | string)[];
title: string;
prevItem?: Paginator;
nextItem?: Paginator;
}

export interface Paginator {
title: string;
permalink: string;
}

export interface Tag {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import BlogPostItem from '@theme/BlogPostItem';
import BlogPostPaginator from '@theme/BlogPostPaginator';

function BlogPostPage(props) {
const {content: BlogPostContents, metadata, nextItem, prevItem} = props;
const {content: BlogPostContents, metadata} = props;
const {frontMatter} = BlogPostContents;
return (
<Layout title={metadata.title} description={metadata.description}>
Expand All @@ -24,7 +24,10 @@ function BlogPostPage(props) {
<BlogPostContents />
</BlogPostItem>
<div className="margin-vert--xl">
<BlogPostPaginator nextItem={nextItem} prevItem={prevItem} />
<BlogPostPaginator
nextItem={metadata.nextItem}
prevItem={metadata.prevItem}
/>
</div>
</div>
</div>
Expand Down