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
15 changes: 15 additions & 0 deletions .changeset/lost-days-sick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
"@patternfly/pfe-core": minor
---

`ssrCallConnectedCallback`: filter which elements receive `connectedCallback` during SSR.

Importing `ssr-shims.js` still opts in all elements (matching the previous `globalThis.litSsrCallConnectedCallback` behavior). Call `ssrCallConnectedCallback` with a predicate to restrict the set:

Comment thread
zeroedin marked this conversation as resolved.
```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-'),
);
```
38 changes: 36 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,41 @@ function getComputedStyle() {
};
};

// @ts-expect-error: opt in to event support in ssr
globalThis.litSsrCallConnectedCallback = true;
type RenderOption = (typeof LitElementRenderer.renderOptions)[number];

/** Callback this module last registered on `LitElementRenderer.renderOptions`. */
let registered: RenderOption | undefined;

/**
* Opt elements into `connectedCallback` during SSR.
* Importing this module registers a default that matches all elements, so a
* bare `import '@patternfly/pfe-core/ssr-shims.js'` keeps the previous behavior.
* A later call with a predicate replaces that default.
* Lit evaluates `renderOptions` first-match, so a second push would never
* restrict the set.
* @param predicate return true for elements that should receive `connectedCallback`
*/
export function ssrCallConnectedCallback(
predicate?: (element: { localName: string }) => boolean,
): void {
const filter = predicate ?? (() => true);
const option: RenderOption = element =>
filter(element) ? { connectedCallback: true } : undefined;

if (registered) {
const i = LitElementRenderer.renderOptions.indexOf(registered);
if (i !== -1) {
LitElementRenderer.renderOptions[i] = option;
registered = option;
return;
}
}

registered = option;
LitElementRenderer.renderOptions.push(option);
}

ssrCallConnectedCallback();

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
Loading