From 031c7eb036ef447a860d38301a6c11e7fd9de90f Mon Sep 17 00:00:00 2001 From: Guillaume Lagorce Date: Wed, 24 Feb 2021 21:48:00 +0100 Subject: [PATCH] Specify routes to generate fetching prismic document This allows to generate unlinked pages --- nuxt.config.js | 2 + services/get-routes-to-generate.js | 22 ++++++ tests/services/get-routes-to-generate.test.js | 74 +++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 services/get-routes-to-generate.js create mode 100644 tests/services/get-routes-to-generate.test.js diff --git a/nuxt.config.js b/nuxt.config.js index afc368de9..b1e983b07 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..bb86f7ab9 --- /dev/null +++ b/services/get-routes-to-generate.js @@ -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: 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) + }) +})