Skip to content

Commit

Permalink
fix(gatsby-plugin-gatsby-cloud): Always create redirects.json (#32845)
Browse files Browse the repository at this point in the history
* Always generate a redirects file in case a user removes all redirects

* Update packages/gatsby-plugin-gatsby-cloud/src/create-redirects.js

Co-authored-by: Ward Peeters <ward@coding-tech.com>

Co-authored-by: Sidhartha Chatterjee <me@sidharthachatterjee.com>
Co-authored-by: Ward Peeters <ward@coding-tech.com>
  • Loading branch information
3 people authored Aug 20, 2021
1 parent 0873388 commit 7f22a27
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

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]/\\"}]}"`;

exports[`create-redirects should assemble a redirects file even if there are no redirects 1`] = `"{\\"redirects\\":[],\\"rewrites\\":[]}"`;

exports[`create-redirects should remove pathPrefix from redirects 1`] = `"{\\"redirects\\":[{\\"fromPath\\":\\"/old-url\\",\\"toPath\\":\\"/new-url\\",\\"isPermanent\\":true},{\\"fromPath\\":\\"/url_that_is/not_pretty\\",\\"toPath\\":\\"/pretty/url\\",\\"statusCode\\":201}],\\"rewrites\\":[]}"`;
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@ jest.mock(`fs-extra`, () => {
})

describe(`create-redirects`, () => {
let reporter

beforeEach(() => {
reporter = {
warn: jest.fn(),
}
fs.existsSync.mockClear()
fs.existsSync.mockReturnValue(true)
})
Expand Down Expand Up @@ -165,6 +160,17 @@ describe(`create-redirects`, () => {
}
}

it(`should assemble a redirects file even if there are no redirects`, async () => {
const pluginData = await createPluginData()
await createRedirects(pluginData, [], [])

const output = await fs.readFile(
pluginData.publicFolder(REDIRECTS_FILENAME),
`utf8`
)
expect(output).toMatchSnapshot()
})

it(`should assemble a redirects file`, async () => {
const pluginData = await createPluginData()

Expand Down
5 changes: 1 addition & 4 deletions packages/gatsby-plugin-gatsby-cloud/src/create-redirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ import { REDIRECTS_FILENAME } from "./constants"
export default async function writeRedirectsFile(
pluginData,
redirects,
rewrites,
pathPrefix
rewrites
) {
const { publicFolder } = pluginData

if (!redirects.length && !rewrites.length) return null

// gatsby adds path-prefix to redirects so we need to remove them again
if (redirects && pluginData.pathPrefix) {
redirects = redirects.map(redirect => {
Expand Down

0 comments on commit 7f22a27

Please sign in to comment.