Unverified Commit cf95146a authored by Janicklas Ralph's avatar Janicklas Ralph Committed by GitHub

Critical css fix (#21462)

Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
Co-authored-by: 's avatarJoe Haddad <joe.haddad@zeit.co>
parent 1f79519e
......@@ -273,6 +273,7 @@ export default async function exportPage({
optimizeImages,
/// @ts-ignore
optimizeCss,
distDir,
fontManifest: optimizeFonts
? requireFontManifest(distDir, serverless)
: null,
......
import '../styles/styles.css'
// This default export is required in a new `pages/_app.js` file.
export default function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
}
import styles from '../styles/index.module.css'
export default function Home() {
return (
<div className={styles.hello}>
<p>Hello World</p>
</div>
)
}
.hello {
font: 15px Helvetica, Arial, sans-serif;
background: #eee;
padding: 100px;
text-align: center;
transition: 100ms ease-in background;
}
.hello:hover {
background: #ccc;
}
.extra-style1 {
background: #000;
}
body {
font-family: 'SF Pro Text', 'SF Pro Icons', 'Helvetica Neue', 'Helvetica',
'Arial', sans-serif;
padding: 20px 20px 60px;
max-width: 680px;
margin: 0 auto;
}
.extra-style {
font-size: 200%;
}
/* eslint-env jest */
import { join } from 'path'
import {
killApp,
findPort,
nextStart,
nextBuild,
renderViaHTTP,
} from 'next-test-utils'
import fs from 'fs-extra'
jest.setTimeout(1000 * 60 * 2)
const appDir = join(__dirname, '../')
const nextConfig = join(appDir, 'next.config.js')
let appPort
let app
function runTests() {
it('should inline critical CSS', async () => {
const html = await renderViaHTTP(appPort, '/index')
expect(html).toMatch(
/<link rel="stylesheet" href="\/_next\/static\/css\/.*\.css" .*>/
)
expect(html).toMatch(/body{font-family:SF Pro Text/)
})
it('should not inline non-critical css', async () => {
const html = await renderViaHTTP(appPort, '/index')
expect(html).not.toMatch(/.extra-style/)
})
}
describe('CSS optimization for SSR apps', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfig,
`module.exports = { experimental: {optimizeCss: true} }`,
'utf8'
)
if (fs.pathExistsSync(join(appDir, '.next'))) {
await fs.remove(join(appDir, '.next'))
}
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))
runTests()
})
describe('CSS optimization for serverless apps', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfig,
`module.exports = { target: 'serverless', experimental: {optimizeCss: true} }`,
'utf8'
)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))
runTests()
})
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