未验证 提交 06d53544 编写于 作者: T Tim Neutkens 提交者: GitHub

Remove documentMiddleware experimental option (#12218)

上级 e6424fef
...@@ -41,7 +41,6 @@ const defaultConfig: { [key: string]: any } = { ...@@ -41,7 +41,6 @@ const defaultConfig: { [key: string]: any } = {
(Number(process.env.CIRCLE_NODE_TOTAL) || (Number(process.env.CIRCLE_NODE_TOTAL) ||
(os.cpus() || { length: 1 }).length) - 1 (os.cpus() || { length: 1 }).length) - 1
), ),
documentMiddleware: false,
granularChunks: true, granularChunks: true,
modern: false, modern: false,
plugins: false, plugins: false,
......
...@@ -10,7 +10,6 @@ import { BuildManifest } from './get-page-files' ...@@ -10,7 +10,6 @@ import { BuildManifest } from './get-page-files'
import { AppType, DocumentType } from '../lib/utils' import { AppType, DocumentType } from '../lib/utils'
import { import {
PageConfig, PageConfig,
NextPageContext,
GetStaticPaths, GetStaticPaths,
GetServerSideProps, GetServerSideProps,
GetStaticProps, GetStaticProps,
...@@ -35,7 +34,6 @@ export type LoadComponentsReturnType = { ...@@ -35,7 +34,6 @@ export type LoadComponentsReturnType = {
buildManifest: BuildManifest buildManifest: BuildManifest
reactLoadableManifest: ReactLoadableManifest reactLoadableManifest: ReactLoadableManifest
Document: DocumentType Document: DocumentType
DocumentMiddleware?: (ctx: NextPageContext) => void
App: AppType App: AppType
getStaticProps?: GetStaticProps getStaticProps?: GetStaticProps
getStaticPaths?: GetStaticPaths getStaticPaths?: GetStaticPaths
...@@ -78,7 +76,6 @@ export async function loadComponents( ...@@ -78,7 +76,6 @@ export async function loadComponents(
) )
const DocumentMod = require(documentPath) const DocumentMod = require(documentPath)
const { middleware: DocumentMiddleware } = DocumentMod
const AppMod = require(appPath) const AppMod = require(appPath)
...@@ -105,7 +102,6 @@ export async function loadComponents( ...@@ -105,7 +102,6 @@ export async function loadComponents(
Document, Document,
Component, Component,
buildManifest, buildManifest,
DocumentMiddleware,
reactLoadableManifest, reactLoadableManifest,
pageConfig: ComponentMod.config || {}, pageConfig: ComponentMod.config || {},
getServerSideProps, getServerSideProps,
......
...@@ -118,7 +118,6 @@ export default class Server { ...@@ -118,7 +118,6 @@ export default class Server {
runtimeConfig?: { [key: string]: any } runtimeConfig?: { [key: string]: any }
assetPrefix?: string assetPrefix?: string
canonicalBase: string canonicalBase: string
documentMiddlewareEnabled: boolean
dev?: boolean dev?: boolean
previewProps: __ApiPreviewProps previewProps: __ApiPreviewProps
customServer?: boolean customServer?: boolean
...@@ -171,8 +170,6 @@ export default class Server { ...@@ -171,8 +170,6 @@ export default class Server {
this.renderOpts = { this.renderOpts = {
poweredByHeader: this.nextConfig.poweredByHeader, poweredByHeader: this.nextConfig.poweredByHeader,
canonicalBase: this.nextConfig.amp.canonicalBase, canonicalBase: this.nextConfig.amp.canonicalBase,
documentMiddlewareEnabled: this.nextConfig.experimental
.documentMiddleware,
staticMarkup, staticMarkup,
buildId: this.buildId, buildId: this.buildId,
generateEtags, generateEtags,
......
...@@ -153,7 +153,6 @@ export type RenderOptsPartial = { ...@@ -153,7 +153,6 @@ export type RenderOptsPartial = {
ampValidator?: (html: string, pathname: string) => Promise<void> ampValidator?: (html: string, pathname: string) => Promise<void>
ampSkipValidation?: boolean ampSkipValidation?: boolean
ampOptimizerConfig?: { [key: string]: any } ampOptimizerConfig?: { [key: string]: any }
documentMiddlewareEnabled?: boolean
isDataReq?: boolean isDataReq?: boolean
params?: ParsedUrlQuery params?: ParsedUrlQuery
previewProps: __ApiPreviewProps previewProps: __ApiPreviewProps
...@@ -295,13 +294,11 @@ export async function renderToHTML( ...@@ -295,13 +294,11 @@ export async function renderToHTML(
const { const {
err, err,
dev = false, dev = false,
documentMiddlewareEnabled = false,
staticMarkup = false, staticMarkup = false,
ampPath = '', ampPath = '',
App, App,
Document, Document,
pageConfig = {}, pageConfig = {},
DocumentMiddleware,
Component, Component,
buildManifest, buildManifest,
reactLoadableManifest, reactLoadableManifest,
...@@ -483,10 +480,6 @@ export async function renderToHTML( ...@@ -483,10 +480,6 @@ export async function renderToHTML(
} }
let props: any let props: any
if (documentMiddlewareEnabled && typeof DocumentMiddleware === 'function') {
await DocumentMiddleware(ctx)
}
const ampState = { const ampState = {
ampFirst: pageConfig.amp === true, ampFirst: pageConfig.amp === true,
hasQuery: Boolean(query.amp), hasQuery: Boolean(query.amp),
......
module.exports = {
experimental: { documentMiddleware: true },
}
import Document, { Html, Head, Main, NextScript } from 'next/document'
export async function middleware({ req, res }) {
if (req.url === '/another') {
res.setHeader('next-middleware', 'hit another!')
return res.end()
}
res.setHeader('next-middleware', 'hi from middleware')
}
class MyDocument extends Document {
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx)
return { ...initialProps }
}
render() {
return (
<Html>
<Head>
<style>{`body { margin: 0 } /* custom! */`}</style>
</Head>
<body className="custom_class">
<Main />
<NextScript />
</body>
</Html>
)
}
}
export default MyDocument
const Another = () => <p>Hi again 👋</p>
Another.getInitialProps = () => ({})
export default Another
const Home = () => <p>Hi there 👋</p>
Home.getInitialProps = () => ({})
export default Home
/* eslint-env jest */
/* global jasmine */
import { join } from 'path'
import {
renderViaHTTP,
fetchViaHTTP,
findPort,
launchApp,
killApp,
} from 'next-test-utils'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
let app
let appPort
describe('Document middleware', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(join(__dirname, '../'), appPort)
})
afterAll(() => killApp(app))
it('should render a page without error', async () => {
const html = await renderViaHTTP(appPort, '/')
expect(html).toMatch(/hi there/i)
})
it('should set header in middleware and still render', async () => {
const res = await fetchViaHTTP(appPort, '/')
const html = await res.text()
const header = res.headers.get('next-middleware')
expect(html).toMatch(/hi there/i)
expect(header).toBe('hi from middleware')
})
it('should set header and abort render on res.end()', async () => {
const res = await fetchViaHTTP(appPort, '/another')
const html = (await res.text()) || ''
const header = res.headers.get('next-middleware')
expect(html.length).toBe(0)
expect(header).toBe('hit another!')
})
})
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册