-
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>
- Loading branch information
1 parent
308d418
commit 6016c26
Showing
6 changed files
with
39 additions
and
175 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
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 |
---|---|---|
@@ -1,3 +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\\":\\"/path3/:level1/:level2\\",\\"toPath\\":\\"/path3/[level1]/[level2]/\\"},{\\"fromPath\\":\\"/url_that_is/ugly\\",\\"toPath\\":\\"/not_ugly/url\\"},{\\"fromPath\\":\\"/path2/*splatparam\\",\\"toPath\\":\\"/path2/[...splatparam]/\\"},{\\"fromPath\\":\\"/path4/:param1\\",\\"toPath\\":\\"/path4/[param1]/\\"},{\\"fromPath\\":\\"/path/*\\",\\"toPath\\":\\"/path/[...]/\\"}]}"`; | ||
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]/\\"}]}"`; |
22 changes: 0 additions & 22 deletions
22
packages/gatsby-plugin-gatsby-cloud/src/__tests__/is-sibling.js
This file was deleted.
Oops, something went wrong.
77 changes: 2 additions & 75 deletions
77
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 |
---|---|---|
@@ -1,90 +1,17 @@ | ||
import { writeFile } from "fs-extra" | ||
import { REDIRECTS_FILENAME } from "./constants" | ||
|
||
/** | ||
* Get all rewrites and sort them by most specific at the top | ||
* code is based on @reach/router match utility (https://github.com/reach/router/blob/152aff2352bc62cefc932e1b536de9efde6b64a5/src/lib/utils.js#L224-L254) | ||
*/ | ||
|
||
const paramRe = /^:(.+)/ | ||
|
||
const SEGMENT_POINTS = 4 | ||
const STATIC_POINTS = 3 | ||
const DYNAMIC_POINTS = 2 | ||
const SPLAT_PENALTY = 1 | ||
const ROOT_POINTS = 1 | ||
|
||
const isRootSegment = segment => segment === `` | ||
const isDynamic = segment => paramRe.test(segment) | ||
const isSplat = segment => segment === `*` | ||
|
||
const rankRoute = (route, index) => { | ||
const score = route.default | ||
? 0 | ||
: segmentize(route).reduce((score, segment) => { | ||
score += SEGMENT_POINTS | ||
if (isRootSegment(segment)) score += ROOT_POINTS | ||
else if (isDynamic(segment)) score += DYNAMIC_POINTS | ||
else if (isSplat(segment)) score -= SEGMENT_POINTS + SPLAT_PENALTY | ||
else score += STATIC_POINTS | ||
return score | ||
}, 0) | ||
return { route, score, index } | ||
} | ||
|
||
const rankRoutes = routes => | ||
routes | ||
.map(rankRoute) | ||
.sort((a, b) => | ||
a.score < b.score ? 1 : a.score > b.score ? -1 : a.index - b.index | ||
) | ||
|
||
const segmentize = uri => | ||
uri | ||
// strip starting/ending slashes | ||
.replace(/(^\/+|\/+$)/g, ``) | ||
.split(`/`) | ||
|
||
/** | ||
* rankRewrites | ||
* | ||
* We need to order rewrites in order of specificity because more specific rewrites have to be first in order. | ||
* i.e. /url_that_is/ugly is more specific than /path4/:param1 even though both have 2 url segments | ||
*/ | ||
const rankRewrites = rewrites => { | ||
let sortedRewrites = [] | ||
|
||
const fromPaths = rewrites.map(({ fromPath }) => fromPath) | ||
const rankedRoutes = rankRoutes(fromPaths) | ||
|
||
for (let { route } of rankedRoutes) { | ||
const rewrite = rewrites.find(rewrite => rewrite.fromPath === route) | ||
if (rewrite) { | ||
sortedRewrites.push(rewrite) | ||
} | ||
} | ||
|
||
return sortedRewrites | ||
} | ||
|
||
export default async function writeRedirectsFile( | ||
pluginData, | ||
redirects, | ||
rewrites, | ||
siblingStaticPaths | ||
rewrites | ||
) { | ||
const { publicFolder } = pluginData | ||
|
||
if (!redirects.length && !rewrites.length) return null | ||
|
||
// order rewrites based on priority | ||
const rankedRewrites = rankRewrites(rewrites) | ||
|
||
// 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: rankedRewrites, siblingStaticPaths }) | ||
) | ||
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
This file was deleted.
Oops, something went wrong.
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