-
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.
gatsby-plugin-netlify tweaks (#2191)
* gatsby-plugin-netlify tweaks * Only write '_redirects' file if redirects have been declared. * Append to '_redirects' file is user has defined one already in the 'static' folder. * Write header and redirects files in parallel. * Replaced 'pify' package with 'fs-extra'. * Redirects plug-in hardening Don't override user redirects. Don't contiunously append to generated redirects either. * Bikeshedding code arrangement
- Loading branch information
1 parent
0e74553
commit 3960d09
Showing
5 changed files
with
41 additions
and
21 deletions.
There are no files selected for viewing
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 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 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 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,16 +1,36 @@ | ||
import fs from "fs" | ||
import pify from "pify" | ||
import { HEADER_COMMENT } from "./constants" | ||
import { appendFile, exists, readFile, writeFile } from "fs-extra" | ||
|
||
const writeFile = pify(fs.writeFile) | ||
|
||
export default function writeRedirectsFile(pluginData, redirects) { | ||
export default async function writeRedirectsFile(pluginData, redirects) { | ||
const { publicFolder } = pluginData | ||
|
||
// https://www.netlify.com/docs/redirects/ | ||
const data = redirects.map(redirect => { | ||
const status = redirect.isPermanent ? 301 : 302 | ||
return `${redirect.fromPath} ${redirect.toPath} ${status}` | ||
}) | ||
if (redirects.length > 0) { | ||
const FILE_PATH = publicFolder(`_redirects`) | ||
|
||
// Map redirect data to the format Netlify expects | ||
// https://www.netlify.com/docs/redirects/ | ||
redirects = redirects.map(redirect => { | ||
const status = redirect.isPermanent ? 301 : 302 | ||
return `${redirect.fromPath} ${redirect.toPath} ${status}` | ||
}) | ||
|
||
let appendToFile = 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) | ||
if (fileExists) { | ||
const fileContents = await readFile(FILE_PATH) | ||
if (fileContents.indexOf(HEADER_COMMENT) < 0) { | ||
appendToFile = true | ||
} | ||
} | ||
|
||
const data = `${HEADER_COMMENT}\n\n${redirects.join(`\n`)}` | ||
|
||
return writeFile(publicFolder(`_redirects`), data.join(`\n`)) | ||
return appendToFile | ||
? appendFile(FILE_PATH, `\n\n${data}`) | ||
: writeFile(FILE_PATH, data) | ||
} | ||
} |
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