Skip to content

Commit

Permalink
Specify routes to generate fetching prismic document
Browse files Browse the repository at this point in the history
This allows to generate unlinked pages
  • Loading branch information
HEYGUL committed Mar 1, 2021
1 parent 7a05a9e commit c1d08f4
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
2 changes: 2 additions & 0 deletions nuxt.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { transports } from 'winston'
import routes from './services/get-routes-to-generate'

export default {
generate: { routes },
target: process.env.SSR_ENABLED === 'true' ? 'server' : 'static',
publicRuntimeConfig: {
languageSwitchEnabled: process.env.LANGUAGE_SWITCH_ENABLED || false,
Expand Down
22 changes: 22 additions & 0 deletions services/get-routes-to-generate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import prismic from 'prismic-javascript'
import _ from 'lodash'

export default async function () {
const api = await prismic.getApi(process.env.PRISMIC_API_ENDPOINT)
const { routes, totalPages } = await getRoutesInPage(api, 1)
for (let page = 2; page <= totalPages; page++) {
const { routes: nextPageRoutes } = await getRoutesInPage(api, page)
routes.push(...nextPageRoutes)
}
return routes
}

async function getRoutesInPage(api, page) {
const { results, total_pages: totalPages } = await api.query('', {
pageSize: 100,
page,
})
const uids = _.map(results, 'uid')
const routes = _.reject(uids, _.isEmpty)
return { totalPages, routes }
}
74 changes: 74 additions & 0 deletions tests/services/get-routes-to-generate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import getRoutesToGenerate from '@/services/get-routes-to-generate'
import prismic from 'prismic-javascript'
jest.mock('prismic-javascript')

describe('#getRoutesToGenerate', () => {
test('it should fetch routes to generate', async () => {
// Given
const expected = ['route-to-generate']
const prismicApi = {
query: jest
.fn()
.mockResolvedValueOnce({ results: [{ uid: 'route-to-generate' }] }),
}
prismic.getApi = () => prismicApi

// When
const result = await getRoutesToGenerate(prismic)

// Then
expect(prismicApi.query).toBeCalledWith('', { pageSize: 100, page: 1 })
expect(result).toEqual(expected)
})

test('it should not return null routes', async () => {
// Given
const expected = ['route-to-generate']
const prismicApi = {
query: jest.fn().mockReturnValueOnce({
results: [{ uid: 'route-to-generate' }, {}, { uid: null }],
}),
}
prismic.getApi = () => prismicApi

// When
const result = await getRoutesToGenerate(prismic)

// Then
expect(prismicApi.query).toBeCalledWith('', { pageSize: 100, page: 1 })
expect(result).toEqual(expected)
})

test('it should handle pagination', async () => {
// Given
const expected = [
'route-to-generate-from-first-page',
'route-to-generate-from-second-page',
]

const firstPage = {
total_pages: 2,
results: [{ uid: 'route-to-generate-from-first-page' }],
}
const secondPage = {
total_pages: 2,
results: [{ uid: 'route-to-generate-from-second-page' }],
}

const prismicApi = {
query: jest
.fn()
.mockResolvedValueOnce(firstPage)
.mockResolvedValueOnce(secondPage),
}
prismic.getApi = () => prismicApi

// When
const result = await getRoutesToGenerate(prismic)

// Then
expect(prismicApi.query).toBeCalledWith('', { pageSize: 100, page: 1 })
expect(prismicApi.query).toBeCalledWith('', { pageSize: 100, page: 2 })
expect(result).toEqual(expected)
})
})

0 comments on commit c1d08f4

Please sign in to comment.