Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .changeset/lost-days-sick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
"@patternfly/pfe-core": minor
---

`ssrCallConnectedCallback`: exported from `ssr-shims.js`, replacing the deprecated `globalThis.litSsrCallConnectedCallback` global. Pass a predicate to limit which elements receive `connectedCallback` during SSR, or omit it to opt in all elements.

```typescript
import { ssrCallConnectedCallback } from '@patternfly/pfe-core/ssr-shims.js';

ssrCallConnectedCallback(el =>
el.localName.startsWith('pf-') || el.localName.startsWith('rh-')
);
```
15 changes: 15 additions & 0 deletions .changeset/poor-sky-blue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
"@patternfly/pfe-tools": minor
---

`renderGlobal`: third `connectedCallbackFilter` parameter limits which elements receive `connectedCallback` during SSR. Without it, all elements opt in.

```typescript
import { renderGlobal } from '@patternfly/pfe-tools/ssr/global.js';

const html = await renderGlobal(
'<my-el></my-el>',
['my-package/my-el.js'],
el => el.localName.startsWith('my-'),
);
```
10 changes: 8 additions & 2 deletions core/pfe-core/ssr-shims.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { installWindowOnGlobal } from '@lit-labs/ssr/lib/dom-shim.js';
import { LitElementRenderer } from '@lit-labs/ssr/lib/lit-element-renderer.js';

class ObserverShim {
observe(): void {
Expand Down Expand Up @@ -30,8 +31,13 @@ function getComputedStyle() {
};
};

// @ts-expect-error: opt in to event support in ssr
globalThis.litSsrCallConnectedCallback = true;
export function ssrCallConnectedCallback(
predicate: (element: { localName: string }) => boolean = () => true,
): void {
LitElementRenderer.renderOptions.push(
element => predicate(element) ? { connectedCallback: true } : undefined,
);
}

installWindowOnGlobal({
ErrorEvent: Event,
Expand Down
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tools/pfe-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"@11ty/eleventy-plugin-syntaxhighlight": "^5.0.2",
"@changesets/cli": "^2.29.8",
"@koa/router": "^15.1.1",
"@lit-labs/ssr": "^4.0.0",
"@lit-labs/ssr": "^4.1.0",
"@open-wc/testing": "^4.0.0",
"@playwright/test": "~1.57.0",
"@pwrs/mappa": "^0.0.4",
Expand Down
6 changes: 5 additions & 1 deletion tools/pfe-tools/ssr/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
* first importing the provided component defintions into nodejs' global scope.
* @param html string to render
* @param importSpecifiers list of web component definition module import specifiers
* @param connectedCallbackFilter optional predicate to limit which elements
* receive `connectedCallback` during SSR. Defaults to all elements.
*/
export async function renderGlobal(
html: string,
importSpecifiers: string[],
connectedCallbackFilter?: (element: { localName: string }) => boolean,
): Promise<string> {
// hack to avoid circular typescript project reference
const spec = '@patternfly/pfe-core/ssr-shims.js';
await import(spec);
const { ssrCallConnectedCallback } = await import(spec);
ssrCallConnectedCallback(connectedCallbackFilter);
const { ssr } = await import('./ssr.js');
await Promise.all(importSpecifiers.map(x => import(x)));
return ssr(html);
Expand Down