Unverified Commit 74909ecf authored by Alex Castle's avatar Alex Castle Committed by GitHub

Move CSS Preloads to top of head at document render (#18864)

Co-authored-by: 's avatarJoe Haddad <joe.haddad@zeit.co>
parent cc0dcf96
......@@ -348,6 +348,23 @@ export class Head extends Component<
this.context.docComponentsRendered.Head = true
let { head } = this.context
let cssPreloads: Array<JSX.Element> = []
let otherHeadElements: Array<JSX.Element> = []
if (head) {
head.forEach((c) => {
if (
c &&
c.type === 'link' &&
c.props['rel'] === 'preload' &&
c.props['as'] === 'style'
) {
cssPreloads.push(c)
} else {
c && otherHeadElements.push(c)
}
})
head = cssPreloads.concat(otherHeadElements)
}
let children = this.props.children
// show a warning if Head contains <title> (only in development)
if (process.env.NODE_ENV !== 'production') {
......
import React from 'react'
import Image from 'next/image'
import Link from 'next/link'
import Head from 'next/head'
const Page = () => {
return (
......@@ -90,6 +91,10 @@ const Page = () => {
<Link href="/lazy">
<a id="lazylink">lazy</a>
</Link>
<Head>
<link rel="stylesheet" href="styles.css" />
<link rel="preload" href="styles.css" as="style" />
</Head>
<p id="stubtext">This is the index page</p>
</div>
)
......
......@@ -202,6 +202,23 @@ async function hasPreloadLinkMatchingUrl(url) {
return false
}
async function hasImagePreloadBeforeCSSPreload() {
const links = await browser.elementsByCss('link')
let foundImage = false
for (const link of links) {
const rel = await link.getAttribute('rel')
if (rel === 'preload') {
const linkAs = await link.getAttribute('as')
if (linkAs === 'image') {
foundImage = true
} else if (linkAs === 'style' && foundImage) {
return true
}
}
}
return false
}
describe('Image Component Tests', () => {
beforeAll(async () => {
await nextBuild(appDir)
......@@ -245,6 +262,9 @@ describe('Image Component Tests', () => {
)
).toBe(true)
})
it('should not create any preload tags higher up the page than CSS preload tags', async () => {
expect(await hasImagePreloadBeforeCSSPreload()).toBe(false)
})
})
describe('Client-side Image Component Tests', () => {
beforeAll(async () => {
......
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