Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement DSD polyfill as an optional plugin for FAST SSR #6289

Merged
merged 6 commits into from
Aug 18, 2022
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "feat: implement Declarative Shadow DOM polyfill as an optional plugin for FAST Server Side Rendering",
"packageName": "@microsoft/fast-ssr",
"email": "roeisenb@microsoft.com",
"dependentChangeType": "prerelease"
}
4 changes: 3 additions & 1 deletion examples/ssr/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import "@microsoft/fast-ssr/install-dom-shim";
import fs from "fs";
import { html } from "@microsoft/fast-element";
import fastSSR, { RequestStorageManager } from "@microsoft/fast-ssr";
import fastSSR, { RequestStorageManager, DeclarativeShadowDOMPolyfill } from "@microsoft/fast-ssr";
import express from "express";
import { DefaultTodoList, app as todoApp, TodoList } from "fast-todo-app";

Expand All @@ -22,8 +22,10 @@ const template = html`
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>SSR Example</title>
<style>${DeclarativeShadowDOMPolyfill.undefinedElementStyles}</style>
<body>
<todo-app></todo-app>
<script>${DeclarativeShadowDOMPolyfill.nonStreamingTemplateUpgrade}</script>
<!--
Use caution in production environments embedding JSON.
In general the JSON should be sanitized to prevent
Expand Down
6 changes: 6 additions & 0 deletions packages/web-components/fast-ssr/docs/api-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ export type ComponentDOMEmissionMode = "shadow";
// @beta (undocumented)
export type ConstructableElementRenderer = (new (tagName: string, renderInfo: RenderInfo) => ElementRenderer) & typeof ElementRenderer;

// @public
export const DeclarativeShadowDOMPolyfill: Readonly<{
undefinedElementStyles: string;
nonStreamingTemplateUpgrade: string;
}>;

// @beta (undocumented)
export abstract class ElementRenderer {
constructor(tagName: string, renderInfo: RenderInfo);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Reference: https://web.dev/declarative-shadow-dom/

/**
* Provides style and JavaScript code snippets for polyfills related
* to Declarative Shadow DOM (DSD).
*/
export const DeclarativeShadowDOMPolyfill = Object.freeze({
/**
* Styles that hide undefined elements, taking account of the
* fact that undefined elements with DSD should not be hidden.
* @remarks
* These styles should be used in a style element tag in the head
* of the HTML document.
*/
undefinedElementStyles: `:not(:defined) > template[shadowroot] ~ * {
display: none;
}` as string,
/**
* A JavaScript polyfill for DSD that recursively converts all templates
* in the document with the shadowroot attribute into actual shadow roots.
* @remarks
* This is a one-time operation that should be placed at the end of the SSR'd
* HTML but before any other scripts run.
*/
nonStreamingTemplateUpgrade: `if (!HTMLTemplateElement.prototype.hasOwnProperty('shadowRoot')) {
(function attachShadowRoots(root) {
root.querySelectorAll("template[shadowroot]").forEach(template => {
const mode = template.getAttribute("shadowroot");
const shadowRoot = template.parentNode.attachShadow({ mode });
shadowRoot.appendChild(template.content);
template.remove();
attachShadowRoots(shadowRoot);
});
})(document);
}` as string,
});
1 change: 1 addition & 0 deletions packages/web-components/fast-ssr/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export default function fastSSR(): {
}

export * from "./request-storage.js";
export * from "./declarative-shadow-dom-polyfill.js";
export type {
ComponentDOMEmissionMode,
ElementRenderer,
Expand Down