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

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

Merged
merged 5 commits into from
Dec 30, 2020
Merged
Show file tree
Hide file tree
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
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