Skip to content

Commit 21773dd

Browse files
fix!: remove catch-all DSG redirect and handle discretely (#334)
* fix!: remove catch-all DSG redirect * docs: add gatsby plugin requirement for DSG pages * feat: add version check for gatsby-netlify-plugin * chore: replace prettier ignore with eslint ignore * refactor: simplify checkPackageVersion Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 582458e commit 21773dd

File tree

6 files changed

+68
-18
lines changed

6 files changed

+68
-18
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ manually, you have two options:
4545

4646
You should also install the Gatsby plugin
4747
[gatsby-plugin-netlify](https://www.gatsbyjs.org/plugins/gatsby-plugin-netlify/).
48-
This is required for SSR pages, and adds support for Gatsby redirects and asset
48+
This is required for SSR and DSG pages, and adds support for Gatsby redirects and asset
4949
caching rules:
5050

5151
1. Add the package as a dependency:

plugin/package-lock.json

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugin/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@
5454
"node-stream-zip": "^1.15.0",
5555
"pathe": "^0.2.0",
5656
"pretty-bytes": "^5.6.0",
57+
"semver": "^7.3.5",
5758
"tempy": "^1.0.0"
5859
},
5960
"devDependencies": {
6061
"@gatsbyjs/reach-router": "^1.3.6",
6162
"@netlify/build": "^26.5.1",
6263
"@types/fs-extra": "^9.0.12",
6364
"@types/multer": "^1.4.7",
65+
"@types/semver": "^7.3.9",
6466
"gatsby": "^4.9.0",
6567
"npm-run-all": "^4.1.5",
6668
"rimraf": "^3.0.2",

plugin/src/helpers/config.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable max-lines */
12
import { EOL } from 'os'
23
import path from 'path'
34
import process from 'process'
@@ -6,6 +7,8 @@ import { stripIndent } from 'common-tags'
67
import fs, { existsSync } from 'fs-extra'
78
import type { GatsbyConfig, PluginRef } from 'gatsby'
89

10+
import { checkPackageVersion } from './files'
11+
912
export async function spliceConfig({
1013
startMarker,
1114
endMarker,
@@ -38,11 +41,9 @@ export async function spliceConfig({
3841
return fs.writeFile(fileName, out)
3942
}
4043

41-
function loadGatsbyConfig({ utils, publish }): GatsbyConfig | never {
42-
const gatsbyConfigFile = path.resolve(
43-
getGatsbyRoot(publish),
44-
'gatsby-config.js',
45-
)
44+
function loadGatsbyConfig({ gatsbyRoot, utils }): GatsbyConfig | never {
45+
const gatsbyConfigFile = path.resolve(gatsbyRoot, 'gatsby-config.js')
46+
4647
if (!existsSync(gatsbyConfigFile)) {
4748
return {}
4849
}
@@ -65,14 +66,28 @@ function hasPlugin(plugins: PluginRef[], pluginName: string): boolean {
6566
)
6667
}
6768

68-
export function checkGatsbyConfig({ utils, netlifyConfig }): void {
69+
export async function checkConfig({ utils, netlifyConfig }): Promise<void> {
70+
const gatsbyRoot = getGatsbyRoot(netlifyConfig.build.publish)
71+
6972
// warn if gatsby-plugin-netlify is missing
7073
const gatsbyConfig = loadGatsbyConfig({
7174
utils,
72-
publish: netlifyConfig.build.publish,
75+
gatsbyRoot,
7376
})
7477

75-
if (!hasPlugin(gatsbyConfig.plugins, 'gatsby-plugin-netlify')) {
78+
if (hasPlugin(gatsbyConfig.plugins, 'gatsby-plugin-netlify')) {
79+
if (
80+
!(await checkPackageVersion(
81+
gatsbyRoot,
82+
'gatsby-plugin-netlify',
83+
'>=4.2.0',
84+
))
85+
) {
86+
console.error(
87+
'The plugin `gatsby-plugin-netlify` does not support DSG, please update to >=4.2.0',
88+
)
89+
}
90+
} else {
7691
console.error(
7792
'Please install `gatsby-plugin-netlify` and enable it in your gatsby-config.js. https://www.gatsbyjs.com/plugins/gatsby-plugin-netlify/',
7893
)
@@ -162,3 +177,4 @@ export function shouldSkipFunctions(cacheDir: string): boolean {
162177
export function getGatsbyRoot(publish: string): string {
163178
return path.resolve(path.dirname(publish))
164179
}
180+
/* eslint-enable max-lines */

plugin/src/helpers/files.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import os from 'os'
22
import process from 'process'
33

4-
import { copyFile, ensureDir, existsSync, readFile, writeFile } from 'fs-extra'
4+
import {
5+
copyFile,
6+
ensureDir,
7+
existsSync,
8+
readFile,
9+
writeFile,
10+
readJson,
11+
} from 'fs-extra'
512
import { dirname, join, resolve } from 'pathe'
13+
import semver from 'semver'
614

715
const DEFAULT_LAMBDA_PLATFORM = 'linux'
816
const DEFAULT_LAMBDA_ABI = '83'
@@ -70,6 +78,22 @@ export const findModuleFromBase = ({ paths, candidates }): string | null => {
7078
return null
7179
}
7280

81+
export async function checkPackageVersion(
82+
root: string,
83+
name: string,
84+
version: string,
85+
): Promise<boolean> {
86+
try {
87+
const packagePath = require.resolve(`${name}/package.json`, {
88+
paths: [root],
89+
})
90+
const packageObj = await readJson(packagePath)
91+
return semver.satisfies(packageObj.version, version)
92+
} catch {
93+
return false
94+
}
95+
}
96+
7397
/**
7498
* When Gatsby runs a build, it copies binary dependencies to the "query-engine" folder. These are auto-detected, based
7599
* on the environment in which the build is running. This can cause problems if these don't match the lambda environment.

plugin/src/index.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { existsSync } from 'fs-extra'
77

88
import { normalizedCacheDir, restoreCache, saveCache } from './helpers/cache'
99
import {
10-
checkGatsbyConfig,
10+
checkConfig,
1111
mutateConfig,
1212
shouldSkipFunctions,
1313
spliceConfig,
@@ -32,7 +32,7 @@ export async function onPreBuild({
3232
}
3333
await restoreCache({ utils, publish: PUBLISH_DIR })
3434

35-
checkGatsbyConfig({ utils, netlifyConfig })
35+
await checkConfig({ utils, netlifyConfig })
3636
}
3737

3838
export async function onBuild({
@@ -79,12 +79,6 @@ The plugin no longer uses this and it should be deleted to avoid conflicts.\n`)
7979
contents: '/api/* /.netlify/functions/__api 200',
8080
fileName: join(netlifyConfig.build.publish, '_redirects'),
8181
})
82-
83-
netlifyConfig.redirects.push({
84-
from: '/*',
85-
to: '/.netlify/builders/__dsg',
86-
status: 200,
87-
})
8882
}
8983

9084
export async function onPostBuild({

0 commit comments

Comments
 (0)