-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(withSplit): passable rewrite rules manually
- Loading branch information
Showing
5 changed files
with
455 additions
and
157 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
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*/' } | ||
] | ||
}) | ||
}) | ||
}) | ||
}) |
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,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') | ||
] | ||
}) | ||
} |
Oops, something went wrong.