From f20de2e04fb5873058865c220ff2590c1057a368 Mon Sep 17 00:00:00 2001 From: Evan Barger Date: Mon, 8 Mar 2021 18:27:18 -0500 Subject: [PATCH 1/2] Remove post-build.sh - instead use onPostBuild to clean up _redirects Former-commit-id: ed4e09dd09f31540b7530603ca2f6c04b1f68e6d --- gatsby-node.js | 17 +++++++++++++++++ package.json | 2 +- scripts/post-build.sh | 8 -------- src/constants/gatsby-node-utils.js | 18 ++++++++++++++++++ 4 files changed, 36 insertions(+), 9 deletions(-) delete mode 100644 scripts/post-build.sh diff --git a/gatsby-node.js b/gatsby-node.js index 2e1ff8415df..57624d88202 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -23,6 +23,8 @@ const { findPrevNextNavNodes, configureRedirects, configureLegacyRedirects, + readFile, + writeFile, } = require('./src/constants/gatsby-node-utils.js'); const isBuild = process.env.NODE_ENV === 'production'; @@ -412,3 +414,18 @@ exports.onPreBootstrap = () => { `); }; + +exports.onPostBuild = async ({ reporter, pathPrefix }) => { + const originalRedirects = await readFile('public/_redirects'); + + // filter out legacyRedirects that are loaded via nginx, not netlify + let filteredRedirects = originalRedirects + .split('\n') + .filter((line) => !line.startsWith(`${pathPrefix}/edb-docs/`)) + .join('\n'); + + await writeFile( + 'public/_redirects', + `${filteredRedirects}\n\n# Netlify pathPrefix path rewrite\n${pathPrefix}/* /:splat 200`, + ); +}; diff --git a/package.json b/package.json index 701dd8d676f..5bfb9fb440a 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "develop": "gatsby develop", "config-sources": "python3 scripts/source/config_sources.py", "format": "prettier --write src/**/*.js gatsby-*.js", - "build": "gatsby build --prefix-paths && bash scripts/post-build.sh", + "build": "gatsby build --prefix-paths", "serve-build": "gatsby serve --prefix-paths", "update-icons": "git submodule update --init --remote && node scripts/createIconTypes.js && node scripts/createIconNames.js", "build-pdf": "python3 scripts/pdf/generate_pdf.py", diff --git a/scripts/post-build.sh b/scripts/post-build.sh deleted file mode 100644 index d903e459964..00000000000 --- a/scripts/post-build.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/sh - -echo "post-build script starting" -if test -f "public/_redirects"; then - echo "writing path prefix to public/_redirects" - printf "\n\n/docs/* /:splat 200" >> public/_redirects -fi -echo "post-build script complete" diff --git a/src/constants/gatsby-node-utils.js b/src/constants/gatsby-node-utils.js index 17b1386c69a..ef6d0950181 100644 --- a/src/constants/gatsby-node-utils.js +++ b/src/constants/gatsby-node-utils.js @@ -1,3 +1,5 @@ +const fs = require('fs'); + const sortVersionArray = (versions) => { return versions .map((version) => version.replace(/\d+/g, (n) => +n + 100000)) @@ -308,6 +310,20 @@ const configureLegacyRedirects = ({ }); }; +const readFile = (filePath) => + new Promise(function (resolve, reject) { + fs.readFile(filePath, 'utf8', function (err, data) { + err ? reject(err) : resolve(data); + }); + }); + +const writeFile = (filePath, data) => + new Promise(function (resolve, reject) { + fs.writeFile(filePath, data, function (err) { + err ? reject(err) : resolve(); + }); + }); + module.exports = { sortVersionArray, replacePathVersion, @@ -325,4 +341,6 @@ module.exports = { findPrevNextNavNodes, configureRedirects, configureLegacyRedirects, + readFile, + writeFile, }; From be48fcc5876950086205d8592d79f929fb322ff3 Mon Sep 17 00:00:00 2001 From: Evan Barger Date: Tue, 9 Mar 2021 12:40:34 -0500 Subject: [PATCH 2/2] Add warning if no filtering happens Former-commit-id: 335ba169dfe39e8ee73d5da194febca2a96a9b50 --- gatsby-node.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/gatsby-node.js b/gatsby-node.js index 57624d88202..d77f483bffd 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -424,6 +424,10 @@ exports.onPostBuild = async ({ reporter, pathPrefix }) => { .filter((line) => !line.startsWith(`${pathPrefix}/edb-docs/`)) .join('\n'); + if (filteredRedirects.length === originalRedirects.length) { + reporter.warn('no redirects were filtered out, did something change?'); + } + await writeFile( 'public/_redirects', `${filteredRedirects}\n\n# Netlify pathPrefix path rewrite\n${pathPrefix}/* /:splat 200`,