Unverified Commit 16eb80b0 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Fix standalone rendering for unmatched _next routes (#51611)

Request data flow in the server

```
request ---> router worker (1) ---> ipc ---> render worker app (2)
                                                              |-----> render worker pages (3)
```

When it's hitting `_next/*` unmatched routes in standalone server, it will render 404, but when you hit `_next/*` it will render app not-found as the app router is always enabled, but router worker isn't set up with require-hook for proper built-in react version, then the app-render will fail with `./server.edge` exports not found error.

We detect if it's in the render worker, then do the app paths rendering instead of directly looking up for app paths in route worker. This could avoid unexpected accesses to the app-render

Fixes #51482 
Fixes #50232
Closes #51506
Reverts changes in #51172
fix NEXT-1260
parent 89f36a98
...@@ -3,7 +3,8 @@ import type { FlightResponseRef } from './flight-response-ref' ...@@ -3,7 +3,8 @@ import type { FlightResponseRef } from './flight-response-ref'
import { readableStreamTee } from '../stream-utils/node-web-streams-helper' import { readableStreamTee } from '../stream-utils/node-web-streams-helper'
import { encodeText, decodeText } from '../stream-utils/encode-decode' import { encodeText, decodeText } from '../stream-utils/encode-decode'
import { htmlEscapeJsonString } from '../htmlescape' import { htmlEscapeJsonString } from '../htmlescape'
import { isEdgeRuntime } from './app-render'
const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge'
/** /**
* Render Flight stream. * Render Flight stream.
......
...@@ -2431,15 +2431,16 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -2431,15 +2431,16 @@ export default abstract class Server<ServerOptions extends Options = Options> {
} }
} }
const { res, query } = ctx const { res, query } = ctx
try { try {
let result: null | FindComponentsResult = null let result: null | FindComponentsResult = null
const is404 = res.statusCode === 404 const is404 = res.statusCode === 404
let using404Page = false let using404Page = false
// use static 404 page if available and is 404 response
if (is404) { if (is404) {
if (this.hasAppDir) { // Rendering app routes only in render worker to make sure the require-hook is setup
if (this.hasAppDir && this.isRenderWorker) {
// Use the not-found entry in app directory // Use the not-found entry in app directory
result = await this.findPageComponents({ result = await this.findPageComponents({
pathname: this.renderOpts.dev ? '/not-found' : '/_not-found', pathname: this.renderOpts.dev ? '/not-found' : '/_not-found',
......
...@@ -5,8 +5,6 @@ import httpProxy from 'next/dist/compiled/http-proxy' ...@@ -5,8 +5,6 @@ import httpProxy from 'next/dist/compiled/http-proxy'
import { Worker } from 'next/dist/compiled/jest-worker' import { Worker } from 'next/dist/compiled/jest-worker'
import { normalizeRepeatedSlashes } from '../../shared/lib/utils' import { normalizeRepeatedSlashes } from '../../shared/lib/utils'
const renderServerPath = require.resolve('./render-server')
export const createServerHandler = async ({ export const createServerHandler = async ({
port, port,
hostname, hostname,
...@@ -20,7 +18,7 @@ export const createServerHandler = async ({ ...@@ -20,7 +18,7 @@ export const createServerHandler = async ({
dev?: boolean dev?: boolean
minimalMode: boolean minimalMode: boolean
}) => { }) => {
const routerWorker = new Worker(renderServerPath, { const routerWorker = new Worker(require.resolve('./render-server'), {
numWorkers: 1, numWorkers: 1,
maxRetries: 10, maxRetries: 10,
forkOptions: { forkOptions: {
......
...@@ -141,10 +141,10 @@ export class NextServer { ...@@ -141,10 +141,10 @@ export class NextServer {
} }
async prepare() { async prepare() {
const server = await this.getServer()
if (this.standaloneMode) return if (this.standaloneMode) return
const server = await this.getServer()
// We shouldn't prepare the server in production, // We shouldn't prepare the server in production,
// because this code won't be executed when deployed // because this code won't be executed when deployed
if (this.options.dev) { if (this.options.dev) {
......
export default function NotFound() {
return 'app-not-found'
}
...@@ -4,6 +4,9 @@ createNextDescribe( ...@@ -4,6 +4,9 @@ createNextDescribe(
'standalone mode - metadata routes', 'standalone mode - metadata routes',
{ {
files: __dirname, files: __dirname,
dependencies: {
swr: 'latest',
},
}, },
({ next }) => { ({ next }) => {
beforeAll(async () => { beforeAll(async () => {
...@@ -12,11 +15,30 @@ createNextDescribe( ...@@ -12,11 +15,30 @@ createNextDescribe(
await next.renameFolder('app', 'app_hidden') await next.renameFolder('app', 'app_hidden')
}) })
it('should work', async () => { it('should handle metadata icons correctly', async () => {
const faviconRes = await next.fetch('/favicon.ico') const faviconRes = await next.fetch('/favicon.ico')
const iconRes = await next.fetch('/icon.svg') const iconRes = await next.fetch('/icon.svg')
expect(faviconRes.status).toBe(200) expect(faviconRes.status).toBe(200)
expect(iconRes.status).toBe(200) expect(iconRes.status).toBe(200)
}) })
it('should handle correctly not-found.js', async () => {
const res = await next.fetch('/not-found/does-not-exist')
expect(res.status).toBe(404)
const html = await res.text()
expect(html).toContain('app-not-found')
})
it('should handle private _next unmatched route correctly', async () => {
const res = await next.fetch('/_next/does-not-exist')
expect(res.status).toBe(404)
const html = await res.text()
expect(html).toContain('app-not-found')
})
it('should handle pages rendering correctly', async () => {
const browser = await next.browser('/hello')
expect(await browser.elementByCss('#content').text()).toBe('hello-bar')
})
} }
) )
import useSWR, { useSWRConfig } from 'swr'
export default function Page({ foo }) {
const { data } = useSWR('hello', (v) => v, { fallbackData: 'hello' })
useSWRConfig() // call SWR context
return <div id="content">{`${data}-${foo}`}</div>
}
export function getServerSideProps() {
return {
props: {
foo: 'bar',
},
}
}
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