Skip to content

Commit

Permalink
fix(gatsby-plugin-netlify): Fix heuristic for appending redirects (#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
sidharthachatterjee authored May 4, 2020
1 parent 4b0e2dd commit ff0f7cf
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions packages/gatsby-plugin-netlify/src/create-redirects.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HEADER_COMMENT } from "./constants"
import { appendFile, exists, readFile, writeFile } from "fs-extra"
import { exists, readFile, writeFile } from "fs-extra"

export default async function writeRedirectsFile(
pluginData,
Expand Down Expand Up @@ -54,22 +54,27 @@ export default async function writeRedirectsFile(
({ fromPath, toPath }) => `${fromPath} ${toPath} 200`
)

let appendToFile = false
let commentFound = false

// Websites may also have statically defined redirects
// In that case we should append to them (not overwrite)
// Make sure we aren't just looking at previous build results though
const fileExists = await exists(FILE_PATH)
let fileContents = ``
if (fileExists) {
const fileContents = await readFile(FILE_PATH)
if (fileContents.indexOf(HEADER_COMMENT) < 0) {
appendToFile = true
}
fileContents = await readFile(FILE_PATH, `utf8`)
commentFound = fileContents.includes(HEADER_COMMENT)
}
let data
if (commentFound) {
const [theirs] = fileContents.split(`\n${HEADER_COMMENT}\n`)
data = theirs
} else {
data = fileContents
}

const data = `${HEADER_COMMENT}\n\n${[...redirects, ...rewrites].join(`\n`)}`

return appendToFile
? appendFile(FILE_PATH, `\n\n${data}`)
: writeFile(FILE_PATH, data)
return writeFile(
FILE_PATH,
[data, HEADER_COMMENT, ...redirects, ...rewrites].join(`\n`)
)
}

0 comments on commit ff0f7cf

Please sign in to comment.