Skip to content

Commit

Permalink
feat(withSplit): passable rewrite rules manually
Browse files Browse the repository at this point in the history
  • Loading branch information
aiji42 committed Jun 29, 2021
1 parent 0f1b2ab commit f26b23c
Show file tree
Hide file tree
Showing 5 changed files with 455 additions and 157 deletions.
68 changes: 68 additions & 0 deletions src/make-rewrites.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { makeRewrites } from './make-rewrites'

describe('makeRewrites', () => {
it('must return inactiveRewiteRule when no challenger', () => {
return makeRewrites({ main: '' }, 'top', true, undefined)().then((res) => {
expect(res).toEqual({
beforeFiles: [{ destination: '/top', source: '/' }]
})
})
})
it('must return inactiveRewiteRule when active flag is not true', () => {
return makeRewrites(
{ main: '', challenger: 'https://example.com' },
'top',
false,
undefined
)().then((res) => {
expect(res).toEqual({
beforeFiles: [{ destination: '/top', source: '/' }]
})
})
})
it('must return activeRewiteRule when has mappings and active flag is true', () => {
return makeRewrites(
{ main: '', challenger: 'https://example.com' },
'top',
true,
undefined
)().then((res) => {
expect(res).toEqual({
beforeFiles: [
{
destination: '/top/',
has: [{ key: 'next-with-split', type: 'cookie', value: 'main' }],
source: '/'
},
{
destination: '/:path*',
has: [{ key: 'next-with-split', type: 'cookie', value: 'main' }],
source: '/:path*/'
},
{
destination: 'https://example.com/top/',
has: [
{ key: 'next-with-split', type: 'cookie', value: 'challenger' }
],
source: '/'
},
{
destination: 'https://example.com/:path*',
has: [
{ key: 'next-with-split', type: 'cookie', value: 'challenger' }
],
source: '/:path*/'
},
{
destination: 'https://example.com/:path*',
has: [
{ key: 'next-with-split', type: 'cookie', value: 'challenger' }
],
source: '/:path*'
},
{ destination: '/_split-challenge', source: '/:path*/' }
]
})
})
})
})
56 changes: 56 additions & 0 deletions src/make-rewrites.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Rewrite, RouteHas } from 'next/dist/lib/load-custom-routes'
import { mergeRewrites } from './merge-rewrites'
const rule = (
source: string,
destination: string,
additional = {}
): Rewrite => ({
source,
destination,
...additional
})
const has = (value: string): RouteHas[] => [
{
type: 'cookie',
key: 'next-with-split',
value
}
]

export type Mappings = { [branch: string]: string }

export type Rewrites = {
beforeFiles?: Rewrite[]
afterFiles?: Rewrite[]
fallback?: Rewrite[]
}

export const makeRewrites =
(
mappings: Mappings,
rootPage: string,
active: boolean,
originalRewrite: (() => Promise<Rewrites | Rewrite[]>) | undefined
) =>
async (): Promise<Rewrites> => {
const rewrite = await originalRewrite?.()
if (!active || Object.keys(mappings).length < 2)
return mergeRewrites(rewrite, {
beforeFiles: [rule('/', `/${rootPage}`)]
})

return mergeRewrites(rewrite, {
beforeFiles: [
...Object.entries(mappings)
.map(([branch, origin]) => [
rule('/', `${origin}/${rootPage}/`, { has: has(branch) }),
rule('/:path*/', `${origin}/:path*`, { has: has(branch) }),
...(origin
? [rule('/:path*', `${origin}/:path*`, { has: has(branch) })]
: [])
])
.flat(),
rule('/:path*/', '/_split-challenge')
]
})
}
Loading

0 comments on commit f26b23c

Please sign in to comment.