diff --git a/nuxt.config.js b/nuxt.config.js index 8c0ac9731..8855b6a01 100755 --- a/nuxt.config.js +++ b/nuxt.config.js @@ -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, diff --git a/services/get-routes-to-generate.js b/services/get-routes-to-generate.js new file mode 100644 index 000000000..a863f9a42 --- /dev/null +++ b/services/get-routes-to-generate.js @@ -0,0 +1,23 @@ +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) { + // eslint-disable-next-line camelcase + const { results, total_pages } = await api.query('', { + pageSize: 100, + page, + }) + const uids = _.map(results, 'uid') + const routes = _.reject(uids, _.isEmpty) + return { totalPages: total_pages, routes } +} diff --git a/tests/services/get-routes-to-generate.test.js b/tests/services/get-routes-to-generate.test.js new file mode 100644 index 000000000..6bb776c72 --- /dev/null +++ b/tests/services/get-routes-to-generate.test.js @@ -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) + }) +})