Skip to content

chore: Support feature flags in SSR #2028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 1, 2024
Merged

chore: Support feature flags in SSR #2028

merged 1 commit into from
Mar 1, 2024

Conversation

just-boris
Copy link
Member

Description

Make it possible to define feature flags in SSR

// remove dark header
global[Symbol.for('awsui-global-flags')] = { removeHighContrastHeader: true };

Related links, issue #, if available: n/a

How has this been tested?

Added an SSR test suite

Review checklist

The following items are to be evaluated by the author(s) and the reviewer(s).

Correctness

  • Changes include appropriate documentation updates.
  • Changes are backward-compatible if not indicated, see CONTRIBUTING.md.
  • Changes do not include unsupported browser features, see CONTRIBUTING.md.
  • Changes were manually tested for accessibility, see accessibility guidelines.

Security

Testing

  • Changes are covered with new/existing unit tests?
  • Changes are covered with new/existing integration tests?

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

const { getGlobalFlag } = globalFlags;

const awsuiGlobalFlagsSymbol = Symbol.for('awsui-global-flags');
Copy link
Member Author

@just-boris just-boris Feb 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some unique symbol type madness here. Symbol.for(key) returns different symbols even if the key is the same: microsoft/TypeScript#35909

So I needed to add an import of awsuiGlobalFlagsSymbol. I also could not get it from const { ... } = globalFlags because it also would not be the same unique symbol.

But on the upside I managed to remove copies of these interfaces below while keeping everything type safe

return typeof window !== 'undefined' ? window : globalThis;
}

function readFlag(window: any, flagName: keyof GlobalFlags) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decided to go with any here, because as ExtendedWindow all around this file were not adding much type safety anyway. But at least the code is more compact now

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use unknown for the parameter type and cast it to FlagsHolder inside the function. That way, we'll get type checks on the window?.[... line

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you show an an example of the readFlag(window: unknown) implementation you ideally want?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found a version from you: 7048f87#diff-5b9d78b3482fe99531e24bedf68759f075aeca010bbcae8797110683d36eb35cR17-R18

I get it now, updated the implementation

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example:

function readFlag(object: unknown, flagName: keyof GlobalFlags) {
  if (!object) return undefined;

  const flagsHolder = object as FlagsHolder;

  return flagsHolder[awsuiGlobalFlagsSymbol]?.[flagName];
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, window: any made the return type of readFlag to be any which is unexpected. Applied your suggestion.

Copy link

codecov bot commented Feb 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.84%. Comparing base (39e52a1) to head (119bb70).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2028   +/-   ##
=======================================
  Coverage   94.83%   94.84%           
=======================================
  Files         669      669           
  Lines       18113    18114    +1     
  Branches     6002     6003    +1     
=======================================
+ Hits        17178    17180    +2     
+ Misses        870      869    -1     
  Partials       65       65           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

return typeof window !== 'undefined' ? window : globalThis;
}

function readFlag(window: any, flagName: keyof GlobalFlags) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use unknown for the parameter type and cast it to FlagsHolder inside the function. That way, we'll get type checks on the window?.[... line

expect(typeof window === 'undefined').toBe(true);
});

test('returns undefined if there global flags are not defined', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: "there" should probably be "the"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these names are copy from the original tests. Updated them in both places

@just-boris just-boris merged commit 9aa87cd into main Mar 1, 2024
@just-boris just-boris deleted the feature-flags-ssr branch March 1, 2024 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants