Skip to content

Commit

Permalink
code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
shuding committed Feb 18, 2022
1 parent e2bfb36 commit e006fe0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 24 deletions.
Expand Up @@ -12,20 +12,6 @@ import {
WebNextResponse,
} from '../../../../server/base-http/web'

const createHeaders = (args?: any) => ({
...args,
'x-middleware-ssr': '1',
'Cache-Control': 'no-cache, no-store, max-age=0, must-revalidate',
})

function sendError(req: any, error: Error) {
const defaultMessage = 'An error occurred while rendering ' + req.url + '.'
return new Response((error && error.message) || defaultMessage, {
status: 500,
headers: createHeaders(),
})
}

// Polyfilled for `path-browserify` inside the Web Server.
process.cwd = () => ''

Expand Down Expand Up @@ -135,19 +121,14 @@ export function getRender({

// Preflight request
if (request.method === 'HEAD') {
// Hint the client that the matched route is a SSR page.
return new Response(null, {
headers: createHeaders(),
headers: {
'x-middleware-ssr': '1',
},
})
}

// @TODO: We should move this into server/render.
if (Document.getInitialProps) {
const err = new Error(
'`getInitialProps` in Document component is not supported with the Edge Runtime.'
)
return sendError(req, err)
}

const renderServerComponentData = isServerComponent
? query.__flight__ !== undefined
: false
Expand Down
4 changes: 3 additions & 1 deletion packages/next/server/dev/hot-reloader.ts
Expand Up @@ -336,6 +336,8 @@ export default class HotReloader {
)
)

const hasEdgeRuntimePages = this.runtime === 'edge'

return webpackConfigSpan
.traceChild('generate-webpack-config')
.traceAsyncFn(() =>
Expand Down Expand Up @@ -363,7 +365,7 @@ export default class HotReloader {
}),
// For the edge runtime, we need an extra compiler to generate the
// web-targeted server bundle for now.
this.runtime === 'edge'
hasEdgeRuntimePages
? getBaseWebpackConfig(this.dir, {
dev: true,
isServer: true,
Expand Down
7 changes: 7 additions & 0 deletions packages/next/server/render.tsx
Expand Up @@ -1249,6 +1249,13 @@ export async function renderToHTML(
*/
const generateStaticHTML = supportsDynamicHTML !== true
const renderDocument = async () => {
if (runtime === 'edge' && Document.getInitialProps) {
// In the Edge runtime, Document.getInitialProps isn't supported.
throw new Error(
'`getInitialProps` in Document component is not supported with the Edge Runtime.'
)
}

if (!runtime && Document.getInitialProps) {
const renderPage: RenderPage = (
options: ComponentsEnhancer = {}
Expand Down

0 comments on commit e006fe0

Please sign in to comment.