-
Notifications
You must be signed in to change notification settings - Fork 599
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
add defer hydration support to fast-element #6441
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
aeba9c2
adding mutation observer and starting to refactor fast-element
nicholasrice dab6ccc
break observable re-binding into it's own method
nicholasrice a35b964
re-apply observable values in constructor so that they don't need to β¦
nicholasrice 65698eb
revert unnecessary binding observer changes
nicholasrice 829d530
refactor and reogranize draft implementation, adding tests
nicholasrice 45a6491
added features to exports and updated api report
nicholasrice 436fdad
Change files
nicholasrice 4ec2e98
align StyleStrategy and ElementControllerStrategy implementations
nicholasrice 65ec239
fix api report
nicholasrice 9f47485
Merge branch 'master' into users/nirice/defer-hydration
nicholasrice 7ab89dc
revert observable property re-assignment
nicholasrice 2c021c1
re-organize hydration support into export paths
nicholasrice ed25bb2
leverage strategy API internally
nicholasrice d0bb19e
make mutation observer internal for now
nicholasrice 6401f78
revert removal of context export path
nicholasrice aab8704
fix unit test failure
nicholasrice ff7b237
perform default strategy assignment as file side effect
nicholasrice 9181f05
Merge branch 'master' into users/nirice/defer-hydration
nicholasrice File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@microsoft-fast-element-720b7480-23f3-4944-9238-cde64e7ca3c4.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "prerelease", | ||
"comment": "Added support to fast-element to support element hydration and the `defer-hydration` attribute", | ||
"packageName": "@microsoft/fast-element", | ||
"email": "[email protected]", | ||
"dependentChangeType": "prerelease" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
packages/web-components/fast-element/src/components/hydration.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
import chai, { expect } from "chai"; | ||
import { css, HostBehavior, Updates } from "../index.js"; | ||
import { html } from "../templating/template.js"; | ||
import { uniqueElementName } from "../testing/exports.js"; | ||
import { ElementController } from "./element-controller.js"; | ||
import { FASTElementDefinition, PartialFASTElementDefinition } from "./fast-definitions.js"; | ||
import { FASTElement } from "./fast-element.js"; | ||
import { HydratableElementController } from "./hydration.js"; | ||
import spies from "chai-spies"; | ||
|
||
chai.use(spies) | ||
|
||
describe("The HydratableElementController", () => { | ||
beforeEach(() => { | ||
HydratableElementController.install(); | ||
}) | ||
afterEach(() => { | ||
ElementController.setStrategy(ElementController); | ||
}) | ||
function createController( | ||
config: Omit<PartialFASTElementDefinition, "name"> = {}, | ||
BaseClass = FASTElement | ||
) { | ||
const name = uniqueElementName(); | ||
const definition = FASTElementDefinition.compose( | ||
class ControllerTest extends BaseClass { | ||
static definition = { ...config, name }; | ||
} | ||
).define(); | ||
|
||
const element = document.createElement(name) as FASTElement; | ||
const controller = ElementController.forCustomElement(element); | ||
|
||
return { | ||
name, | ||
element, | ||
controller, | ||
definition, | ||
shadowRoot: element.shadowRoot! as ShadowRoot & { | ||
adoptedStyleSheets: CSSStyleSheet[]; | ||
}, | ||
}; | ||
} | ||
|
||
it("A FASTElement's controller should be an instance of HydratableElementController after invoking 'addHydrationSupport'", () => { | ||
const { element } = createController() | ||
|
||
expect(element.$fastController).to.be.instanceOf(HydratableElementController); | ||
}); | ||
|
||
describe("without the `defer-hydration` attribute on connection", () => { | ||
it("should render the element's template", async () => { | ||
const { element } = createController({template: html`<p>Hello world</p>`}) | ||
|
||
document.body.appendChild(element); | ||
await Updates.next(); | ||
expect(element.shadowRoot?.innerHTML).to.be.equal("<p>Hello world</p>"); | ||
document.body.removeChild(element) | ||
}); | ||
it("should apply the element's main stylesheet", async () => { | ||
const { element } = createController({styles: css`:host{ color :red}`}) | ||
|
||
document.body.appendChild(element); | ||
expect(element.$fastController.mainStyles?.isAttachedTo(element)).to.be.true; | ||
document.body.removeChild(element) | ||
}); | ||
it("should invoke a HostBehavior's connectedCallback", async () => { | ||
const behavior: HostBehavior = { | ||
connectedCallback: chai.spy(() => {}) | ||
} | ||
|
||
const { element, controller } = createController() | ||
controller.addBehavior(behavior); | ||
|
||
document.body.appendChild(element); | ||
expect(behavior.connectedCallback).to.have.been.called() | ||
document.body.removeChild(element) | ||
}); | ||
}) | ||
|
||
describe("with the `defer-hydration` is set before connection", () => { | ||
it("should not render the element's template", async () => { | ||
const { element } = createController({template: html`<p>Hello world</p>`}) | ||
|
||
element.setAttribute('defer-hydration', ''); | ||
document.body.appendChild(element); | ||
await Updates.next(); | ||
expect(element.shadowRoot?.innerHTML).be.equal(""); | ||
document.body.removeChild(element) | ||
}); | ||
it("should not attach the element's main stylesheet", async () => { | ||
const { element } = createController({styles: css`:host{ color :red}`}) | ||
|
||
element.setAttribute('defer-hydration', ''); | ||
document.body.appendChild(element); | ||
expect(element.$fastController.mainStyles?.isAttachedTo(element)).to.be.false; | ||
document.body.removeChild(element) | ||
}); | ||
it("should not invoke a HostBehavior's connectedCallback", async () => { | ||
const behavior: HostBehavior = { | ||
connectedCallback: chai.spy(() => {}) | ||
} | ||
|
||
const { element, controller } = createController() | ||
element.setAttribute('defer-hydration', '') | ||
controller.addBehavior(behavior); | ||
|
||
document.body.appendChild(element); | ||
expect(behavior.connectedCallback).not.to.have.been.called() | ||
document.body.removeChild(element) | ||
}); | ||
}) | ||
|
||
describe("when the `defer-hydration` attribute removed after connection", () => { | ||
it("should render the element's template", async () => { | ||
const { element } = createController({template: html`<p>Hello world</p>`}) | ||
|
||
element.setAttribute('defer-hydration', ''); | ||
document.body.appendChild(element); | ||
await Updates.next(); | ||
expect(element.shadowRoot?.innerHTML).be.equal(""); | ||
element.removeAttribute('defer-hydration') | ||
await Updates.next(); | ||
expect(element.shadowRoot?.innerHTML).to.be.equal("<p>Hello world</p>"); | ||
document.body.removeChild(element) | ||
}); | ||
it("should attach the element's main stylesheet", async () => { | ||
const { element } = createController({styles: css`:host{ color :red}`}) | ||
|
||
element.setAttribute('defer-hydration', ''); | ||
document.body.appendChild(element); | ||
expect(element.$fastController.mainStyles?.isAttachedTo(element)).to.be.false; | ||
element.removeAttribute('defer-hydration'); | ||
await Updates.next(); | ||
expect(element.$fastController.mainStyles?.isAttachedTo(element)).to.be.true; | ||
document.body.removeChild(element); | ||
}); | ||
it("should invoke a HostBehavior's connectedCallback", async () => { | ||
const behavior: HostBehavior = { | ||
connectedCallback: chai.spy(() => {}) | ||
} | ||
|
||
const { element, controller } = createController() | ||
element.setAttribute('defer-hydration', '') | ||
controller.addBehavior(behavior); | ||
|
||
document.body.appendChild(element); | ||
expect(behavior.connectedCallback).not.to.have.been.called(); | ||
element.removeAttribute('defer-hydration'); | ||
await Updates.next(); | ||
expect(behavior.connectedCallback).to.have.been.called(); | ||
document.body.removeChild(element) | ||
}); | ||
}) | ||
}); |
44 changes: 44 additions & 0 deletions
44
packages/web-components/fast-element/src/components/hydration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { UnobservableMutationObserver } from "../utilities.js"; | ||
import { ElementController } from "./element-controller.js"; | ||
|
||
const deferHydrationAttribute = "defer-hydration"; | ||
|
||
/** | ||
* An ElementController capable of hydrating FAST elements from | ||
* Declarative Shadow DOM. | ||
* | ||
* @beta | ||
*/ | ||
export class HydratableElementController< | ||
TElement extends HTMLElement = HTMLElement | ||
> extends ElementController<TElement> { | ||
private static hydrationObserver = new UnobservableMutationObserver( | ||
HydratableElementController.hydrationObserverHandler | ||
); | ||
|
||
private static hydrationObserverHandler(records: MutationRecord[]) { | ||
for (const record of records) { | ||
HydratableElementController.hydrationObserver.unobserve(record.target); | ||
(record.target as any).$fastController.connect(); | ||
} | ||
} | ||
|
||
public connect() { | ||
if (this.source.hasAttribute(deferHydrationAttribute)) { | ||
HydratableElementController.hydrationObserver.observe(this.source, { | ||
attributeFilter: [deferHydrationAttribute], | ||
}); | ||
} else { | ||
super.connect(); | ||
} | ||
} | ||
|
||
public disconnect() { | ||
super.disconnect(); | ||
HydratableElementController.hydrationObserver.unobserve(this.source); | ||
} | ||
|
||
public static install() { | ||
ElementController.setStrategy(HydratableElementController); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/web-components/fast-element/src/components/install-hydration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { HydratableElementController } from "./hydration.js"; | ||
|
||
HydratableElementController.install(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Set strategy right here?