Skip to content

feat: Add support for advanced redirect types #117

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

Merged
merged 4 commits into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/__tests__/__snapshots__/create-redirects.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`create-redirects writes file 1`] = `
"
## Created with gatsby-plugin-netlify
/foo /bar 302
/blog /canada/blog 302 Country=ca
/speaker /english/speaker 302 Language=en
/speaker /french/speaker 302 Language=fr
/param id=7 /test 302
/dogs/* /animals/:splat 302
/cats id=:id /animals/:id 302"
`;
175 changes: 2 additions & 173 deletions src/__tests__/build-headers-program.ts
Original file line number Diff line number Diff line change
@@ -1,179 +1,9 @@
/* eslint-disable max-lines, max-nested-callbacks */
import { tmpdir } from 'os'
import { join } from 'path'

import { existsSync, mkdtemp, readFile } from 'fs-extra'
import { existsSync, readFile } from 'fs-extra'

import buildHeadersProgram from '../build-headers-program'
import { DEFAULT_OPTIONS } from '../constants'

// eslint-disable-next-line max-lines-per-function
const createPluginData = async () => {
const tmpDir = await mkdtemp(join(tmpdir(), `gatsby-plugin-netlify-`))

return {
components: new Map([
[
1,
{
componentChunkName: `component---node-modules-gatsby-plugin-offline-app-shell-js`,
},
],
[
2,
{
componentChunkName: `component---src-templates-blog-post-js`,
},
],
[
3,
{
componentChunkName: `component---src-pages-404-js`,
},
],
[
4,
{
componentChunkName: `component---src-pages-index-js`,
},
],
]),
pages: new Map([
[
`/offline-plugin-app-shell-fallback/`,
{
jsonName: `offline-plugin-app-shell-fallback-a30`,
internalComponentName: `ComponentOfflinePluginAppShellFallback`,
path: `/offline-plugin-app-shell-fallback/`,
matchPath: undefined,
componentChunkName: `component---node-modules-gatsby-plugin-offline-app-shell-js`,
isCreatedByStatefulCreatePages: false,
context: {},
updatedAt: 1_557_740_602_268,
pluginCreator___NODE: `63e5f7ff-e5f1-58f7-8e2c-55872ac42281`,
pluginCreatorId: `63e5f7ff-e5f1-58f7-8e2c-55872ac42281`,
},
],
[
`/hi-folks/`,
{
jsonName: `hi-folks-a2b`,
internalComponentName: `ComponentHiFolks`,
path: `/hi-folks/`,
matchPath: undefined,
componentChunkName: `component---src-templates-blog-post-js`,
isCreatedByStatefulCreatePages: false,
context: {},
updatedAt: 1_557_740_602_330,
pluginCreator___NODE: `7374ebf2-d961-52ee-92a2-c25e7cb387a9`,
pluginCreatorId: `7374ebf2-d961-52ee-92a2-c25e7cb387a9`,
},
],
[
`/my-second-post/`,
{
jsonName: `my-second-post-2aa`,
internalComponentName: `ComponentMySecondPost`,
path: `/my-second-post/`,
matchPath: undefined,
componentChunkName: `component---src-templates-blog-post-js`,
isCreatedByStatefulCreatePages: false,
context: {},
updatedAt: 1_557_740_602_333,
pluginCreator___NODE: `7374ebf2-d961-52ee-92a2-c25e7cb387a9`,
pluginCreatorId: `7374ebf2-d961-52ee-92a2-c25e7cb387a9`,
},
],
[
`/hello-world/`,
{
jsonName: `hello-world-8bc`,
internalComponentName: `ComponentHelloWorld`,
path: `/hello-world/`,
matchPath: undefined,
componentChunkName: `component---src-templates-blog-post-js`,
isCreatedByStatefulCreatePages: false,
context: {},
updatedAt: 1_557_740_602_335,
pluginCreator___NODE: `7374ebf2-d961-52ee-92a2-c25e7cb387a9`,
pluginCreatorId: `7374ebf2-d961-52ee-92a2-c25e7cb387a9`,
},
],
[
`/404/`,
{
jsonName: `404-22d`,
internalComponentName: `Component404`,
path: `/404/`,
matchPath: undefined,
componentChunkName: `component---src-pages-404-js`,
isCreatedByStatefulCreatePages: true,
context: {},
updatedAt: 1_557_740_602_358,
pluginCreator___NODE: `049c1cfd-95f7-5555-a4ac-9b396d098b26`,
pluginCreatorId: `049c1cfd-95f7-5555-a4ac-9b396d098b26`,
},
],
[
`/`,
{
jsonName: `index`,
internalComponentName: `ComponentIndex`,
path: `/`,
matchPath: undefined,
componentChunkName: `component---src-pages-index-js`,
isCreatedByStatefulCreatePages: true,
context: {},
updatedAt: 1_557_740_602_361,
pluginCreator___NODE: `049c1cfd-95f7-5555-a4ac-9b396d098b26`,
pluginCreatorId: `049c1cfd-95f7-5555-a4ac-9b396d098b26`,
},
],
[
`/404.html`,
{
jsonName: `404-html-516`,
internalComponentName: `Component404Html`,
path: `/404.html`,
matchPath: undefined,
componentChunkName: `component---src-pages-404-js`,
isCreatedByStatefulCreatePages: true,
context: {},
updatedAt: 1_557_740_602_382,
pluginCreator___NODE: `f795702c-a3b8-5a88-88ee-5d06019d44fa`,
pluginCreatorId: `f795702c-a3b8-5a88-88ee-5d06019d44fa`,
},
],
]),
manifest: {
'main.js': `render-page.js`,
'main.js.map': `render-page.js.map`,
app: [
`webpack-runtime-acaa8994f1f704475e21.js`,
`styles.1025963f4f2ec7abbad4.css`,
`styles-565f081c8374bbda155f.js`,
`app-f33c13590352da20930f.js`,
],
'component---node-modules-gatsby-plugin-offline-app-shell-js': [
`component---node-modules-gatsby-plugin-offline-app-shell-js-78f9e4dea04737fa062d.js`,
],
'component---src-templates-blog-post-js': [
`0-0180cd94ef2497ac7db8.js`,
`component---src-templates-blog-post-js-517987eae96e75cddbe7.js`,
],
'component---src-pages-404-js': [
`0-0180cd94ef2497ac7db8.js`,
`component---src-pages-404-js-53e6c51a5a7e73090f50.js`,
],
'component---src-pages-index-js': [
`0-0180cd94ef2497ac7db8.js`,
`component---src-pages-index-js-0bdd01c77ee09ef0224c.js`,
],
},
pathPrefix: ``,
publicFolder: (...files: any[]) => join(tmpDir, ...files),
}
}
import { createPluginData } from './helpers'

