Skip to content

Commit

Permalink
refactor: add extra guard for browser vs server
Browse files Browse the repository at this point in the history
  • Loading branch information
nolanlawson committed Aug 6, 2024
1 parent d437138 commit 3fdcaf4
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/@lwc/engine-core/src/framework/fragment-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,22 @@ if (process.env.NODE_ENV === 'test-karma-lwc') {
};
}

function checkIsBrowser() {
// The fragment cache only serves prevent calling innerHTML multiple times which doesn't happen on the server.
/* istanbul ignore next */
if (!process.env.IS_BROWSER) {
throw new Error(
'The fragment cache is intended to only be used in @lwc/engine-dom, not @lwc/engine-server'
);
}
}

export function getFromFragmentCache(cacheKey: number, strings: string[]) {
checkIsBrowser();
return fragmentCache[cacheKey].get(strings);
}

export function setInFragmentCache(cacheKey: number, strings: string[], element: Element) {
checkIsBrowser();
fragmentCache[cacheKey].set(strings, element);
}

0 comments on commit 3fdcaf4

Please sign in to comment.