-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve sw versioning, try flags with stage
- Loading branch information
1 parent
d74a44f
commit 328948b
Showing
5 changed files
with
66 additions
and
4 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,3 @@ | ||
export type Stage = "local" | "REVIEW" | "STAGING" | "PRODUCTION"; | ||
|
||
export const stage = (process.env.STAGE || "local") as Stage; |
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 { Stage, stage as stageFromEnvironment } from "./environment"; | ||
|
||
/** | ||
* A union of the flag names (alphabetical order). | ||
*/ | ||
export type Flag = | ||
/** | ||
* Enables service worker registration. | ||
* | ||
* Registers the service worker and enables offline use. | ||
*/ | ||
"sw"; | ||
|
||
interface FlagMetadata { | ||
defaultOnStages: Stage[]; | ||
name: Flag; | ||
} | ||
|
||
const allFlags: FlagMetadata[] = [{ name: "sw", defaultOnStages: [] }]; | ||
|
||
type Flags = Record<Flag, boolean>; | ||
|
||
const flagsForParams = (stage: Stage, params: URLSearchParams) => { | ||
const enableFlags = new Set(params.getAll("flag")); | ||
const allFlagsDefault = enableFlags.has("none") | ||
? false | ||
: enableFlags.has("*") | ||
? true | ||
: undefined; | ||
return Object.fromEntries( | ||
allFlags.map((f) => [ | ||
f.name, | ||
isEnabled(f, stage, allFlagsDefault, enableFlags.has(f.name)), | ||
]) | ||
) as Flags; | ||
}; | ||
|
||
const isEnabled = ( | ||
f: FlagMetadata, | ||
stage: Stage, | ||
allFlagsDefault: boolean | undefined, | ||
thisFlagOn: boolean | ||
): boolean => { | ||
if (thisFlagOn) { | ||
return true; | ||
} | ||
if (allFlagsDefault !== undefined) { | ||
return allFlagsDefault; | ||
} | ||
return f.defaultOnStages.includes(stage); | ||
}; | ||
|
||
export const flags: Flags = (() => { | ||
const params = new URLSearchParams(window.location.search); | ||
return flagsForParams(stageFromEnvironment, params); | ||
})(); |
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