Commit 53f3cb62 authored by JJ Kasper's avatar JJ Kasper Committed by Joe Haddad

Remove top level usage of loader imports in next/document (#9419)

* Don't use loader import at top level in next/document

* Update ts-ignores
parent ba5e6943
...@@ -285,7 +285,10 @@ export async function renderToHTML( ...@@ -285,7 +285,10 @@ export async function renderToHTML(
let results: any = props ? {} : [] let results: any = props ? {} : []
if ((Document as any)[`${method}Middleware`]) { if ((Document as any)[`${method}Middleware`]) {
const curResults = await (Document as any)[`${method}Middleware`](...args) let middlewareFunc = await (Document as any)[`${method}Middleware`]
middlewareFunc = middlewareFunc.default || middlewareFunc
const curResults = await middlewareFunc(...args)
if (props) { if (props) {
for (const result of curResults) { for (const result of curResults) {
results = { results = {
......
// @ts-ignore
import bodyTagsMiddleware from 'next-plugin-loader?middleware=document-body-tags-server!'
// @ts-ignore
import headTagsMiddleware from 'next-plugin-loader?middleware=document-head-tags-server!'
// @ts-ignore
import htmlPropsMiddleware from 'next-plugin-loader?middleware=document-html-props-server!'
// @ts-ignore
import enhanceAppMiddleware from 'next-plugin-loader?middleware=unstable-enhance-app-server!'
// @ts-ignore
import getStylesMiddleware from 'next-plugin-loader?middleware=unstable-get-styles-server!'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import React, { Component } from 'react' import React, { Component } from 'react'
import flush from 'styled-jsx/server' import flush from 'styled-jsx/server'
...@@ -62,9 +52,24 @@ function isLowPriority(file: string) { ...@@ -62,9 +52,24 @@ function isLowPriority(file: string) {
* Commonly used for implementing server side rendering for `css-in-js` libraries. * Commonly used for implementing server side rendering for `css-in-js` libraries.
*/ */
export default class Document<P = {}> extends Component<DocumentProps & P> { export default class Document<P = {}> extends Component<DocumentProps & P> {
static headTagsMiddleware = headTagsMiddleware static headTagsMiddleware = process.env.__NEXT_PLUGINS
static bodyTagsMiddleware = bodyTagsMiddleware ? import(
static htmlPropsMiddleware = htmlPropsMiddleware // @ts-ignore loader syntax
'next-plugin-loader?middleware=document-head-tags-server!'
)
: () => []
static bodyTagsMiddleware = process.env.__NEXT_PLUGINS
? import(
// @ts-ignore loader syntax
'next-plugin-loader?middleware=document-body-tags-server!'
)
: () => []
static htmlPropsMiddleware = process.env.__NEXT_PLUGINS
? import(
// @ts-ignore loader syntax
'next-plugin-loader?middleware=document-html-props-server!'
)
: () => []
/** /**
* `getInitialProps` hook returns the context object with the addition of `renderPage`. * `getInitialProps` hook returns the context object with the addition of `renderPage`.
...@@ -73,7 +78,13 @@ export default class Document<P = {}> extends Component<DocumentProps & P> { ...@@ -73,7 +78,13 @@ export default class Document<P = {}> extends Component<DocumentProps & P> {
static async getInitialProps( static async getInitialProps(
ctx: DocumentContext ctx: DocumentContext
): Promise<DocumentInitialProps> { ): Promise<DocumentInitialProps> {
const enhancers = await enhanceAppMiddleware(ctx) const enhancers = process.env.__NEXT_PLUGINS
? await import(
// @ts-ignore loader syntax
'next-plugin-loader?middleware=unstable-enhance-app-server!'
).then(mod => mod.default(ctx))
: []
const enhanceApp = (App: any) => { const enhanceApp = (App: any) => {
for (const enhancer of enhancers) { for (const enhancer of enhancers) {
App = enhancer(App) App = enhancer(App)
...@@ -82,7 +93,15 @@ export default class Document<P = {}> extends Component<DocumentProps & P> { ...@@ -82,7 +93,15 @@ export default class Document<P = {}> extends Component<DocumentProps & P> {
} }
const { html, head, dataOnly } = await ctx.renderPage({ enhanceApp }) const { html, head, dataOnly } = await ctx.renderPage({ enhanceApp })
const styles = [...flush(), ...(await getStylesMiddleware(ctx))] const styles = [
...flush(),
...(process.env.__NEXT_PLUGINS
? await import(
// @ts-ignore loader syntax
'next-plugin-loader?middleware=unstable-get-styles-server!'
).then(mod => mod.default(ctx))
: []),
]
return { html, head, styles, dataOnly } return { html, head, styles, dataOnly }
} }
......
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