Skip to content

Commit

Permalink
Merge pull request #4 from storefront-foundation/prerender
Browse files Browse the repository at this point in the history
Added prerender helper
  • Loading branch information
dijs authored Nov 16, 2020
2 parents fdb2567 + b4fb5cc commit 2a9511f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/getPrerenderUrls.js
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
}
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export { default as subcategory } from './subcategory.js'
export { default as signUp } from './signUp.js'
export { default as signIn } from './signIn.js'
export { default as signOut } from './signOut.js'
export { default as getPrerenderUrls } from './getPrerenderUrls.js'
1 change: 1 addition & 0 deletions src/utils/client.js
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'

Expand Down

0 comments on commit 2a9511f

Please sign in to comment.