Skip to content

Commit

Permalink
refactor: move fragment cache-busting to Karma-only (#4025)
Browse files Browse the repository at this point in the history
Co-authored-by: Will Harney <[email protected]>
  • Loading branch information
nolanlawson and wjhsf authored Feb 29, 2024
1 parent 06599e8 commit 31f93c3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
30 changes: 24 additions & 6 deletions packages/@lwc/engine-core/src/framework/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
isTrue,
isUndefined,
KEY__SCOPED_CSS,
keys,
noop,
toString,
} from '@lwc/shared';

Expand Down Expand Up @@ -111,7 +113,26 @@ function validateLightDomTemplate(template: Template, vm: VM) {
const enum FragmentCache {
HAS_SCOPED_STYLE = 1 << 0,
SHADOW_MODE_SYNTHETIC = 1 << 1,
HAS_LEGACY_SCOPE_TOKEN = 1 << 2,
}

// This should be a no-op outside of LWC's Karma tests, where it's not needed
let registerFragmentCache: (fragmentCache: any) => void = noop;

// Only used in LWC's Karma tests
if (process.env.NODE_ENV === 'test-karma-lwc') {
// Keep track of fragmentCaches, so we can clear them in LWC's Karma tests
const fragmentCaches: any[] = [];
registerFragmentCache = (fragmentCache: any) => {
fragmentCaches.push(fragmentCache);
};

(window as any).__lwcResetFragmentCaches = () => {
for (const fragmentCache of fragmentCaches) {
for (const key of keys(fragmentCache)) {
delete fragmentCache[key];
}
}
};
}

function buildParseFragmentFn(
Expand All @@ -120,6 +141,8 @@ function buildParseFragmentFn(
return (strings: string[], ...keys: number[]) => {
const cache = create(null);

registerFragmentCache(cache);

return function (): Element {
const {
context: { hasScopedStyles, stylesheetToken, legacyStylesheetToken },
Expand All @@ -138,11 +161,6 @@ function buildParseFragmentFn(
if (hasStyleToken && isSyntheticShadow) {
cacheKey |= FragmentCache.SHADOW_MODE_SYNTHETIC;
}
if (hasLegacyToken) {
// This isn't strictly required for prod, but it's required for our karma tests
// since the lwcRuntimeFlag may change over time
cacheKey |= FragmentCache.HAS_LEGACY_SCOPE_TOKEN;
}

if (!isUndefined(cache[cacheKey])) {
return cache[cacheKey];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ describe('legacy scope tokens', () => {

afterEach(() => {
setFeatureFlagForTest('ENABLE_LEGACY_SCOPE_TOKENS', false);
// We keep a cache of parsed static fragments; these need to be reset
// since they can vary based on whether we use the legacy scope token or not.
window.__lwcResetFragmentCaches();
});

function getAttributes(elm) {
Expand Down

0 comments on commit 31f93c3

Please sign in to comment.