From f74f7cf1bc197df306497430960294975ebd2119 Mon Sep 17 00:00:00 2001 From: Rob Stanford Date: Fri, 25 Mar 2022 09:57:44 +0000 Subject: [PATCH 1/5] fix!: remove catch-all DSG redirect --- plugin/src/index.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/plugin/src/index.ts b/plugin/src/index.ts index bd9d477e..fe65d173 100644 --- a/plugin/src/index.ts +++ b/plugin/src/index.ts @@ -79,12 +79,6 @@ The plugin no longer uses this and it should be deleted to avoid conflicts.\n`) contents: '/api/* /.netlify/functions/__api 200', fileName: join(netlifyConfig.build.publish, '_redirects'), }) - - netlifyConfig.redirects.push({ - from: '/*', - to: '/.netlify/builders/__dsg', - status: 200, - }) } export async function onPostBuild({ From 9b316ac0fdfa51b9a288632c4a6d22c62550442c Mon Sep 17 00:00:00 2001 From: Rob Stanford Date: Fri, 25 Mar 2022 09:58:59 +0000 Subject: [PATCH 2/5] docs: add gatsby plugin requirement for DSG pages --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 19bf5088..45ad4a4b 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ manually, you have two options: You should also install the Gatsby plugin [gatsby-plugin-netlify](https://www.gatsbyjs.org/plugins/gatsby-plugin-netlify/). -This is required for SSR pages, and adds support for Gatsby redirects and asset +This is required for SSR and DSG pages, and adds support for Gatsby redirects and asset caching rules: 1. Add the package as a dependency: From 06d961dd33e0316327967ceeef5b45816e42ba5a Mon Sep 17 00:00:00 2001 From: Rob Stanford Date: Fri, 1 Apr 2022 16:44:13 +0100 Subject: [PATCH 3/5] feat: add version check for gatsby-netlify-plugin --- plugin/package-lock.json | 14 ++++++++++++++ plugin/package.json | 2 ++ plugin/src/helpers/config.ts | 27 +++++++++++++++++++-------- plugin/src/helpers/files.ts | 30 +++++++++++++++++++++++++++++- plugin/src/index.ts | 4 ++-- 5 files changed, 66 insertions(+), 11 deletions(-) diff --git a/plugin/package-lock.json b/plugin/package-lock.json index 1c0060dc..5422ae8e 100644 --- a/plugin/package-lock.json +++ b/plugin/package-lock.json @@ -23,6 +23,7 @@ "node-stream-zip": "^1.15.0", "pathe": "^0.2.0", "pretty-bytes": "^5.6.0", + "semver": "^7.3.5", "tempy": "^1.0.0" }, "devDependencies": { @@ -30,6 +31,7 @@ "@netlify/build": "^26.5.1", "@types/fs-extra": "^9.0.12", "@types/multer": "^1.4.7", + "@types/semver": "^7.3.9", "gatsby": "^4.9.0", "npm-run-all": "^4.1.5", "rimraf": "^3.0.2", @@ -6155,6 +6157,12 @@ "integrity": "sha512-hppQEBDmlwhFAXKJX2KnWLYu5yMfi91yazPb2l+lbJiwW+wdo1gNeRA+3RgNSO39WYX2euey41KEwnqesU2Jew==", "dev": true }, + "node_modules/@types/semver": { + "version": "7.3.9", + "resolved": "https://registry.npmjs.org/@types/semver/-/semver-7.3.9.tgz", + "integrity": "sha512-L/TMpyURfBkf+o/526Zb6kd/tchUP3iBDEPjqjb+U2MAJhVRxxrmr2fwpe08E7QsV7YLcpq0tUaQ9O9x97ZIxQ==", + "dev": true + }, "node_modules/@types/serve-static": { "version": "1.13.10", "resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.13.10.tgz", @@ -29699,6 +29707,12 @@ "integrity": "sha512-hppQEBDmlwhFAXKJX2KnWLYu5yMfi91yazPb2l+lbJiwW+wdo1gNeRA+3RgNSO39WYX2euey41KEwnqesU2Jew==", "dev": true }, + "@types/semver": { + "version": "7.3.9", + "resolved": "https://registry.npmjs.org/@types/semver/-/semver-7.3.9.tgz", + "integrity": "sha512-L/TMpyURfBkf+o/526Zb6kd/tchUP3iBDEPjqjb+U2MAJhVRxxrmr2fwpe08E7QsV7YLcpq0tUaQ9O9x97ZIxQ==", + "dev": true + }, "@types/serve-static": { "version": "1.13.10", "resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.13.10.tgz", diff --git a/plugin/package.json b/plugin/package.json index c8be075b..48dfc184 100644 --- a/plugin/package.json +++ b/plugin/package.json @@ -54,6 +54,7 @@ "node-stream-zip": "^1.15.0", "pathe": "^0.2.0", "pretty-bytes": "^5.6.0", + "semver": "^7.3.5", "tempy": "^1.0.0" }, "devDependencies": { @@ -61,6 +62,7 @@ "@netlify/build": "^26.5.1", "@types/fs-extra": "^9.0.12", "@types/multer": "^1.4.7", + "@types/semver": "^7.3.9", "gatsby": "^4.9.0", "npm-run-all": "^4.1.5", "rimraf": "^3.0.2", diff --git a/plugin/src/helpers/config.ts b/plugin/src/helpers/config.ts index 9cecc9cd..8f3b29ef 100644 --- a/plugin/src/helpers/config.ts +++ b/plugin/src/helpers/config.ts @@ -6,6 +6,8 @@ import { stripIndent } from 'common-tags' import fs, { existsSync } from 'fs-extra' import type { GatsbyConfig, PluginRef } from 'gatsby' +import { checkPackageVersion } from './files' + export async function spliceConfig({ startMarker, endMarker, @@ -38,11 +40,9 @@ export async function spliceConfig({ return fs.writeFile(fileName, out) } -function loadGatsbyConfig({ utils, publish }): GatsbyConfig | never { - const gatsbyConfigFile = path.resolve( - getGatsbyRoot(publish), - 'gatsby-config.js', - ) +function loadGatsbyConfig({ gatsbyRoot, utils }): GatsbyConfig | never { + const gatsbyConfigFile = path.resolve(gatsbyRoot, 'gatsby-config.js') + if (!existsSync(gatsbyConfigFile)) { return {} } @@ -65,14 +65,25 @@ function hasPlugin(plugins: PluginRef[], pluginName: string): boolean { ) } -export function checkGatsbyConfig({ utils, netlifyConfig }): void { +export async function checkConfig({ utils, netlifyConfig }): Promise { + const gatsbyRoot = getGatsbyRoot(netlifyConfig.build.publish) + // warn if gatsby-plugin-netlify is missing const gatsbyConfig = loadGatsbyConfig({ utils, - publish: netlifyConfig.build.publish, + gatsbyRoot, }) - if (!hasPlugin(gatsbyConfig.plugins, 'gatsby-plugin-netlify')) { + if (hasPlugin(gatsbyConfig.plugins, 'gatsby-plugin-netlify')) { + if ( + // prettier-ignore + !(await checkPackageVersion(gatsbyRoot, 'gatsby-plugin-netlify', '>=4.2.0',)) + ) { + console.error( + 'The plugin `gatsby-plugin-netlify` does not support DSG, please update to >=4.2.0', + ) + } + } else { console.error( 'Please install `gatsby-plugin-netlify` and enable it in your gatsby-config.js. https://www.gatsbyjs.com/plugins/gatsby-plugin-netlify/', ) diff --git a/plugin/src/helpers/files.ts b/plugin/src/helpers/files.ts index 8d162d0e..53689a5c 100644 --- a/plugin/src/helpers/files.ts +++ b/plugin/src/helpers/files.ts @@ -1,8 +1,16 @@ import os from 'os' import process from 'process' -import { copyFile, ensureDir, existsSync, readFile, writeFile } from 'fs-extra' +import { + copyFile, + ensureDir, + existsSync, + readFile, + writeFile, + readJson, +} from 'fs-extra' import { dirname, join, resolve } from 'pathe' +import semver from 'semver' const DEFAULT_LAMBDA_PLATFORM = 'linux' const DEFAULT_LAMBDA_ABI = '83' @@ -70,6 +78,26 @@ export const findModuleFromBase = ({ paths, candidates }): string | null => { return null } +export async function checkPackageVersion( + root: string, + name: string, + version: string, +): Promise { + const packagePath = findModuleFromBase({ + paths: [root], + candidates: [name], + }) + + let packageObj: { version: string } + try { + packageObj = await readJson(`${packagePath}/package.json`) + } catch { + return false + } + + return semver.satisfies(packageObj.version, version) +} + /** * When Gatsby runs a build, it copies binary dependencies to the "query-engine" folder. These are auto-detected, based * on the environment in which the build is running. This can cause problems if these don't match the lambda environment. diff --git a/plugin/src/index.ts b/plugin/src/index.ts index fe65d173..09d96d0f 100644 --- a/plugin/src/index.ts +++ b/plugin/src/index.ts @@ -7,7 +7,7 @@ import { existsSync } from 'fs-extra' import { normalizedCacheDir, restoreCache, saveCache } from './helpers/cache' import { - checkGatsbyConfig, + checkConfig, mutateConfig, shouldSkipFunctions, spliceConfig, @@ -32,7 +32,7 @@ export async function onPreBuild({ } await restoreCache({ utils, publish: PUBLISH_DIR }) - checkGatsbyConfig({ utils, netlifyConfig }) + await checkConfig({ utils, netlifyConfig }) } export async function onBuild({ From 658060ab5668f2581e4c247cde7f4ee1a700e14e Mon Sep 17 00:00:00 2001 From: Rob Stanford Date: Fri, 1 Apr 2022 18:07:43 +0100 Subject: [PATCH 4/5] chore: replace prettier ignore with eslint ignore --- plugin/src/helpers/config.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/plugin/src/helpers/config.ts b/plugin/src/helpers/config.ts index 8f3b29ef..3b55de10 100644 --- a/plugin/src/helpers/config.ts +++ b/plugin/src/helpers/config.ts @@ -1,3 +1,4 @@ +/* eslint-disable max-lines */ import { EOL } from 'os' import path from 'path' import process from 'process' @@ -76,8 +77,11 @@ export async function checkConfig({ utils, netlifyConfig }): Promise { if (hasPlugin(gatsbyConfig.plugins, 'gatsby-plugin-netlify')) { if ( - // prettier-ignore - !(await checkPackageVersion(gatsbyRoot, 'gatsby-plugin-netlify', '>=4.2.0',)) + !(await checkPackageVersion( + gatsbyRoot, + 'gatsby-plugin-netlify', + '>=4.2.0', + )) ) { console.error( 'The plugin `gatsby-plugin-netlify` does not support DSG, please update to >=4.2.0', @@ -173,3 +177,4 @@ export function shouldSkipFunctions(cacheDir: string): boolean { export function getGatsbyRoot(publish: string): string { return path.resolve(path.dirname(publish)) } +/* eslint-enable max-lines */ From 1f95c1a6f06c5161c7f87d5c121e1da0512b33a3 Mon Sep 17 00:00:00 2001 From: Rob Stanford Date: Fri, 1 Apr 2022 18:08:34 +0100 Subject: [PATCH 5/5] refactor: simplify checkPackageVersion --- plugin/src/helpers/files.ts | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/plugin/src/helpers/files.ts b/plugin/src/helpers/files.ts index 53689a5c..714041a0 100644 --- a/plugin/src/helpers/files.ts +++ b/plugin/src/helpers/files.ts @@ -83,19 +83,15 @@ export async function checkPackageVersion( name: string, version: string, ): Promise { - const packagePath = findModuleFromBase({ - paths: [root], - candidates: [name], - }) - - let packageObj: { version: string } try { - packageObj = await readJson(`${packagePath}/package.json`) + const packagePath = require.resolve(`${name}/package.json`, { + paths: [root], + }) + const packageObj = await readJson(packagePath) + return semver.satisfies(packageObj.version, version) } catch { return false } - - return semver.satisfies(packageObj.version, version) } /**