Unverified Commit 2eaa3ae1 authored by Ngô Đức Anh's avatar Ngô Đức Anh Committed by GitHub

fix appDir returning 404 in production with `"output": "standalone"` (#43268)

<!--
Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change that you're making:
-->

Fixes: https://github.com/vercel/next.js/issues/42812
Fixes: https://github.com/vercel/next.js/issues/43037

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ]
[e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)

This PR fixes the issue in which urls from appDir will always not be
found in production when built with `"output": "standalone"` by copying
.next/server/app and .next/server/app-paths-manifest.json into
.next/standalone/server.
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 60d5c961
...@@ -61,6 +61,9 @@ import { ...@@ -61,6 +61,9 @@ import {
FONT_LOADER_MANIFEST, FONT_LOADER_MANIFEST,
CLIENT_STATIC_FILES_RUNTIME_MAIN_APP, CLIENT_STATIC_FILES_RUNTIME_MAIN_APP,
APP_CLIENT_INTERNALS, APP_CLIENT_INTERNALS,
SUBRESOURCE_INTEGRITY_MANIFEST,
MIDDLEWARE_BUILD_MANIFEST,
MIDDLEWARE_REACT_LOADABLE_MANIFEST,
} from '../shared/lib/constants' } from '../shared/lib/constants'
import { getSortedRoutes, isDynamicRoute } from '../shared/lib/router/utils' import { getSortedRoutes, isDynamicRoute } from '../shared/lib/router/utils'
import { __ApiPreviewProps } from '../server/api-utils' import { __ApiPreviewProps } from '../server/api-utils'
...@@ -865,6 +868,11 @@ export default async function build( ...@@ -865,6 +868,11 @@ export default async function build(
) )
const manifestPath = path.join(distDir, SERVER_DIRECTORY, PAGES_MANIFEST) const manifestPath = path.join(distDir, SERVER_DIRECTORY, PAGES_MANIFEST)
const appManifestPath = path.join(
distDir,
SERVER_DIRECTORY,
APP_PATHS_MANIFEST
)
const requiredServerFiles = nextBuildSpan const requiredServerFiles = nextBuildSpan
.traceChild('generate-required-server-files') .traceChild('generate-required-server-files')
...@@ -885,8 +893,26 @@ export default async function build( ...@@ -885,8 +893,26 @@ export default async function build(
BUILD_MANIFEST, BUILD_MANIFEST,
PRERENDER_MANIFEST, PRERENDER_MANIFEST,
path.join(SERVER_DIRECTORY, MIDDLEWARE_MANIFEST), path.join(SERVER_DIRECTORY, MIDDLEWARE_MANIFEST),
path.join(SERVER_DIRECTORY, MIDDLEWARE_BUILD_MANIFEST + '.js'),
path.join(
SERVER_DIRECTORY,
MIDDLEWARE_REACT_LOADABLE_MANIFEST + '.js'
),
...(appDir ...(appDir
? [ ? [
...(config.experimental.sri
? [
path.join(
SERVER_DIRECTORY,
SUBRESOURCE_INTEGRITY_MANIFEST + '.js'
),
path.join(
SERVER_DIRECTORY,
SUBRESOURCE_INTEGRITY_MANIFEST + '.json'
),
]
: []),
path.relative(distDir, appManifestPath),
path.join(SERVER_DIRECTORY, FLIGHT_MANIFEST + '.js'), path.join(SERVER_DIRECTORY, FLIGHT_MANIFEST + '.js'),
path.join(SERVER_DIRECTORY, FLIGHT_MANIFEST + '.json'), path.join(SERVER_DIRECTORY, FLIGHT_MANIFEST + '.json'),
path.join( path.join(
...@@ -2046,6 +2072,7 @@ export default async function build( ...@@ -2046,6 +2072,7 @@ export default async function build(
dir, dir,
distDir, distDir,
pageKeys.pages, pageKeys.pages,
pageKeys.app,
outputFileTracingRoot, outputFileTracingRoot,
requiredServerFiles.config, requiredServerFiles.config,
middlewareManifest middlewareManifest
...@@ -2750,17 +2777,32 @@ export default async function build( ...@@ -2750,17 +2777,32 @@ export default async function build(
}) })
await promises.copyFile(filePath, outputPath) await promises.copyFile(filePath, outputPath)
} }
await recursiveCopy( if (pagesDir) {
path.join(distDir, SERVER_DIRECTORY, 'pages'), await recursiveCopy(
path.join( path.join(distDir, SERVER_DIRECTORY, 'pages'),
distDir, path.join(
'standalone', distDir,
path.relative(outputFileTracingRoot, distDir), 'standalone',
SERVER_DIRECTORY, path.relative(outputFileTracingRoot, distDir),
'pages' SERVER_DIRECTORY,
), 'pages'
{ overwrite: true } ),
) { overwrite: true }
)
}
if (appDir) {
await recursiveCopy(
path.join(distDir, SERVER_DIRECTORY, 'app'),
path.join(
distDir,
'standalone',
path.relative(outputFileTracingRoot, distDir),
SERVER_DIRECTORY,
'app'
),
{ overwrite: true }
)
}
} }
staticPages.forEach((pg) => allStaticPages.add(pg)) staticPages.forEach((pg) => allStaticPages.add(pg))
......
...@@ -1598,6 +1598,7 @@ export async function copyTracedFiles( ...@@ -1598,6 +1598,7 @@ export async function copyTracedFiles(
dir: string, dir: string,
distDir: string, distDir: string,
pageKeys: ReadonlyArray<string>, pageKeys: ReadonlyArray<string>,
appPageKeys: readonly string[] | undefined,
tracingRoot: string, tracingRoot: string,
serverConfig: { [key: string]: any }, serverConfig: { [key: string]: any },
middlewareManifest: MiddlewareManifest middlewareManifest: MiddlewareManifest
...@@ -1680,6 +1681,15 @@ export async function copyTracedFiles( ...@@ -1680,6 +1681,15 @@ export async function copyTracedFiles(
Log.warn(`Failed to copy traced files for ${pageFile}`, err) Log.warn(`Failed to copy traced files for ${pageFile}`, err)
}) })
} }
if (appPageKeys) {
for (const page of appPageKeys) {
const pageFile = path.join(distDir, 'server', 'app', `${page}`, 'page.js')
const pageTraceFile = `${pageFile}.nft.json`
await handleTraceFiles(pageTraceFile).catch((err) => {
Log.warn(`Failed to copy traced files for ${pageFile}`, err)
})
}
}
await handleTraceFiles(path.join(distDir, 'next-server.js.nft.json')) await handleTraceFiles(path.join(distDir, 'next-server.js.nft.json'))
const serverOutputPath = path.join( const serverOutputPath = path.join(
outputPath, outputPath,
......
...@@ -5,6 +5,7 @@ module.exports = { ...@@ -5,6 +5,7 @@ module.exports = {
algorithm: 'sha256', algorithm: 'sha256',
}, },
}, },
output: 'standalone',
rewrites: async () => { rewrites: async () => {
return { return {
afterFiles: [ afterFiles: [
......
import os from 'os'
import { createNext, FileRef } from 'e2e-utils' import { createNext, FileRef } from 'e2e-utils'
import crypto from 'crypto' import crypto from 'crypto'
import { NextInstance } from 'test/lib/next-modes/base' import { NextInstance } from 'test/lib/next-modes/base'
import { import {
check, check,
fetchViaHTTP, fetchViaHTTP,
findPort,
getRedboxHeader, getRedboxHeader,
hasRedbox, hasRedbox,
initNextServerScript,
killApp,
renderViaHTTP, renderViaHTTP,
waitFor, waitFor,
} from 'next-test-utils' } from 'next-test-utils'
import path from 'path' import path from 'path'
import cheerio from 'cheerio' import cheerio from 'cheerio'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import fs from 'fs-extra'
describe('app dir', () => { describe('app dir', () => {
const isDev = (global as any).isNextDev const isDev = (global as any).isNextDev
...@@ -2554,4 +2559,44 @@ describe('app dir', () => { ...@@ -2554,4 +2559,44 @@ describe('app dir', () => {
} }
runTests() runTests()
if ((global as any).isNextStart) {
it('should work correctly with output standalone', async () => {
const tmpFolder = path.join(os.tmpdir(), 'next-standalone-' + Date.now())
await fs.move(path.join(next.testDir, '.next/standalone'), tmpFolder)
let server
try {
const testServer = path.join(tmpFolder, 'server.js')
const appPort = await findPort()
server = await initNextServerScript(
testServer,
/Listening on/,
{
...process.env,
PORT: appPort,
},
undefined,
{
cwd: tmpFolder,
}
)
for (const testPath of [
'/',
'/api/hello',
'/blog/first',
'/dashboard',
'/dashboard/deployments/123',
'/catch-all/first',
]) {
const res = await fetchViaHTTP(appPort, testPath)
expect(res.status).toBe(200)
}
} finally {
if (server) await killApp(server)
await fs.remove(tmpFolder)
}
})
}
}) })
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