-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add createRedirects action and Netlify support #2185
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b94a651
Added createRedirect actio+reducer
bvaughn 9d4d9b7
Added redirect support to develop mode
bvaughn e306cb6
Netlify plugin writes redirects to _redirects file
bvaughn 6344172
Moving packages/gatsby-plugin-netlify to packages/gatsby-plugin
bvaughn 6ed8460
Renamed string references for 'gatsby-plugin-netlify-headers' to 'gat…
bvaughn 57eedeb
Typo fix
bvaughn 9020566
Fixed .gitignore
bvaughn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/*.js | ||
!index.js | ||
yarn.lock |
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
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...atsby-plugin-netlify-headers/package.json → packages/gatsby-plugin-netlify/package.json
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
File renamed without changes.
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,16 @@ | ||
import fs from "fs" | ||
import pify from "pify" | ||
|
||
const writeFile = pify(fs.writeFile) | ||
|
||
export default 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}` | ||
}) | ||
|
||
return writeFile(publicFolder(`_redirects`), data.join(`\n`)) | ||
} | ||
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 |
---|---|---|
|
@@ -4,6 +4,7 @@ import WebpackAssetsManifest from "webpack-assets-manifest" | |
|
||
import makePluginData from "./plugin-data" | ||
import buildHeadersProgram from "./build-headers-program" | ||
import createRedirects from "./create-redirects" | ||
import { DEFAULT_OPTIONS, BUILD_HTML_STAGE, BUILD_CSS_STAGE } from "./constants" | ||
|
||
let assetsManifest = {} | ||
|
@@ -32,5 +33,8 @@ exports.onPostBuild = async ({ store, pathPrefix }, userPluginOptions) => { | |
const pluginData = makePluginData(store, assetsManifest, pathPrefix) | ||
const pluginOptions = { ...DEFAULT_OPTIONS, ...userPluginOptions } | ||
|
||
return buildHeadersProgram(pluginData, pluginOptions) | ||
const { redirects } = store.getState() | ||
|
||
await buildHeadersProgram(pluginData, pluginOptions) | ||
await createRedirects(pluginData, redirects) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this could be a promise.all, so they can run in parallel instead of series? |
||
} |
File renamed without changes.
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
34 changes: 34 additions & 0 deletions
34
packages/gatsby/src/internal-plugins/query-runner/redirects-writer.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,34 @@ | ||
import _ from "lodash" | ||
import fs from "fs-extra" | ||
import { store, emitter } from "../../redux/" | ||
import { joinPath } from "../../utils/path" | ||
|
||
const writeRedirects = async () => { | ||
bootstrapFinished = true | ||
|
||
let { program, redirects } = store.getState() | ||
|
||
await fs.writeFile( | ||
joinPath(program.directory, `.cache/redirects.json`), | ||
JSON.stringify(redirects, null, 2) | ||
) | ||
} | ||
|
||
exports.writeRedirects = writeRedirects | ||
|
||
let bootstrapFinished = false | ||
let oldRedirects | ||
const debouncedWriteRedirects = _.debounce(() => { | ||
// Don't write redirects again until bootstrap has finished. | ||
if ( | ||
bootstrapFinished && | ||
!_.isEqual(oldRedirects, store.getState().redirects) | ||
) { | ||
writeRedirects() | ||
oldRedirects = store.getState().Redirects | ||
} | ||
}, 250) | ||
|
||
emitter.on(`CREATE_REDIRECT`, () => { | ||
debouncedWriteRedirects() | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module.exports = (state = [], action) => { | ||
switch (action.type) { | ||
case `CREATE_REDIRECT`: | ||
return [...state, action.payload] | ||
default: | ||
return state | ||
} | ||
} |
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 |
---|---|---|
|
@@ -171,6 +171,6 @@ module.exports = { | |
], | ||
}, | ||
}, | ||
`gatsby-plugin-netlify-headers`, | ||
`gatsby-plugin-netlify`, | ||
], | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we could add a plugin option to disable this as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any harm in writing an empty
_redirects
file? Or are you concerned that we might override an existing one? (Guess it would be worth checking to see if we should append vs write.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right—someone might want to make one by hand through the static folder rather than go through generation in this plugin.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll create a follow-up PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in PR #2191