From 706dc39502c4b2a38828405e224c3b3127922d36 Mon Sep 17 00:00:00 2001 From: Aral Roca Date: Mon, 30 Sep 2024 16:27:03 +0200 Subject: [PATCH 1/2] feat: adapt global styles in WCs SSR --- .../utils/ssr-web-component/index.test.tsx | 21 +++++++++++++++++++ .../src/utils/ssr-web-component/index.tsx | 17 +++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/packages/brisa/src/utils/ssr-web-component/index.test.tsx b/packages/brisa/src/utils/ssr-web-component/index.test.tsx index 1adb3f0af..f642d414d 100644 --- a/packages/brisa/src/utils/ssr-web-component/index.test.tsx +++ b/packages/brisa/src/utils/ssr-web-component/index.test.tsx @@ -856,5 +856,26 @@ describe('utils', () => { expect(output[2][0][2][0][2][0]).toBe('span'); expect(output[2][0][2][0][2][2]).toBe('bar'); }); + + it('should add style with the CSS_FILES imports when CSS_FILES comes and is not skipping global styles', async () => { + globalThis.mockConstants = { + ...getConstants(), + CSS_FILES: ['foo.css', 'bar.css'], + } as unknown as BrisaConstants; + const Component = ({ foo }: any) =>
{foo}
; + const selector = 'web-component'; + const output = (await SSRWebComponent( + { + 'ssr-Component': Component, + 'ssr-selector': selector, + foo: bar, + }, + requestContext, + )) as any; + + expect(output[2][0][2][2][1]).toEqual({ + html: ``, + }); + }); }); }); diff --git a/packages/brisa/src/utils/ssr-web-component/index.tsx b/packages/brisa/src/utils/ssr-web-component/index.tsx index 18c94059d..40ed53f24 100644 --- a/packages/brisa/src/utils/ssr-web-component/index.tsx +++ b/packages/brisa/src/utils/ssr-web-component/index.tsx @@ -2,6 +2,7 @@ import { toInline } from '@/helpers'; import { Fragment as BrisaFragment } from '@/jsx-runtime'; import type { RequestContext } from '@/types'; import { getConstants } from '@/constants'; +import dangerHTML from '../danger-html'; export const AVOID_DECLARATIVE_SHADOW_DOM_SYMBOL = Symbol.for( 'AVOID_DECLARATIVE_SHADOW_DOM', @@ -31,7 +32,7 @@ export default async function SSRWebComponent( selector = props.selector; } - const { WEB_CONTEXT_PLUGINS } = getConstants(); + const { WEB_CONTEXT_PLUGINS, CSS_FILES } = getConstants(); const showContent = !store.has(AVOID_DECLARATIVE_SHADOW_DOM_SYMBOL); const self = { shadowRoot: {}, attachInternals: voidFn } as any; let style = ''; @@ -98,6 +99,12 @@ export default async function SSRWebComponent( } } + // This should be calculated after the Component execution because the devs can + // skip global CSS setting adoptedStyleSheets to an empty array + // (this approach works in both worlds: SSR + client-side) + const skipGlobalCSS = self.shadowRoot.adoptedStyleSheets?.length === 0; + const useCSSImports = !skipGlobalCSS && CSS_FILES.length > 0; + return ( // @ts-ignore @@ -105,10 +112,12 @@ export default async function SSRWebComponent( )} {/* @ts-ignore */} @@ -116,3 +125,7 @@ export default async function SSRWebComponent( ); } + +function getCSSImports(CSS_FILES: string[]) { + return CSS_FILES.map((file) => `@import '/${file}'`).join(';'); +} From 3d87488b4adf7d4a98aaeaa7e22e2cd9e88d8b6c Mon Sep 17 00:00:00 2001 From: Aral Roca Date: Mon, 30 Sep 2024 16:41:21 +0200 Subject: [PATCH 2/2] test: change flaky test --- packages/brisa/src/utils/get-client-code-in-page/index.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/brisa/src/utils/get-client-code-in-page/index.test.ts b/packages/brisa/src/utils/get-client-code-in-page/index.test.ts index d2f298d4d..558aa7892 100644 --- a/packages/brisa/src/utils/get-client-code-in-page/index.test.ts +++ b/packages/brisa/src/utils/get-client-code-in-page/index.test.ts @@ -22,7 +22,7 @@ const i18nCode = 3072; const brisaSize = 5743; // TODO: Reduce this size :/ const webComponents = 1144; const unsuspenseSize = 217; -const rpcSize = 2468; // TODO: Reduce this size +const rpcSize = 2467; // TODO: Reduce this size const lazyRPCSize = 4171; // TODO: Reduce this size // lazyRPC is loaded after user interaction (action, link), // so it's not included in the initial size