Skip to content

fix!: remove catch-all DSG redirect and handle discretely #334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 14 additions & 0 deletions plugin/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@
"node-stream-zip": "^1.15.0",
"pathe": "^0.2.0",
"pretty-bytes": "^5.6.0",
"semver": "^7.3.5",
"tempy": "^1.0.0"
},
"devDependencies": {
"@gatsbyjs/reach-router": "^1.3.6",
"@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",
Expand Down
32 changes: 24 additions & 8 deletions plugin/src/helpers/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable max-lines */
import { EOL } from 'os'
import path from 'path'
import process from 'process'
Expand All @@ -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,
Expand Down Expand Up @@ -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 {}
}
Expand All @@ -65,14 +66,28 @@ function hasPlugin(plugins: PluginRef[], pluginName: string): boolean {
)
}

export function checkGatsbyConfig({ utils, netlifyConfig }): void {
export async function checkConfig({ utils, netlifyConfig }): Promise<void> {
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/',
)
Expand Down Expand Up @@ -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 */
26 changes: 25 additions & 1 deletion plugin/src/helpers/files.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -70,6 +78,22 @@ export const findModuleFromBase = ({ paths, candidates }): string | null => {
return null
}

export async function checkPackageVersion(
root: string,
name: string,
version: string,
): Promise<boolean> {
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.
Expand Down
10 changes: 2 additions & 8 deletions plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { existsSync } from 'fs-extra'

import { normalizedCacheDir, restoreCache, saveCache } from './helpers/cache'
import {
checkGatsbyConfig,
checkConfig,
mutateConfig,
shouldSkipFunctions,
spliceConfig,
Expand All @@ -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({
Expand Down Expand Up @@ -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({
Expand Down