Netlify plugin, programmatic redirects

This commit is contained in:
Aaron Carlino 2019-12-16 13:53:53 +13:00
parent c57be05308
commit d59778b795
6 changed files with 105 additions and 42 deletions

3
.gitignore vendored
View File

@ -68,6 +68,3 @@ yarn-error.log
.pnp.js
# Yarn Integrity file
.yarn-integrity
# Auto-generated per context
static/_redirects

View File

@ -1,3 +1,5 @@
[![Netlify Status](https://api.netlify.com/api/v1/badges/98ac537e-14f6-4864-bf56-d5a60c76ccc9/deploy-status)](https://app.netlify.com/sites/ss-docs/deploys)
# doc.silverstripe.org
This repository contains the source code powering [SilverStripe's

View File

@ -12,6 +12,7 @@ module.exports = {
`gatsby-plugin-sass`,
`gatsby-plugin-sharp`,
`gatsby-plugin-sitemap`,
`gatsby-plugin-netlify`,
{
resolve: `gatsby-source-git`,
options: {
@ -121,11 +122,6 @@ module.exports = {
]
},
},
{
resolve: `gatsby-plugin-offline`,
options: {
precachePages: [`/en/4/developer_guides/**`],
},
}
`gatsby-plugin-remove-serviceworker`,
],
}

View File

@ -86,8 +86,8 @@ exports.onCreateNode = async ({ node, getNode, getNodesByType, actions, createNo
};
exports.createPages = async ({ actions, graphql }) => {
const { createPage } = actions;
exports.createPages = async ({ actions, graphql, getNodesByType }) => {
const { createPage, createRedirect } = actions;
const docTemplate = path.resolve(`src/templates/docs-template.tsx`);
const result = await graphql(`
@ -116,31 +116,22 @@ exports.createPages = async ({ actions, graphql }) => {
});
})
};
exports.onPostBuild = async ({ getNodesByType }) => {
console.log(`Writing legacy redirects...`);
const redirects = new Map();
const v4docs = getNodesByType('SilverstripeDocument').filter(n => n.slug.match(/^\/en\/4\//));
const v3docs = getNodesByType('SilverstripeDocument').filter(n => n.slug.match(/^\/en\/3\//));
console.log(`Creating legacy redirects...`);
const redirects = new Map();
const v4docs = getNodesByType('SilverstripeDocument').filter(n => n.slug.match(/^\/en\/4\//));
const v3docs = getNodesByType('SilverstripeDocument').filter(n => n.slug.match(/^\/en\/3\//));
[...v4docs, ...v3docs].forEach(n => {
const legacy = n.slug.replace(/^\/en\/[0-9]\//, '/en/');
if (!redirects.has(legacy)) {
redirects.set(legacy, n.slug);
}
});
redirects.forEach((toPath, fromPath) => {
createRedirect({ fromPath, toPath, isPermanent: true });
});
[...v4docs, ...v3docs].forEach(n => {
const legacy = n.slug.replace(/^\/en\/[0-9]\//, '/en/');
if (!redirects.has(legacy)) {
redirects.set(legacy, n.slug);
}
});
const lines = [];
redirects.forEach((slug, legacy) => {
lines.push(`${legacy} ${slug}`);
});
fs.writeFileSync(path.join(__dirname, 'static', '_redirects'),
`### This file is auto-generated. Do not modify ###
${lines.join("\n")}`
);
return Promise.resolve();
}
};

View File

@ -18,9 +18,11 @@
"gatsby": "^2.17.10",
"gatsby-image": "^2.2.30",
"gatsby-plugin-manifest": "^2.2.26",
"gatsby-plugin-netlify": "^2.1.30",
"gatsby-plugin-offline": "^3.0.18",
"gatsby-plugin-purgecss": "^4.0.1",
"gatsby-plugin-react-helmet": "^3.1.13",
"gatsby-plugin-remove-serviceworker": "^1.0.0",
"gatsby-plugin-sass": "^2.1.20",
"gatsby-plugin-sharp": "^2.2.36",
"gatsby-plugin-sitemap": "^2.2.20",
@ -43,6 +45,7 @@
"react-helmet": "^5.2.1",
"react-spring": "^8.0.27",
"resize-observer-polyfill": "^1.5.1",
"sharp": "^0.23.4",
"smooth-scroll": "^16.1.0",
"styled-components": "^4.4.1",
"unist-util-select": "^3.0.0"
@ -60,7 +63,8 @@
"typescript": "^3.7.2"
},
"keywords": [
"silverstripe", "documentation"
"silverstripe",
"documentation"
],
"license": "BSD-3",
"scripts": {

View File

@ -773,6 +773,13 @@
dependencies:
regenerator-runtime "^0.13.2"
"@babel/runtime@^7.7.6":
version "7.7.6"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.7.6.tgz#d18c511121aff1b4f2cd1d452f1bac9601dd830f"
integrity sha512-BWAJxpNVa0QlE5gZdWjSxXtemZyZ9RmrmVozxt3NUXeZhVIJ5ANyqmMc0JDrivBZyxUuQvFxlvH4OWWOogGfUw==
dependencies:
regenerator-runtime "^0.13.2"
"@babel/template@^7.7.0":
version "7.7.0"
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.7.0.tgz#4fadc1b8e734d97f56de39c77de76f2562e597d0"
@ -2829,7 +2836,7 @@ chalk@1.1.3, chalk@^1.0.0, chalk@^1.1.1, chalk@^1.1.3:
strip-ansi "^3.0.0"
supports-color "^2.0.0"
chalk@^2.0.0, chalk@^2.0.1, chalk@^2.1.0, chalk@^2.3.0, chalk@^2.4.1, chalk@^2.4.2:
chalk@^2.0, chalk@^2.0.0, chalk@^2.0.1, chalk@^2.1.0, chalk@^2.3.0, chalk@^2.4.1, chalk@^2.4.2:
version "2.4.2"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.2.tgz#cd42541677a54333cf541a49108c1432b44c9424"
integrity sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==
@ -5575,6 +5582,17 @@ gatsby-plugin-manifest@^2.2.26:
semver "^5.7.1"
sharp "^0.23.2"
gatsby-plugin-netlify@^2.1.30:
version "2.1.30"
resolved "https://registry.yarnpkg.com/gatsby-plugin-netlify/-/gatsby-plugin-netlify-2.1.30.tgz#e6412b399d5182f11bccce06c8dc0aef008d2f5d"
integrity sha512-JOiG9zlPwGSYiDdCqLcnGvAuo3k/bW7IBEqdo3qo+ZDQk515HWdCPuYHfwXXOW4gCUB+5OCxnafOxTCYj7QXbg==
dependencies:
"@babel/runtime" "^7.7.6"
fs-extra "^8.1.0"
kebab-hash "^0.1.2"
lodash "^4.17.15"
webpack-assets-manifest "^3.1.1"
gatsby-plugin-offline@^3.0.18:
version "3.0.18"
resolved "https://registry.yarnpkg.com/gatsby-plugin-offline/-/gatsby-plugin-offline-3.0.18.tgz#442d4e1c181028e81c0ebaa31132d6e2d240954c"
@ -5617,6 +5635,11 @@ gatsby-plugin-react-helmet@^3.1.13:
dependencies:
"@babel/runtime" "^7.6.3"
gatsby-plugin-remove-serviceworker@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/gatsby-plugin-remove-serviceworker/-/gatsby-plugin-remove-serviceworker-1.0.0.tgz#9fb433bc8bd766e14e1d3711c4ac6f051e1dff7c"
integrity sha1-n7QzvIvXZuFOHTcRxKxvBR4d/3w=
gatsby-plugin-sass@^2.1.20:
version "2.1.20"
resolved "https://registry.yarnpkg.com/gatsby-plugin-sass/-/gatsby-plugin-sass-2.1.20.tgz#a9a15c896fb9754f6e44ff7171400e7dd40e72d6"
@ -7801,6 +7824,13 @@ jsx-ast-utils@^2.2.1:
array-includes "^3.0.3"
object.assign "^4.1.0"
kebab-hash@^0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/kebab-hash/-/kebab-hash-0.1.2.tgz#dfb7949ba34d8e70114ea7d83e266e5e2a4abaac"
integrity sha512-BTZpq3xgISmQmAVzkISy4eUutsUA7s4IEFlCwOBJjvSFOwyR7I+fza+tBc/rzYWK/NrmFHjfU1IhO3lu29Ib/w==
dependencies:
lodash.kebabcase "^4.1.1"
keyv@3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/keyv/-/keyv-3.0.0.tgz#44923ba39e68b12a7cec7df6c3268c031f2ef373"
@ -7999,6 +8029,16 @@ lodash.foreach@^4.5.0:
resolved "https://registry.yarnpkg.com/lodash.foreach/-/lodash.foreach-4.5.0.tgz#1a6a35eace401280c7f06dddec35165ab27e3e53"
integrity sha1-Gmo16s5AEoDH8G3d7DUWWrJ+PlM=
lodash.get@^4.0:
version "4.4.2"
resolved "https://registry.yarnpkg.com/lodash.get/-/lodash.get-4.4.2.tgz#2d177f652fa31e939b4438d5341499dfa3825e99"
integrity sha1-LRd/ZS+jHpObRDjVNBSZ36OCXpk=
lodash.has@^4.0:
version "4.5.2"
resolved "https://registry.yarnpkg.com/lodash.has/-/lodash.has-4.5.2.tgz#d19f4dc1095058cccbe2b0cdf4ee0fe4aa37c862"
integrity sha1-0Z9NwQlQWMzL4rDN9O4P5Ko3yGI=
lodash.isplainobject@^4.0.6:
version "4.0.6"
resolved "https://registry.yarnpkg.com/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz#7c526a52d89b45c45cc690b88163be0497f550cb"
@ -8009,6 +8049,11 @@ lodash.isstring@^4.0.1:
resolved "https://registry.yarnpkg.com/lodash.isstring/-/lodash.isstring-4.0.1.tgz#d527dfb5456eca7cc9bb95d5daeaf88ba54a5451"
integrity sha1-1SfftUVuynzJu5XV2ur4i6VKVFE=
lodash.kebabcase@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/lodash.kebabcase/-/lodash.kebabcase-4.1.1.tgz#8489b1cb0d29ff88195cceca448ff6d6cc295c36"
integrity sha1-hImxyw0p/4gZXM7KRI/21swpXDY=
lodash.map@^4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash.map/-/lodash.map-4.6.0.tgz#771ec7839e3473d9c4cde28b19394c3562f4f6d3"
@ -8620,7 +8665,7 @@ mixin-deep@^1.2.0:
for-in "^1.0.2"
is-extendable "^1.0.1"
mkdirp@0.5.1, "mkdirp@>=0.5 0", mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@~0.5.1:
mkdirp@0.5.1, "mkdirp@>=0.5 0", mkdirp@^0.5, mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@~0.5.1:
version "0.5.1"
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.1.tgz#30057438eac6cf7f8c4767f38648d6697d75c903"
integrity sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=
@ -10245,7 +10290,7 @@ potrace@^2.1.2:
dependencies:
jimp "^0.6.4"
prebuild-install@^5.3.2:
prebuild-install@^5.3.2, prebuild-install@^5.3.3:
version "5.3.3"
resolved "https://registry.yarnpkg.com/prebuild-install/-/prebuild-install-5.3.3.tgz#ef4052baac60d465f5ba6bf003c9c1de79b9da8e"
integrity sha512-GV+nsUXuPW2p8Zy7SarF/2W/oiK8bFQgJcncoJ0d7kRpekEA0ftChjfEaF9/Y+QJEc/wFR7RAEa8lYByuUIe2g==
@ -11591,6 +11636,21 @@ sharp@^0.23.2:
tar "^5.0.5"
tunnel-agent "^0.6.0"
sharp@^0.23.4:
version "0.23.4"
resolved "https://registry.yarnpkg.com/sharp/-/sharp-0.23.4.tgz#ca36067cb6ff7067fa6c77b01651cb9a890f8eb3"
integrity sha512-fJMagt6cT0UDy9XCsgyLi0eiwWWhQRxbwGmqQT6sY8Av4s0SVsT/deg8fobBQCTDU5iXRgz0rAeXoE2LBZ8g+Q==
dependencies:
color "^3.1.2"
detect-libc "^1.0.3"
nan "^2.14.0"
npmlog "^4.1.2"
prebuild-install "^5.3.3"
semver "^6.3.0"
simple-get "^3.1.0"
tar "^5.0.5"
tunnel-agent "^0.6.0"
shebang-command@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/shebang-command/-/shebang-command-1.2.0.tgz#44aac65b695b03398968c39f363fee5deafdf1ea"
@ -13395,6 +13455,19 @@ web-namespaces@^1.0.0, web-namespaces@^1.1.2:
resolved "https://registry.yarnpkg.com/web-namespaces/-/web-namespaces-1.1.3.tgz#9bbf5c99ff0908d2da031f1d732492a96571a83f"
integrity sha512-r8sAtNmgR0WKOKOxzuSgk09JsHlpKlB+uHi937qypOu3PZ17UxPrierFKDye/uNHjNTTEshu5PId8rojIPj/tA==
webpack-assets-manifest@^3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/webpack-assets-manifest/-/webpack-assets-manifest-3.1.1.tgz#39bbc3bf2ee57fcd8ba07cda51c9ba4a3c6ae1de"
integrity sha512-JV9V2QKc5wEWQptdIjvXDUL1ucbPLH2f27toAY3SNdGZp+xSaStAgpoMcvMZmqtFrBc9a5pTS1058vxyMPOzRQ==
dependencies:
chalk "^2.0"
lodash.get "^4.0"
lodash.has "^4.0"
mkdirp "^0.5"
schema-utils "^1.0.0"
tapable "^1.0.0"
webpack-sources "^1.0.0"
webpack-dev-middleware@^3.7.2:
version "3.7.2"
resolved "https://registry.yarnpkg.com/webpack-dev-middleware/-/webpack-dev-middleware-3.7.2.tgz#0019c3db716e3fa5cecbf64f2ab88a74bab331f3"
@ -13470,7 +13543,7 @@ webpack-merge@^4.2.2:
dependencies:
lodash "^4.17.15"
webpack-sources@^1.1.0, webpack-sources@^1.4.0, webpack-sources@^1.4.1:
webpack-sources@^1.0.0, webpack-sources@^1.1.0, webpack-sources@^1.4.0, webpack-sources@^1.4.1:
version "1.4.3"
resolved "https://registry.yarnpkg.com/webpack-sources/-/webpack-sources-1.4.3.tgz#eedd8ec0b928fbf1cbfe994e22d2d890f330a933"
integrity sha512-lgTS3Xhv1lCOKo7SA5TjKXMjpSM4sBjNV5+q2bqesbSPs5FjGmU6jjtBSkX9b4qW87vDIsCIlUPOEhbZrMdjeQ==