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: diff --git a/plugin/package-lock.json b/plugin/package-lock.json index 936fb16f..d074f2de 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", @@ -6161,6 +6163,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", @@ -29683,6 +29691,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..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' @@ -6,6 +7,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 +41,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 +66,28 @@ 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 ( + !(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/', ) @@ -162,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 */ diff --git a/plugin/src/helpers/files.ts b/plugin/src/helpers/files.ts index 8d162d0e..714041a0 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,22 @@ export const findModuleFromBase = ({ paths, candidates }): string | null => { return null } +export async function checkPackageVersion( + root: string, + name: string, + version: string, +): Promise { + try { + const packagePath = require.resolve(`${name}/package.json`, { + paths: [root], + }) + const packageObj = await readJson(packagePath) + return semver.satisfies(packageObj.version, version) + } catch { + return false + } +} + /** * 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 bd9d477e..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({ @@ -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({