Skip to content

Commit

Permalink
keep _app.server and not log if not customized
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Mar 29, 2022
1 parent 635fa20 commit b3290c3
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 20 deletions.
7 changes: 4 additions & 3 deletions packages/next/build/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,15 @@ export function createPagesMapping(
pages['/_error'] = `${PAGES_DIR_ALIAS}/_error`
pages['/_document'] = `${PAGES_DIR_ALIAS}/_document`
if (hasServerComponents) {
pages['/_app.server'] = `${PAGES_DIR_ALIAS}/_app.server` || pages['/_app']
pages['/_app.server'] = `${PAGES_DIR_ALIAS}/_app.server`
}
} else {
pages['/_app'] = pages['/_app'] || 'next/dist/pages/_app'
pages['/_error'] = pages['/_error'] || 'next/dist/pages/_error'
pages['/_document'] = pages['/_document'] || `next/dist/pages/_document`
pages['/_document'] = pages['/_document'] || 'next/dist/pages/_document'
if (hasServerComponents) {
pages['/_app.server'] = pages['/_app.server'] || pages['/_app'] // 'next/dist/pages/_app.server'
pages['/_app.server'] =
pages['/_app.server'] || 'next/dist/pages/_app.server'
}
}
return pages
Expand Down
9 changes: 7 additions & 2 deletions packages/next/build/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ export async function printTreeView(
]

const hasCustomApp = await findPageFile(pagesDir, '/_app', pageExtensions)
const hasCustomAppServer = await findPageFile(
pagesDir,
'/_app.server',
pageExtensions
)

pageInfos.set('/404', {
...(pageInfos.get('/404') || pageInfos.get('/_error')),
Expand All @@ -170,8 +175,8 @@ export async function printTreeView(
!(
e === '/_document' ||
e === '/_error' ||
e === '/_app.server' ||
(!hasCustomApp && e === '/_app')
(!hasCustomApp && e === '/_app') ||
(!hasCustomAppServer && e === '/_app.server')
)
)
.sort((a, b) => a.localeCompare(b))
Expand Down
3 changes: 3 additions & 0 deletions packages/next/pages/_app.server.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function AppServer({ children }: { children: React.ReactNode }) {
return children
}
3 changes: 2 additions & 1 deletion packages/next/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,10 @@ function enhanceComponents(
}

function renderFlight(AppMod: any, ComponentMod: any, props: any) {
const isServerComponent = !!ComponentMod.__next_rsc__
const App = interopDefault(AppMod)
const Component = interopDefault(ComponentMod)
const AppServer = !!ComponentMod.__next_rsc__
const AppServer = isServerComponent
? (App as React.ComponentType)
: React.Fragment

Expand Down
16 changes: 8 additions & 8 deletions packages/next/taskfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1829,6 +1829,13 @@ export async function pages_app(task, opts) {
.target('dist/pages')
}

export async function pages_app_server(task, opts) {
await task
.source('pages/_app.server.tsx')
.swc('client', { dev: opts.dev, keepImportAssertions: true })
.target('dist/pages')
}

export async function pages_error(task, opts) {
await task
.source('pages/_error.tsx')
Expand All @@ -1843,16 +1850,9 @@ export async function pages_document(task, opts) {
.target('dist/pages')
}

export async function pages_document_server(task, opts) {
await task
.source('pages/_document-concurrent.tsx')
.swc('client', { dev: opts.dev, keepImportAssertions: true })
.target('dist/pages')
}

export async function pages(task, opts) {
await task.parallel(
['pages_app', 'pages_error', 'pages_document', 'pages_document_server'],
['pages_app', 'pages_error', 'pages_document', 'pages_app_server'],
opts
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export default function AppServer({ children }) {
return (
<div className="app-server-component" style={{ border: '2px solid blue' }}>
<div className="app-server-root">
<style>{`.app-server-root { border: 2px solid blue; }`}</style>
{children}
</div>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function splitLines(text) {
.filter(Boolean)
}

async function testRoute(appPort, url, { isStatic, isEdge }) {
async function testRoute(appPort, url, { isStatic, isEdge, isRSC }) {
const html1 = await renderViaHTTP(appPort, url)
const renderedAt1 = +html1.match(/Time: (\d+)/)[1]
expect(html1).toContain(`Runtime: ${isEdge ? 'Edge' : 'Node.js'}`)
Expand All @@ -29,6 +29,12 @@ async function testRoute(appPort, url, { isStatic, isEdge }) {
// Should be re-rendered.
expect(renderedAt1).toBeLessThan(renderedAt2)
}
const customAppServerHtml = '<div class="app-server-root">'
if (isRSC) {
expect(html1).toContain(customAppServerHtml)
} else {
expect(html1).not.toContain(customAppServerHtml)
}
}

describe('Without global runtime configuration', () => {
Expand All @@ -49,62 +55,71 @@ describe('Without global runtime configuration', () => {
await testRoute(context.appPort, '/static', {
isStatic: true,
isEdge: false,
isRSC: false,
})
})

it('should build /node as a static page with the nodejs runtime', async () => {
await testRoute(context.appPort, '/node', {
isStatic: true,
isEdge: false,
isRSC: false,
})
})

it('should build /node-ssr as a dynamic page with the nodejs runtime', async () => {
await testRoute(context.appPort, '/node-ssr', {
isStatic: false,
isEdge: false,
isRSC: false,
})
})

it('should build /node-ssg as a static page with the nodejs runtime', async () => {
await testRoute(context.appPort, '/node-ssg', {
isStatic: true,
isEdge: false,
isRSC: false,
})
})

it('should build /node-rsc as a static page with the nodejs runtime', async () => {
await testRoute(context.appPort, '/node-rsc', {
isStatic: true,
isEdge: false,
isRSC: true,
})
})

it('should build /node-rsc-ssr as a dynamic page with the nodejs runtime', async () => {
await testRoute(context.appPort, '/node-rsc-ssr', {
isStatic: false,
isEdge: false,
isRSC: true,
})
})

it('should build /node-rsc-ssg as a static page with the nodejs runtime', async () => {
await testRoute(context.appPort, '/node-rsc-ssg', {
isStatic: true,
isEdge: false,
isRSC: true,
})
})

it('should build /edge as a dynamic page with the edge runtime', async () => {
await testRoute(context.appPort, '/edge', {
isStatic: false,
isEdge: true,
isRSC: false,
})
})

it('should build /edge-rsc as a dynamic page with the edge runtime', async () => {
await testRoute(context.appPort, '/edge-rsc', {
isStatic: false,
isEdge: true,
isRSC: true,
})
})

Expand All @@ -113,7 +128,9 @@ describe('Without global runtime configuration', () => {
/^[┌├└/]/.test(line)
)
const expectedOutputLines = splitLines(`
┌ λ /404
┌ /_app
├ λ /_app.server
├ λ /404
├ ℇ /edge
├ ℇ /edge-rsc
├ ○ /node
Expand All @@ -124,9 +141,11 @@ describe('Without global runtime configuration', () => {
├ λ /node-ssr
└ ○ /static
`)
const isMatched = expectedOutputLines.every((line, index) =>
stdoutLines[index].startsWith(line)
)
const isMatched = expectedOutputLines.every((line, index) => {
const matched = stdoutLines[index].startsWith(line)
return matched
})

expect(isMatched).toBe(true)
})
})

0 comments on commit b3290c3

Please sign in to comment.