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

Append styles imported in JS to end of document.head #1045

Merged
merged 1 commit into from
Dec 12, 2021
Merged
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
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);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should ensure that styles already present in index.html will be kept where they are, to keep the existing order. Especially when dealing with external styles, that are not funneled through this. (e.g. vendor.css, then external google-font-css, then app.css). All other dynamically added styles will be appended to <head>, in whatever order webpack gives us.

This is all we can do here about ensuring the "right" order, is it?

}
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