Skip to content

Commit

Permalink
feat: change the configuration values of middleware management
Browse files Browse the repository at this point in the history
  • Loading branch information
aiji42 committed Jan 25, 2022
1 parent dbb5e8d commit 8005bd1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
1 change: 0 additions & 1 deletion src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export type SplitOptions = {
[branchName: string]: string | { host: string; weight: number }
}
cookie?: CookieSerializeOptions
middleware?: string
}
}

Expand Down
28 changes: 13 additions & 15 deletions src/with-split.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ type WithSplitArgs = {
currentBranch?: string
isOriginal?: boolean
hostname?: string
middleware?: { manage?: boolean; paths?: string[] }
}

export const withSplit =
({ splits: _splits = {}, ...manuals }: WithSplitArgs) =>
({
splits: _splits = {},
middleware = { manage: false },
...manuals
}: WithSplitArgs) =>
(nextConfig: NextConfig): NextConfig => {
// Load the configuration using Spectrum.
const splits: SplitOptions =
Expand All @@ -21,7 +26,7 @@ export const withSplit =
: JSON.parse(process.env.SPLIT_CONFIG_BY_SPECTRUM ?? '{}')

if (['true', '1'].includes(process.env.SPLIT_DISABLE ?? '')) {
prepareMiddleware(splits, 'remove')
middleware.manage && manageMiddleware(middleware.paths ?? [], 'remove')
return nextConfig
}

Expand Down Expand Up @@ -49,7 +54,8 @@ export const withSplit =
)
}

prepareMiddleware(splits, isMain ? 'install' : 'remove')
middleware.manage &&
manageMiddleware(middleware.paths ?? [], isMain ? 'install' : 'remove')

if (isSubjectedSplitTest(splits, currentBranch))
process.env.NEXT_PUBLIC_IS_TARGET_SPLIT_TESTING = 'true'
Expand Down Expand Up @@ -88,18 +94,8 @@ const isSubjectedSplitTest = (
return branches.includes(currentBranch)
}

const getMiddlewarePaths = (splits: SplitOptions): string[] => {
return Object.values(splits)
.map(({ middleware }) => middleware)
.filter((path): path is string => !!path)
}

const prepareMiddleware = (
splits: SplitOptions,
command: 'install' | 'remove'
) => {
getMiddlewarePaths(splits).forEach((path) => {
console.log(path)
const manageMiddleware = (paths: string[], command: 'install' | 'remove') => {
paths.forEach((path) => {
exec(`npx next-with-split ${command} ${path}`, (err, stdout, stderr) => {
if (stdout) console.log(stdout)
if (err) {
Expand All @@ -109,4 +105,6 @@ const prepareMiddleware = (
if (stderr) throw new Error(stderr)
})
})

// TODO: Explores the pages directory and alerts if there is middleware outside of its control.
}

0 comments on commit 8005bd1

Please sign in to comment.