Skip to content
This repository was archived by the owner on May 10, 2021. It is now read-only.

Commit c2bfb6b

Browse files
committed
Simplify build output for SSG pages
Combine the line for page HTML and page JSON into a single line per page and move output for SSG pages with fallback to section for SSR pages. This way, we can eliminate the nesting from SSG pages (we no longer have subsection 1, 2, and 3). This will allow us to more easily implement the maxLogLines option for limiting output for each section to a specific number of lines.
1 parent 63f5ce2 commit c2bfb6b

File tree

4 files changed

+24
-53
lines changed

4 files changed

+24
-53
lines changed

lib/setupSsgPages.js

Lines changed: 13 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,77 +11,42 @@ const getNetlifyFunctionName = require('./getNetlifyFunctionNam
1111
// Identify all pages that require server-side rendering and create a separate
1212
// Netlify Function for every page.
1313
const setupSsgPages = () => {
14-
logTitle("🔥 Setting up SSG pages")
14+
// Folder for page JSON data
15+
const nextDataFolder = join(NETLIFY_PUBLISH_PATH, "_next", "data/")
16+
17+
logTitle("🔥 Copying pre-rendered SSG pages to", NETLIFY_PUBLISH_PATH,
18+
"and JSON data to", nextDataFolder)
1519

1620
// Get SSG pages
1721
const ssgPages = allNextJsPages.filter(page => page.isSsg())
1822

19-
// Copy pre-rendered SSG pages to Netlify publish folder
20-
logItem("1. Copying pre-rendered SSG pages to", NETLIFY_PUBLISH_PATH)
23+
ssgPages.forEach(({ route, htmlFile, jsonFile, dataRoute }) => {
24+
logItem(route)
2125

22-
ssgPages.forEach(({ htmlFile }) => {
23-
const filePath = join("pages", htmlFile)
24-
logItem(" ", filePath)
26+
// Copy pre-rendered HTML page
27+
const htmlPath = join("pages", htmlFile)
2528

2629
copySync(
27-
join(NEXT_DIST_DIR, "serverless", filePath),
30+
join(NEXT_DIST_DIR, "serverless", htmlPath),
2831
join(NETLIFY_PUBLISH_PATH, htmlFile),
2932
{
3033
overwrite: false,
3134
errorOnExist: true
3235
}
3336
)
34-
})
3537

36-
// Copy SSG page data to _next/data/ folder
37-
const nextDataFolder = join(NETLIFY_PUBLISH_PATH, "_next", "data/")
38-
logItem("2. Copying SSG page data to", nextDataFolder)
39-
40-
ssgPages.forEach(({ jsonFile, dataRoute }) => {
41-
const dataPath = join("pages", jsonFile)
42-
logItem(" ", dataPath)
38+
// Copy page's JSON data
39+
const jsonPath = join("pages", jsonFile)
4340

4441
copySync(
45-
join(NEXT_DIST_DIR, "serverless", dataPath),
42+
join(NEXT_DIST_DIR, "serverless", jsonPath),
4643
join(NETLIFY_PUBLISH_PATH, dataRoute),
4744
{
4845
overwrite: false,
4946
errorOnExist: true
5047
}
5148
)
5249
})
53-
54-
// Set up Netlify Functions to handle fallbacks for SSG pages
55-
const ssgFallbackPages = allNextJsPages.filter(page => page.isSsgFallback())
56-
logItem("3. Setting up Netlify Functions for SSG pages with fallback: true")
57-
58-
ssgFallbackPages.forEach(({ filePath }) => {
59-
logItem(" ", filePath)
60-
61-
// Set function name based on file path
62-
const functionName = getNetlifyFunctionName(filePath)
63-
const functionDirectory = join(NETLIFY_FUNCTIONS_PATH, functionName)
64-
65-
// Copy function template
66-
copySync(
67-
FUNCTION_TEMPLATE_PATH,
68-
join(functionDirectory, `${functionName}.js`),
69-
{
70-
overwrite: false,
71-
errorOnExist: true
72-
}
73-
)
74-
75-
// Copy page
76-
copySync(
77-
join(NEXT_DIST_DIR, "serverless", filePath),
78-
join(functionDirectory, "nextJsPage.js"),
79-
{
80-
overwrite: false,
81-
errorOnExist: true
82-
}
83-
)
84-
})
8550
}
8651

8752
module.exports = setupSsgPages

lib/setupSsrPages.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ const getNetlifyFunctionName = require('./getNetlifyF
1010
// Identify all pages that require server-side rendering and create a separate
1111
// Netlify Function for every page.
1212
const setupSsrPages = () => {
13-
logTitle("💫 Setting up SSR pages as Netlify Functions in", NETLIFY_FUNCTIONS_PATH)
13+
logTitle("💫 Setting up SSR pages and SSG pages with fallback",
14+
"as Netlify Functions in", NETLIFY_FUNCTIONS_PATH)
1415

15-
// Get SSR pages
16-
const ssrPages = allNextJsPages.filter(page => page.isSsr())
16+
// Get SSR pages and SSG fallback pages (which also need to be rendered
17+
// server-side)
18+
const ssrPages = allNextJsPages.filter(page =>
19+
page.isSsr() || page.isSsgFallback()
20+
)
1721

1822
// Create Netlify Function for every page
1923
ssrPages.forEach(({ filePath }) => {

next-on-netlify.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env node
22

3+
const { logTitle } = require('./lib/logger')
34
const prepareFolders = require('./lib/prepareFolders')
45
const copyPublicFiles = require('./lib/copyPublicFiles')
56
const copyNextAssets = require('./lib/copyNextAssets')
@@ -22,4 +23,4 @@ setupHtmlPages()
2223

2324
setupRedirects()
2425

25-
console.log("\x1b[1m✅ Success! All done!\x1b[22m")
26+
logTitle("✅ Success! All done!")

tests/defaults.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ describe('Next', () => {
6666
expect(BUILD_OUTPUT).toMatch("Next on Netlify")
6767
expect(BUILD_OUTPUT).toMatch("Copying public/ folder to out_publish/")
6868
expect(BUILD_OUTPUT).toMatch("Copying static NextJS assets to out_publish/")
69-
expect(BUILD_OUTPUT).toMatch("Setting up SSR pages as Netlify Functions in out_functions/")
69+
expect(BUILD_OUTPUT).toMatch("Setting up SSR pages and SSG pages with fallback as Netlify Functions in out_functions/")
70+
expect(BUILD_OUTPUT).toMatch("Copying pre-rendered SSG pages to out_publish/ and JSON data to out_publish/_next/data/")
7071
expect(BUILD_OUTPUT).toMatch("Writing pre-rendered HTML pages to out_publish/")
7172
expect(BUILD_OUTPUT).toMatch("Setting up redirects")
7273
expect(BUILD_OUTPUT).toMatch("Success! All done!")

0 commit comments

Comments
 (0)