-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(gatsby-plugin-gatsby-cloud): Remove sibling detection code and re…
…ly on match paths (#29610) * Remove sibling detection code and rely on match paths * Update snapshot Co-authored-by: gatsbybot <mathews.kyle+gatsbybot@gmail.com> (cherry picked from commit 6016c26)
- Loading branch information
1 parent
01d07b3
commit ad2c204
Showing
4 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
packages/gatsby-plugin-gatsby-cloud/src/__tests__/__snapshots__/create-redirects.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`create-redirects should assemble a redirects file 1`] = `"{\\"redirects\\":[{\\"fromPath\\":\\"/old-url\\",\\"toPath\\":\\"/new-url\\",\\"isPermanent\\":true},{\\"fromPath\\":\\"/url_that_is/not_pretty\\",\\"toPath\\":\\"/pretty/url\\",\\"statusCode\\":201}],\\"rewrites\\":[{\\"fromPath\\":\\"/url_that_is/ugly\\",\\"toPath\\":\\"/not_ugly/url\\"},{\\"fromPath\\":\\"/path2/*splatparam\\",\\"toPath\\":\\"/path2/[...splatparam]/\\"},{\\"fromPath\\":\\"/path/*\\",\\"toPath\\":\\"/path/[...]/\\"},{\\"fromPath\\":\\"/path3/:level1/:level2\\",\\"toPath\\":\\"/path3/[level1]/[level2]/\\"},{\\"fromPath\\":\\"/path4/:param1\\",\\"toPath\\":\\"/path4/[param1]/\\"}]}"`; |
17 changes: 17 additions & 0 deletions
17
packages/gatsby-plugin-gatsby-cloud/src/create-redirects.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { writeFile } from "fs-extra" | ||
import { REDIRECTS_FILENAME } from "./constants" | ||
|
||
export default async function writeRedirectsFile( | ||
pluginData, | ||
redirects, | ||
rewrites | ||
) { | ||
const { publicFolder } = pluginData | ||
|
||
if (!redirects.length && !rewrites.length) return null | ||
|
||
// Is it ok to pass through the data or should we format it so that we don't have dependencies | ||
// between the redirects and rewrites formats? What are the chances those will change? | ||
const FILE_PATH = publicFolder(REDIRECTS_FILENAME) | ||
return writeFile(FILE_PATH, JSON.stringify({ redirects, rewrites })) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import WebpackAssetsManifest from "webpack-assets-manifest" | ||
|
||
import makePluginData from "./plugin-data" | ||
import buildHeadersProgram from "./build-headers-program" | ||
import createRedirects from "./create-redirects" | ||
import { readJSON } from "fs-extra" | ||
import { joinPath } from "gatsby-core-utils" | ||
import { DEFAULT_OPTIONS, BUILD_HTML_STAGE, BUILD_CSS_STAGE } from "./constants" | ||
|
||
let assetsManifest = {} | ||
|
||
// Inject a webpack plugin to get the file manifests so we can translate all link headers | ||
exports.onCreateWebpackConfig = ({ actions, stage }) => { | ||
if (stage !== BUILD_HTML_STAGE && stage !== BUILD_CSS_STAGE) { | ||
return | ||
} | ||
|
||
actions.setWebpackConfig({ | ||
plugins: [ | ||
new WebpackAssetsManifest({ | ||
assets: assetsManifest, // mutates object with entries | ||
merge: true, | ||
}), | ||
], | ||
}) | ||
} | ||
|
||
exports.onPostBuild = async ( | ||
{ store, pathPrefix, reporter }, | ||
userPluginOptions | ||
) => { | ||
const pluginData = makePluginData(store, assetsManifest, pathPrefix) | ||
const pluginOptions = { ...DEFAULT_OPTIONS, ...userPluginOptions } | ||
|
||
const { redirects } = store.getState() | ||
|
||
let rewrites = [] | ||
if (pluginOptions.generateMatchPathRewrites) { | ||
const matchPathsFile = joinPath( | ||
pluginData.program.directory, | ||
`.cache`, | ||
`match-paths.json` | ||
) | ||
|
||
const matchPaths = await readJSON(matchPathsFile) | ||
|
||
rewrites = matchPaths.map(({ matchPath, path }) => { | ||
return { | ||
fromPath: matchPath, | ||
toPath: path, | ||
} | ||
}) | ||
} | ||
|
||
await Promise.all([ | ||
buildHeadersProgram(pluginData, pluginOptions, reporter), | ||
createRedirects(pluginData, redirects, rewrites), | ||
]) | ||
} | ||
|
||
const MATCH_ALL_KEYS = /^/ | ||
const pluginOptionsSchema = function ({ Joi }) { | ||
const headersSchema = Joi.object() | ||
.pattern(MATCH_ALL_KEYS, Joi.array().items(Joi.string())) | ||
.description(`Add more headers to specific pages`) | ||
|
||
return Joi.object({ | ||
headers: headersSchema, | ||
allPageHeaders: Joi.array() | ||
.items(Joi.string()) | ||
.description(`Add more headers to all the pages`), | ||
mergeSecurityHeaders: Joi.boolean().description( | ||
`When set to false, turns off the default security headers` | ||
), | ||
mergeLinkHeaders: Joi.boolean().description( | ||
`When set to false, turns off the default gatsby js headers` | ||
), | ||
mergeCachingHeaders: Joi.boolean().description( | ||
`When set to false, turns off the default caching headers` | ||
), | ||
transformHeaders: Joi.function() | ||
.maxArity(2) | ||
.description( | ||
`Transform function for manipulating headers under each path (e.g.sorting), etc. This should return an object of type: { key: Array<string> }` | ||
), | ||
generateMatchPathRewrites: Joi.boolean().description( | ||
`When set to false, turns off automatic creation of redirect rules for client only paths` | ||
), | ||
}) | ||
} | ||
|
||
exports.pluginOptionsSchema = pluginOptionsSchema |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import path from "path" | ||
|
||
export function buildPrefixer(prefix, ...paths) { | ||
return (...subpaths) => path.join(prefix, ...paths, ...subpaths) | ||
} | ||
|
||
// This function assembles data across the manifests and store to match a similar | ||
// shape of `static-entry.js`. With it, we can build headers that point to the correct | ||
// hashed filenames and ensure we pull in the componentChunkName. | ||
export default function makePluginData(store, assetsManifest, pathPrefix) { | ||
const { program, pages: storePages } = store.getState() | ||
const publicFolder = buildPrefixer(program.directory, `public`) | ||
const stats = require(publicFolder(`webpack.stats.json`)) | ||
// Get all the files, not just the first | ||
const chunkManifest = stats.assetsByChunkName | ||
const pages = storePages | ||
|
||
// We combine the manifest of JS and the manifest of assets to make a lookup table. | ||
const manifest = { ...assetsManifest, ...chunkManifest } | ||
|
||
return { | ||
pages, | ||
manifest, | ||
program, | ||
pathPrefix, | ||
publicFolder, | ||
} | ||
} |