-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from storefront-foundation/prerender
Added prerender helper
- Loading branch information
Showing
3 changed files
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import getClient from './utils/client' | ||
|
||
// Fetches the page and API paths for all categories and a few product from each specified by the given options | ||
export default async function getPrerenderUrls(options = { offset: 0, limit: 10 }) { | ||
const client = await getClient() | ||
|
||
const { categories = [] } = await client.getMenu() | ||
|
||
const paths = [] | ||
|
||
// Prerender all category pages | ||
for (let category of categories) { | ||
paths.push({ path: `/s/${category.id}` }) | ||
paths.push({ path: `/api/s/${category.id}` }) | ||
|
||
// Try prerendering the first 10 products of each category | ||
const search = await client.findProducts({ | ||
...options, | ||
refine: [`cgid=${category.id}`], | ||
}) | ||
|
||
if (search.hits) { | ||
for (let hit of search.hits) { | ||
paths.push({ path: `/p/${hit.productId}` }) | ||
paths.push({ path: `/api/p/${hit.productId}` }) | ||
} | ||
} | ||
} | ||
|
||
return paths | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import fetch from 'node-fetch' | ||
import querystring from 'querystring' | ||
import { COOKIES } from './constants' | ||
|
||
|