Skip to content

Commit

Permalink
Merge pull request #131 from dailybruin/matthew/parallel-fetches
Browse files Browse the repository at this point in the history
Run fetches in parallel
  • Loading branch information
BrysonXiao committed May 2, 2024
2 parents 18f46ee + 416ee4f commit f1580bc
Showing 1 changed file with 71 additions and 50 deletions.
121 changes: 71 additions & 50 deletions pages/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,55 +48,76 @@ class Index extends Component {
}
static async getInitialProps(context) {
const posts = {};
const aStoryRes = await fetch(
`${Config.apiUrl}/wp-json/wp/v2/posts?_embed&per_page=1&tags=${aTAGID}&${Config.articleCardFields}`
);
const bStoryRes = await fetch(
`${Config.apiUrl}/wp-json/wp/v2/posts?_embed&per_page=1&tags=${bTAGID}&${Config.articleCardFields}`
);
const c1StoryRes = await fetch(
`${Config.apiUrl}/wp-json/wp/v2/posts?_embed&per_page=1&tags=${c1TAGID}&${Config.articleCardFields}`
);
const c2StoryRes = await fetch(
`${Config.apiUrl}/wp-json/wp/v2/posts?_embed&per_page=1&tags=${c2TAGID}&${Config.articleCardFields}`
);
const dStoryRes = await fetch(
`${Config.apiUrl}/wp-json/wp/v2/posts?_embed&per_page=1&tags=${dTAGID}&${Config.articleCardFields}`
);
const eStoryRes = await fetch(
`${Config.apiUrl}/wp-json/wp/v2/posts?_embed&per_page=1&tags=${eTAGID}&${Config.articleCardFields}`
);
const qdStoryRes = await fetch(
`${Config.apiUrl}/wp-json/wp/v2/posts?_embed&per_page=3&categories=${quadCATID}&${Config.articleCardFields}`
);
const mmStoryRes = await fetch(
`${Config.apiUrl}/wp-json/wp/v2/posts?_embed&per_page=6&tags=${m1TAGID}&${Config.articleCardFields}`
);
const f1StoryRes = await fetch(
`${Config.apiUrl}/wp-json/wp/v2/posts?_embed&per_page=1&tags=${f1TAGID}&${Config.articleCardFields}`
);
const f2StoryRes = await fetch(
`${Config.apiUrl}/wp-json/wp/v2/posts?_embed&per_page=1&tags=${f2TAGID}&${Config.articleCardFields}`
);
const nsStoryRes = await fetch(
`${Config.apiUrl}/wp-json/wp/v2/posts?_embed&per_page=3&categories=${newsCATID}&${Config.articleCardFields}`
);
const enStoryRes = await fetch(
`${Config.apiUrl}/wp-json/wp/v2/posts?_embed&per_page=3&categories=${enterpriseCATID}&${Config.articleCardFields}`
);
const opStoryRes = await fetch(
`${Config.apiUrl}/wp-json/wp/v2/posts?_embed&per_page=3&categories=${opinionCATID}&${Config.articleCardFields}`
);
const aeStoryRes = await fetch(
`${Config.apiUrl}/wp-json/wp/v2/posts?_embed&per_page=3&categories=${artsCATID}&${Config.articleCardFields}`
);
const spStoryRes = await fetch(
`${Config.apiUrl}/wp-json/wp/v2/posts?_embed&per_page=3&categories=${sportsCATID}&${Config.articleCardFields}`
);
const classifiedsRes = await fetch(
`${Config.apiUrl}/wp-json/wp/v2/classifieds?_embed&Featured=3`
);
const sponsoredRes = await fetch(`${Config.apiUrl}/wp-json/db/v1/links`);
const fetchPromises = [
fetch(
`${Config.apiUrl}/wp-json/wp/v2/posts?_embed&per_page=1&tags=${aTAGID}&${Config.articleCardFields}`
),
fetch(
`${Config.apiUrl}/wp-json/wp/v2/posts?_embed&per_page=1&tags=${bTAGID}&${Config.articleCardFields}`
),
fetch(
`${Config.apiUrl}/wp-json/wp/v2/posts?_embed&per_page=1&tags=${c1TAGID}&${Config.articleCardFields}`
),
fetch(
`${Config.apiUrl}/wp-json/wp/v2/posts?_embed&per_page=1&tags=${c2TAGID}&${Config.articleCardFields}`
),
fetch(
`${Config.apiUrl}/wp-json/wp/v2/posts?_embed&per_page=1&tags=${dTAGID}&${Config.articleCardFields}`
),
fetch(
`${Config.apiUrl}/wp-json/wp/v2/posts?_embed&per_page=1&tags=${eTAGID}&${Config.articleCardFields}`
),
fetch(
`${Config.apiUrl}/wp-json/wp/v2/posts?_embed&per_page=3&categories=${quadCATID}&${Config.articleCardFields}`
),
fetch(
`${Config.apiUrl}/wp-json/wp/v2/posts?_embed&per_page=6&tags=${m1TAGID}&${Config.articleCardFields}`
),
fetch(
`${Config.apiUrl}/wp-json/wp/v2/posts?_embed&per_page=1&tags=${f1TAGID}&${Config.articleCardFields}`
),
fetch(
`${Config.apiUrl}/wp-json/wp/v2/posts?_embed&per_page=1&tags=${f2TAGID}&${Config.articleCardFields}`
),
fetch(
`${Config.apiUrl}/wp-json/wp/v2/posts?_embed&per_page=3&categories=${newsCATID}&${Config.articleCardFields}`
),
fetch(
`${Config.apiUrl}/wp-json/wp/v2/posts?_embed&per_page=3&categories=${enterpriseCATID}&${Config.articleCardFields}`
),
fetch(
`${Config.apiUrl}/wp-json/wp/v2/posts?_embed&per_page=3&categories=${opinionCATID}&${Config.articleCardFields}`
),
fetch(
`${Config.apiUrl}/wp-json/wp/v2/posts?_embed&per_page=3&categories=${artsCATID}&${Config.articleCardFields}`
),
fetch(
`${Config.apiUrl}/wp-json/wp/v2/posts?_embed&per_page=3&categories=${sportsCATID}&${Config.articleCardFields}`
),
fetch(`${Config.apiUrl}/wp-json/wp/v2/classifieds?_embed&Featured=3`),
fetch(`${Config.apiUrl}/wp-json/db/v1/links`)
];
const fetchResults = await Promise.allSettled(fetchPromises);
const [
aStoryRes,
bStoryRes,
c1StoryRes,
c2StoryRes,
dStoryRes,
eStoryRes,
qdStoryRes,
mmStoryRes,
f1StoryRes,
f2StoryRes,
nsStoryRes,
enStoryRes,
opStoryRes,
aeStoryRes,
spStoryRes,
classifiedsRes,
sponsoredRes
] = fetchResults.map(res => res.value);

posts.aStory = await aStoryRes.json();
posts.bStory = await bStoryRes.json();
posts.c1Story = await c1StoryRes.json();
Expand Down Expand Up @@ -195,7 +216,7 @@ class Index extends Component {
<meta
name="robots"
content="max-snippet:-1, max-image-preview:large, max-video-preview:-1"
/>
/>
</Head>
<HomeLayout
posts={this.props.posts}
Expand Down

0 comments on commit f1580bc

Please sign in to comment.