Skip to content

Commit 3a81198

Browse files
committed
add preload link
1 parent b963eab commit 3a81198

13 files changed

+58
-21
lines changed

src/App.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import Posts from './Posts';
88
import Post from './Post';
99
import Comments from './Comments';
1010
import {onegraphAuth} from './Environment';
11-
import {Route, Link, Switch} from 'react-router-dom';
11+
import {Route, Switch} from 'react-router-dom';
12+
import Link from './PreloadLink';
1213
import idx from 'idx';
1314
import {NotificationContainer} from './Notifications';
1415
import OneGraphLogo from './oneGraphLogo';

src/Comments.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ function Comments({post, relay, postId}: Props) {
9393
return (
9494
<Box id="comments">
9595
{comments.map(comment => {
96-
return <Comment comment={comment} />;
96+
return <Comment key={comment.id} comment={comment} />;
9797
})}
9898
<PostBox>
9999
<Stack
@@ -163,6 +163,7 @@ export default createPaginationContainer(
163163
@connection(key: "Comments_post_comments") {
164164
edges {
165165
node {
166+
id
166167
...Comment_comment
167168
}
168169
}

src/Post.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ import EmojiIcon from './emojiIcon';
1414
import AddIcon from './addIcon';
1515
import Tippy, {TippyGroup} from '@tippy.js/react';
1616
import 'tippy.js/themes/light-border.css';
17-
import {Link} from 'react-router-dom';
18-
import {HashLink} from 'react-router-hash-link';
17+
import Link from './PreloadLink';
1918
import {postRootQuery} from './App';
2019
import GitHubLoginButton from './GitHubLoginButton';
2120
import {NotificationContext} from './Notifications';
@@ -371,9 +370,7 @@ const Post = ({relay, post}: Props) => {
371370
{formatDate(new Date(post.createdAt), 'MMM eo, yyyy')}
372371
</Text>
373372
<Text size="xsmall">
374-
<HashLink to={`/post/${post.number}#comments`}>
375-
view comments
376-
</HashLink>
373+
<Link to={`/post/${post.number}#comments`}>view comments</Link>
377374
</Text>
378375
</Box>
379376
<Text size="small">

src/PreloadLink.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// @flow
2+
3+
import React from 'react';
4+
import {matchPath} from 'react-router-dom';
5+
import {HashLink} from 'react-router-hash-link';
6+
import {routes} from './App';
7+
import {fetchQuery} from 'react-relay';
8+
import {environment} from './Environment';
9+
10+
async function runQueryForLink(to) {
11+
for (const routeConfig of routes) {
12+
const match = matchPath(to, routeConfig);
13+
if (match) {
14+
// Puts the query into the store
15+
fetchQuery(
16+
environment,
17+
routeConfig.query,
18+
routeConfig.getVariables(match),
19+
);
20+
break;
21+
}
22+
}
23+
}
24+
25+
export default function PreloadLink(props: any) {
26+
const preload = () => {
27+
runQueryForLink(props.to);
28+
};
29+
return <HashLink onClick={preload} onMouseOver={preload} {...props} />;
30+
}

src/__generated__/App_Post_Query.graphql.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/__generated__/App_Query.graphql.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/__generated__/CommentsPaginationQuery.graphql.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/__generated__/Comments_AddCommentMutation.graphql.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/__generated__/Comments_post.graphql.js

Lines changed: 10 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/__generated__/Post_AddReactionMutation.graphql.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)