-
-
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.
- Loading branch information
Showing
2 changed files
with
55 additions
and
0 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
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,52 @@ | ||
#!/usr/bin/env node | ||
|
||
import { statSync, unlinkSync, writeFileSync } from 'node:fs' | ||
import { resolve } from 'node:path' | ||
|
||
const installMiddleware = (pagePath: string) => { | ||
writeFileSync( | ||
resolve(__dirname, '../..', pagePath, '_middleware.js'), | ||
scriptText | ||
) | ||
} | ||
|
||
const removeMiddleware = (pagePath: string) => { | ||
;['js', 'ts'].forEach((ext) => { | ||
const path = resolve(__dirname, '../..', pagePath, `_middleware.${ext}`) | ||
if ( | ||
statSync(path, { | ||
throwIfNoEntry: false | ||
}) | ||
) { | ||
unlinkSync(path) | ||
} | ||
}) | ||
} | ||
|
||
const scriptText = `// NOTE: in this location, this file does nothing | ||
// scripts/split.mjs will copy this file to /pages/_middleware.js at | ||
// install/deploy if process.env.NEXT_SPLIT_ACTIVE == 1 | ||
export { middleware } from 'next-with-split' | ||
` | ||
|
||
const main = () => { | ||
const command = process.argv[2] | ||
const pagesPath = process.argv[3] | ||
|
||
switch (command) { | ||
case 'remove': | ||
console.info( | ||
`split traffic disabled, removing middleware from ${pagesPath}` | ||
) | ||
removeMiddleware(pagesPath) | ||
break | ||
case 'install': | ||
console.info( | ||
`split traffic enabled, installing middleware to ${pagesPath}` | ||
) | ||
installMiddleware(pagesPath) | ||
break | ||
} | ||
} | ||
|
||
main() |