|
1 | 1 | const { join } = require('path') |
2 | 2 |
|
3 | | -const asyncForEach = require('../../helpers/asyncForEach') |
4 | 3 | const getFilePathForRoute = require('../../helpers/getFilePathForRoute') |
5 | 4 | const isRouteWithFallback = require('../../helpers/isRouteWithFallback') |
6 | | -const { logTitle, logItem } = require('../../helpers/logger') |
7 | | -const setupNetlifyFunctionForPage = require('../../helpers/setupNetlifyFunctionForPage') |
8 | | -const setupStaticFileForPage = require('../../helpers/setupStaticFileForPage') |
| 5 | +const { logTitle } = require('../../helpers/logger') |
9 | 6 |
|
10 | 7 | const getPages = require('./pages') |
11 | 8 |
|
12 | 9 | // Copy pre-rendered SSG pages |
13 | 10 | const setup = async ({ functionsPath, publishPath }) => { |
14 | 11 | logTitle('🔥 Copying pre-rendered pages with getStaticProps and JSON data to', publishPath) |
15 | | - |
16 | 12 | // Keep track of the functions that have been set up, so that we do not set up |
17 | 13 | // a function for the same file path twice |
18 | | - const filePathsDone = [] |
19 | | - |
| 14 | + const filePathsDone = new Set() |
20 | 15 | const pages = await getPages() |
21 | 16 |
|
22 | | - await asyncForEach(pages, async ({ route, dataRoute, srcRoute }) => { |
23 | | - logItem(route) |
24 | | - |
25 | | - // Copy pre-rendered HTML page |
26 | | - const htmlPath = getFilePathForRoute(route, 'html') |
27 | | - await setupStaticFileForPage({ inputPath: htmlPath, publishPath }) |
28 | | - |
29 | | - // Copy page's JSON data |
30 | | - const jsonPath = getFilePathForRoute(route, 'json') |
31 | | - await setupStaticFileForPage({ |
32 | | - inputPath: jsonPath, |
33 | | - outputPath: dataRoute, |
34 | | - publishPath, |
35 | | - }) |
36 | | - |
37 | | - // Set up the Netlify function (this is ONLY for preview mode) |
38 | | - const relativePath = getFilePathForRoute(srcRoute || route, 'js') |
39 | | - const filePath = join('pages', relativePath) |
40 | | - |
41 | | - // Skip if we have already set up a function for this file |
42 | | - // or if the source route has a fallback (handled by getStaticPropsWithFallback) |
43 | | - if (filePathsDone.includes(filePath) || (await isRouteWithFallback(srcRoute))) return |
44 | | - |
45 | | - logItem(filePath) |
46 | | - await setupNetlifyFunctionForPage({ filePath, functionsPath }) |
47 | | - filePathsDone.push(filePath) |
48 | | - }) |
| 17 | + const jobs = [] |
| 18 | + |
| 19 | + await Promise.all( |
| 20 | + pages.map(async ({ route, dataRoute, srcRoute }) => { |
| 21 | + // Copy pre-rendered HTML page |
| 22 | + const htmlPath = getFilePathForRoute(route, 'html') |
| 23 | + |
| 24 | + jobs.push({ type: 'static', inputPath: htmlPath, publishPath }) |
| 25 | + |
| 26 | + // Copy page's JSON data |
| 27 | + const jsonPath = getFilePathForRoute(route, 'json') |
| 28 | + jobs.push({ |
| 29 | + type: 'static', |
| 30 | + inputPath: jsonPath, |
| 31 | + outputPath: dataRoute, |
| 32 | + publishPath, |
| 33 | + }) |
| 34 | + |
| 35 | + // Set up the Netlify function (this is ONLY for preview mode) |
| 36 | + const relativePath = getFilePathForRoute(srcRoute || route, 'js') |
| 37 | + const filePath = join('pages', relativePath) |
| 38 | + |
| 39 | + // Skip if we have already set up a function for this file |
| 40 | + |
| 41 | + if (filePathsDone.has(filePath)) { |
| 42 | + return |
| 43 | + } |
| 44 | + filePathsDone.add(filePath) |
| 45 | + |
| 46 | + // or if the source route has a fallback (handled by getStaticPropsWithFallback) |
| 47 | + if (await isRouteWithFallback(srcRoute)) { |
| 48 | + return |
| 49 | + } |
| 50 | + jobs.push({ type: 'function', filePath, functionsPath }) |
| 51 | + }), |
| 52 | + ) |
| 53 | + return jobs |
49 | 54 | } |
50 | 55 |
|
51 | 56 | module.exports = setup |
0 commit comments