Skip to content

Commit

Permalink
Move CSS Preloads to top of head at document render (#18864)
Browse files Browse the repository at this point in the history
Co-authored-by: Joe Haddad <joe.haddad@zeit.co>
  • Loading branch information
atcastle and Timer committed Dec 30, 2020
1 parent cc0dcf9 commit 74909ec
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/next/pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
5 changes: 5 additions & 0 deletions test/integration/image-component/basic/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react'
import Image from 'next/image'
import Link from 'next/link'
import Head from 'next/head'

const Page = () => {
return (
Expand Down Expand Up @@ -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>
)
Expand Down
3 changes: 3 additions & 0 deletions test/integration/image-component/basic/public/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
p {
color: red;
}
20 changes: 20 additions & 0 deletions test/integration/image-component/basic/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 () => {
Expand Down

0 comments on commit 74909ec

Please sign in to comment.