Commit 2f1b15c6 authored by Igor Crispim Diniz's avatar Igor Crispim Diniz Committed by Tim Neutkens

Fix _app global css import order (#9836)

* fix root components import order

* import order tests

* chunks attachment order tests
parent 3a797f1a
...@@ -64,6 +64,8 @@ export async function loadComponents( ...@@ -64,6 +64,8 @@ export async function loadComponents(
const DocumentMod = require(documentPath) const DocumentMod = require(documentPath)
const { middleware: DocumentMiddleware } = DocumentMod const { middleware: DocumentMiddleware } = DocumentMod
const AppMod = require(appPath)
const ComponentMod = requirePage(pathname, distDir, serverless) const ComponentMod = requirePage(pathname, distDir, serverless)
const [ const [
...@@ -77,7 +79,7 @@ export async function loadComponents( ...@@ -77,7 +79,7 @@ export async function loadComponents(
require(join(distDir, REACT_LOADABLE_MANIFEST)), require(join(distDir, REACT_LOADABLE_MANIFEST)),
interopDefault(ComponentMod), interopDefault(ComponentMod),
interopDefault(DocumentMod), interopDefault(DocumentMod),
interopDefault(require(appPath)), interopDefault(AppMod),
]) ])
return { return {
......
...@@ -499,8 +499,8 @@ export async function renderToHTML( ...@@ -499,8 +499,8 @@ export async function renderToHTML(
const devFiles = buildManifest.devFiles const devFiles = buildManifest.devFiles
const files = [ const files = [
...new Set([ ...new Set([
...getPageFiles(buildManifest, pathname),
...getPageFiles(buildManifest, '/_app'), ...getPageFiles(buildManifest, '/_app'),
...getPageFiles(buildManifest, pathname),
]), ]),
] ]
const polyfillFiles = getPageFiles(buildManifest, '/_polyfills') const polyfillFiles = getPageFiles(buildManifest, '/_polyfills')
......
module.exports = {
webpack: (config, { isServer }) => {
if (!isServer) {
const optimization = config.optimization || {}
const splitChunks = optimization.splitChunks || {}
const cacheGroups = splitChunks.cacheGroups || {}
config.optimization = {
...optimization,
splitChunks: {
...splitChunks,
cacheGroups: {
...cacheGroups,
requiredByApp: {
test: /requiredByApp.js/,
name: 'requiredByApp',
enforce: true,
priority: 10,
chunks: 'all',
},
requiredByPage: {
test: /requiredByPage.js/,
name: 'requiredByPage',
enforce: true,
priority: 10,
chunks: 'all',
},
},
},
}
}
return config
},
}
import React from 'react'
import RequiredByApp from '../requiredByApp'
import sideEffect from '../sideEffectModule'
sideEffect('_app')
function MyApp({ Component, pageProps }) {
return (
<React.Fragment>
<RequiredByApp />
<Component {...pageProps} />
</React.Fragment>
)
}
export default MyApp
import Document, { Html, Head, Main, NextScript } from 'next/document'
import sideEffect from '../sideEffectModule'
sideEffect('_document')
class MyDocument extends Document {
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx)
return {
...initialProps,
}
}
render() {
return (
<Html>
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}
export default MyDocument
import sideEffect from '../sideEffectModule'
import RequiredByPage from '../requiredByPage'
const sideEffects = sideEffect('page')
function Hi() {
return (
<div>
<RequiredByPage />
<p>Hello world!</p>
{sideEffects.map((arg, index) => (
<p key={arg} className="side-effect-calls">
{arg}
</p>
))}
</div>
)
}
Hi.getInitialProps = () => ({})
export default Hi
function RequiredByApp() {
return (
<div>
<p>RequiredByApp!</p>
</div>
)
}
export default RequiredByApp
function RequiredByPage() {
return (
<div>
<p>RequiredByPage</p>
</div>
)
}
export default RequiredByPage
const sideEffect = arg => {
if (!sideEffect.callArguments) {
sideEffect.callArguments = []
}
sideEffect.callArguments.push(arg)
return sideEffect.callArguments
}
export default sideEffect
/* eslint-env jest */
/* global jasmine */
import { join } from 'path'
import cheerio from 'cheerio'
import {
stopApp,
startApp,
nextBuild,
nextServer,
fetchViaHTTP,
findPort,
launchApp,
killApp,
} from 'next-test-utils'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60
const appDir = join(__dirname, '../')
let appPort
let server
let app
describe('Root components import order', () => {
beforeAll(async () => {
await nextBuild(appDir)
app = nextServer({
dir: join(__dirname, '../'),
dev: false,
quiet: true,
})
server = await startApp(app)
appPort = server.address().port
})
afterAll(() => stopApp(server))
const respectsSideEffects = async () => {
const res = await fetchViaHTTP(appPort, '/')
const html = await res.text()
const $ = cheerio.load(html)
const expectSideEffectsOrder = ['_document', '_app', 'page']
const sideEffectCalls = $('.side-effect-calls')
Array.from(sideEffectCalls).forEach((sideEffectCall, index) => {
expect($(sideEffectCall).text()).toEqual(expectSideEffectsOrder[index])
})
}
it(
'root components should be imported in this order _document > _app > page in order to respect side effects',
respectsSideEffects
)
const respectsChunkAttachmentOrder = async () => {
const res = await fetchViaHTTP(appPort, '/')
const html = await res.text()
const $ = cheerio.load(html)
const requiredByRegex = /^\/_next\/static\/chunks\/(requiredBy\w*).*\.js/
const chunks = Array.from($('head').contents())
.filter(
child =>
child.type === 'tag' &&
child.name === 'link' &&
child.attribs.href.match(requiredByRegex)
)
.map(child => child.attribs.href.match(requiredByRegex)[1])
const requiredByAppIndex = chunks.indexOf('requiredByApp')
const requiredByPageIndex = chunks.indexOf('requiredByPage')
expect(requiredByAppIndex).toBeLessThan(requiredByPageIndex)
}
it(
'_app chunks should be attached to de dom before page chunks',
respectsChunkAttachmentOrder
)
describe('on dev server', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(join(__dirname, '../'), appPort)
})
afterAll(() => killApp(app))
it(
'root components should be imported in this order _document > _app > page in order to respect side effects',
respectsSideEffects
)
it(
'_app chunks should be attached to de dom before page chunks',
respectsChunkAttachmentOrder
)
})
})
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