jest.mock(`fs-extra`, () => {
const actualFsExtra = jest.requireActual(`fs-extra`)
Expand Down Expand Up @@ -308,4 +138,3 @@ describe(`build-headers-program`, () => {
expect(reporter.panic).toHaveBeenCalled()
})
})
/* eslint-enable max-lines, max-nested-callbacks */
89 changes: 89 additions & 0 deletions src/__tests__/create-redirects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { readFile } from 'fs-extra'

import createRedirects from '../create-redirects'

import { createPluginData } from './helpers'

jest.mock(`fs-extra`, () => {
const actualFsExtra = jest.requireActual(`fs-extra`)
return {
...actualFsExtra,
}
})

const redirects = [
{
"fromPath": "/foo",
"isPermanent": false,
"ignoreCase": true,
"redirectInBrowser": false,
"toPath": "/bar"
},
{
"fromPath": "/blog",
"isPermanent": false,
"ignoreCase": true,
"redirectInBrowser": false,
"toPath": "/canada/blog",
"conditions": {
"country": "ca"
}
},
{
"fromPath": "/speaker",
"isPermanent": false,
"ignoreCase": true,
"redirectInBrowser": false,
"toPath": "/english/speaker",
"conditions": {
"language": [
"en"
]
}
},
{
"fromPath": "/speaker",
"isPermanent": false,
"ignoreCase": true,
"redirectInBrowser": false,
"toPath": "/french/speaker",
"conditions": {
"language": [
"fr"
]
}
},
{
"fromPath": "/param?id=7",
"isPermanent": false,
"ignoreCase": true,
"redirectInBrowser": false,
"toPath": "/test"
},
{
"fromPath": "/dogs/*",
"isPermanent": false,
"ignoreCase": true,
"redirectInBrowser": false,
"toPath": "/animals/*"
},
{
"fromPath": "/cats?id=:id",
"isPermanent": false,
"ignoreCase": true,
"redirectInBrowser": false,
"toPath": "/animals/:id"
}
]

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

it(`writes file`, async () => {
const pluginData = await createPluginData()

await createRedirects(pluginData, redirects, [])

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