Skip to content

Commit

Permalink
Merge pull request #1045 from simonihmig/append-styles
Browse files Browse the repository at this point in the history
Append styles imported in JS to end of `document.head`
  • Loading branch information
ef4 authored Dec 12, 2021
2 parents 8182d45 + c781808 commit bbc26b3
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions packages/core/src/html-placeholder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ export default class Placeholder {
}
}

insert(node: Node) {
insert(node: Node): void {
this.end.parentElement.insertBefore(node, this.end);
}

appendToHead(node: Node): void {
this.end.ownerDocument.head.appendChild(node);
}

isScript(): boolean {
return this.target.tagName === 'SCRIPT';
}
Expand Down Expand Up @@ -70,12 +74,20 @@ export default class Placeholder {
let newTag = this.end.ownerDocument.createElement('link');
newTag.href = href;
newTag.rel = 'stylesheet';
this.insert(newTag);
this.insertNewline();

if (this.isScript()) {
// Add dynamic styles from scripts to the bottom of the head, and not to where the script was,
// to prevent FOUC when pre-rendering (FastBoot)
this.appendToHead(newTag);
} else {
// Keep the new style in the same place as the original one
this.insert(newTag);
}
this.insertNewline(newTag as InDOMNode);
}

insertNewline() {
this.end.parentElement.insertBefore(this.end.ownerDocument.createTextNode('\n'), this.end);
insertNewline(node: InDOMNode = this.end): void {
node.parentElement.insertBefore(node.ownerDocument.createTextNode('\n'), node);
}
}

Expand Down

0 comments on commit bbc26b3

Please sign in to comment.