Skip to content

Commit

Permalink
Merge pull request #1045 from EnterpriseDB/feature/evan/strip-legacy-…
Browse files Browse the repository at this point in the history
…redirects-from-netlify

strip nginx legacy redirects from netlify _redirects

Former-commit-id: 7bf9861
  • Loading branch information
epbarger authored Mar 9, 2021
2 parents 1941922 + be48fcc commit 0229dba
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
21 changes: 21 additions & 0 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const {
findPrevNextNavNodes,
configureRedirects,
configureLegacyRedirects,
readFile,
writeFile,
} = require('./src/constants/gatsby-node-utils.js');

const isBuild = process.env.NODE_ENV === 'production';
Expand Down Expand Up @@ -412,3 +414,22 @@ 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');

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`,
);
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 0 additions & 8 deletions scripts/post-build.sh

This file was deleted.

18 changes: 18 additions & 0 deletions src/constants/gatsby-node-utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const fs = require('fs');

const sortVersionArray = (versions) => {
return versions
.map((version) => version.replace(/\d+/g, (n) => +n + 100000))
Expand Down Expand Up @@ -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,
Expand All @@ -325,4 +341,6 @@ module.exports = {
findPrevNextNavNodes,
configureRedirects,
configureLegacyRedirects,
readFile,
writeFile,
};

0 comments on commit 0229dba

Please sign in to comment.