Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Temporarily revert change to pages render #52407

Merged
merged 4 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 } 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 = process.env.NEXT_TEST_MODE === 'dev'
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)
})