Commit 9b0c641b authored by Luc's avatar Luc Committed by Joe Haddad

Insert <style> tags after anchor element (#8795)

* insert css *after* noscript tag in dev mode

* use arrow function
parent 0eda4d59
......@@ -564,10 +564,32 @@ export default async function getBaseWebpackConfig(
options: {
// By default, style-loader injects CSS into the bottom
// of <head>. This causes ordering problems between dev
// and prod. To fix this, we render a <noscript> tag for
// the styles to be placed within. These styles will be
// applied _before_ <style jsx global>.
insert: `#__next_css__DO_NOT_USE__`,
// and prod. To fix this, we render a <noscript> tag as
// an "anchor" for the styles to be placed after. These
// styles will be applied _before_ <style jsx global>.
insert: (element: Node) => {
const parent = document.querySelector('head') as Element
const anchor = document.querySelector(
'#__next_css__DO_NOT_USE__'
)
// @ts-ignore
const lastInsert = window.__lastInsertedByStyleLoader
if (lastInsert && lastInsert.nextSibling) {
// if a style element was already inserted, insert after
parent.insertBefore(element, lastInsert.nextSibling)
} else if (anchor && anchor.nextSibling) {
// else, insert after the anchor
parent.insertBefore(element, anchor.nextSibling)
} else {
// fallback to inserting in last position into head
parent.appendChild(element)
}
// @ts-ignore
window.__lastInsertedByStyleLoader = element
},
},
},
// When building for production we extract CSS into
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment