Skip to content

Commit

Permalink
fix: added temp patch for custom render
Browse files Browse the repository at this point in the history
- fixes #52384
  • Loading branch information
wyattjoh committed Jul 7, 2023
1 parent 82dd3db commit 06b780e
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,11 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> =
? `import * as userland500Page from ${stringified500Path}`
: ''
}
const renderToHTML = undefined
// TODO: re-enable this once we've refactored to use implicit matches
// const renderToHTML = undefined
import { renderToHTML as pagesRenderToHTML } from 'next/dist/esm/server/render'
import RouteModule from "next/dist/esm/server/future/route-modules/pages/module"
const pageMod = {
Expand Down
13 changes: 11 additions & 2 deletions packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
} from '../shared/lib/router/utils/route-matcher'
import type { MiddlewareRouteMatch } from '../shared/lib/router/utils/middleware-route-matcher'
import type { RouteMatch } from './future/route-matches/route-match'
import type { RenderOpts } from './render'
import { renderToHTML, type RenderOpts } from './render'

import fs from 'fs'
import { join, relative, resolve, sep, isAbsolute } from 'path'
Expand Down Expand Up @@ -985,7 +985,16 @@ export default class NextNodeServer extends BaseServer {
)
}

throw new Error('Invariant: render should have used routeModule')
// TODO: re-enable this once we've refactored to use implicit matches
// throw new Error('Invariant: render should have used routeModule')

return renderToHTML(
req.originalRequest,
res.originalResponse,
pathname,
query,
renderOpts
)
}

private streamResponseChunk(res: ServerResponse, chunk: any) {
Expand Down
9 changes: 9 additions & 0 deletions test/e2e/custom-app-render/components/page-identifier.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react'

export function PageIdentifier({ page }) {
return (
<div id="page" data-page={page}>
Page: {page}
</div>
)
}
16 changes: 16 additions & 0 deletions test/e2e/custom-app-render/custom-app-render.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { createNextDescribe } from 'e2e-utils'

createNextDescribe(
'custom-app-render',
{
files: __dirname,
skipDeployment: true,
startCommand: 'node server.js',
},
({ next }) => {
it.each(['/', '/render'])('should render %s', async (page) => {
const $ = await next.render$(page)
expect($('#page').data('page')).toBe(page)
})
}
)
7 changes: 7 additions & 0 deletions test/e2e/custom-app-render/pages/_app.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function App({ Component, pageProps }) {
return <Component {...pageProps} />
}

App.getInitialProps = async () => {
return {}
}
7 changes: 7 additions & 0 deletions test/e2e/custom-app-render/pages/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react'

import { PageIdentifier } from '../components/page-identifier'

export default function IndexPage() {
return <PageIdentifier page="/" />
}
7 changes: 7 additions & 0 deletions test/e2e/custom-app-render/pages/render.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react'

import { PageIdentifier } from '../components/page-identifier'

export default function RenderNodePage() {
return <PageIdentifier page="/render" />
}
49 changes: 49 additions & 0 deletions test/e2e/custom-app-render/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const http = require('http')
const { parse } = require('url')
const next = require('next')

async function main() {
const dev = global.isNextDev || false
process.env.NODE_ENV = dev ? 'development' : 'production'

const port = parseInt(process.env.PORT, 10) || 3000

const app = next({ dev, port })
const handle = app.getRequestHandler()

await app.prepare()

const server = http.createServer(async (req, res) => {
try {
const parsedUrl = parse(req.url, true)
const { pathname, query } = parsedUrl

if (pathname.startsWith('/render')) {
await app.render(req, res, pathname, query)
} else {
await handle(req, res, parsedUrl)
}
} catch (err) {
res.statusCode = 500
res.end('Internal Server Error')
}
})

server.once('error', (err) => {
console.error(err)
process.exit(1)
})

server.listen(port, () => {
console.log(
`> started server on url: http://localhost:${port} as ${
dev ? 'development' : process.env.NODE_ENV
}`
)
})
}

main().catch((err) => {
console.error(err)
process.exit(1)
})

0 comments on commit 06b780e

Please sign in to comment.