Skip to content

Commit abefe10

Browse files
authored
feat: write granular info into skip file (#128)
* feat: write granular function info into skip file * chore: code style amend * fix: remove function skip file when not needed * test: add tests for new skip functions file
1 parent fd0bc56 commit abefe10

File tree

2 files changed

+68
-19
lines changed

2 files changed

+68
-19
lines changed

src/__tests__/gatsby-node.ts

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable max-lines-per-function */
12
jest.mock('../plugin-data', () => ({
23
__esModule: true,
34
default: jest.fn().mockReturnValue({
@@ -14,10 +15,12 @@ jest.mock('fs-extra', () => ({
1415
existsSync: jest.fn(),
1516
readFile: jest.fn(),
1617
writeFile: jest.fn(),
18+
writeJson: jest.fn(),
19+
remove: jest.fn(),
1720
}))
1821

1922
// Importing writeFile here gives us access to the mocked method to assert the correct content is written to the file within test cases
20-
import { writeFile } from 'fs-extra'
23+
import { writeFile, writeJson, remove } from 'fs-extra'
2124
import { testPluginOptionsSchema } from 'gatsby-plugin-utils'
2225

2326
import { pluginOptionsSchema, onPostBuild } from '../gatsby-node'
@@ -67,12 +70,12 @@ describe(`gatsby-node.js`, () => {
6770
})
6871

6972
describe('onPostBuild', () => {
70-
let store = {}
7173
const reporter = {
7274
info: jest.fn(),
7375
}
74-
beforeEach(() => {
75-
store = {
76+
77+
it('creates redirects for SSR and DSG pages in format Netlify expects', async () => {
78+
const store = {
7679
getState: jest.fn().mockReturnValue({
7780
redirects: [],
7881
pages: [
@@ -89,13 +92,6 @@ describe(`gatsby-node.js`, () => {
8992
program: { directory: '' },
9093
}),
9194
}
92-
})
93-
94-
afterEach(() => {
95-
jest.resetAllMocks()
96-
})
97-
98-
it('creates redirects for SSR and DSG pages in format Netlify expects', async () => {
9995
const expectedValue = [
10096
'',
10197
'## Created with gatsby-plugin-netlify',
@@ -106,7 +102,55 @@ describe(`gatsby-node.js`, () => {
106102
].join(`\n`)
107103

108104
await onPostBuild({ store, pathPrefix: '', reporter }, {})
109-
expect(writeFile).toHaveBeenCalledWith('mock-file-path', expectedValue)
105+
expect(writeFile).toHaveBeenLastCalledWith('mock-file-path', expectedValue)
106+
})
107+
108+
it('creates skip file for unneeded function bundles', async () => {
109+
const store = {
110+
getState: jest.fn().mockReturnValue({
111+
redirects: [],
112+
pages: [
113+
{
114+
mode: 'DSG',
115+
path: 'some/other/path',
116+
},
117+
],
118+
functions: [],
119+
program: { directory: '' },
120+
}),
121+
}
122+
const expectedValue = {
123+
API: false,
124+
SSR: false,
125+
DSG: true,
126+
}
127+
128+
await onPostBuild({ store, pathPrefix: '', reporter }, {})
129+
expect(writeJson).toHaveBeenLastCalledWith('.cache/.nf-skip-gatsby-functions', expectedValue)
130+
})
131+
132+
it('removes skip file when all function bundles needed', async () => {
133+
const store = {
134+
getState: jest.fn().mockReturnValue({
135+
redirects: [],
136+
pages: [
137+
{
138+
mode: 'SSR',
139+
path: 'some/path',
140+
},
141+
{
142+
mode: 'DSG',
143+
path: 'some/other/path',
144+
},
145+
],
146+
functions: [true],
147+
program: { directory: '' },
148+
}),
149+
}
150+
151+
await onPostBuild({ store, pathPrefix: '', reporter }, {})
152+
expect(remove).toHaveBeenCalled()
110153
})
111154
})
112155
})
156+
/* eslint-enable max-lines-per-function */

src/gatsby-node.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// https://www.netlify.com/docs/headers-and-basic-auth/
22
import { join } from 'path'
33

4-
import { writeFile } from 'fs-extra'
4+
import { writeJson, remove } from 'fs-extra'
55
import { generatePageDataPath } from 'gatsby-core-utils'
66
import WebpackAssetsManifest from 'webpack-assets-manifest'
77

@@ -72,15 +72,19 @@ export const onPostBuild = async ({ store, pathPrefix, reporter }: any, userPlug
7272
let count = 0
7373
const rewrites: any = []
7474

75-
let needsFunctions = functions.length !== 0
75+
const neededFunctions = {
76+
API: functions.length !== 0,
77+
SSR: false,
78+
DSG: false,
79+
}
7680

7781
;[...pages.values()].forEach((page) => {
7882
const { mode, matchPath, path } = page
7983
const matchPathClean = matchPath && matchPath.replace(/\*.*/, '*')
8084
const matchPathIsNotPath = matchPath && matchPath !== path
8185

8286
if (mode === `SSR` || mode === `DSG`) {
83-
needsFunctions = true
87+
neededFunctions[mode] = true
8488
const fromPath = matchPathClean ?? path
8589
const toPath = mode === `SSR` ? `/.netlify/functions/__ssr` : `/.netlify/functions/__dsg`
8690
count++
@@ -103,12 +107,13 @@ export const onPostBuild = async ({ store, pathPrefix, reporter }: any, userPlug
103107
})
104108
reporter.info(`[gatsby-plugin-netlify] Created ${count} SSR/DSG redirect${count === 1 ? `` : `s`}...`)
105109

106-
if (!needsFunctions) {
107-
reporter.info(`[gatsby-plugin-netlify] No Netlify functions needed. Skipping...`)
108-
await writeFile(join(program.directory, `.cache`, `.nf-skip-gatsby-functions`), ``)
109-
}
110+
const skipFilePath = join(program.directory, `.cache`, `.nf-skip-gatsby-functions`)
111+
const generateSkipFile = Object.values(neededFunctions).includes(false)
112+
? writeJson(skipFilePath, neededFunctions)
113+
: remove(skipFilePath)
110114

111115
await Promise.all([
116+
generateSkipFile,
112117
buildHeadersProgram(pluginData, pluginOptions, reporter),
113118
createRedirects(pluginData, redirects, rewrites),
114119
])

0 commit comments

Comments
 (0)