Skip to content

Commit

Permalink
Merge branch 'canary' into rsc-routing
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Mar 16, 2022
2 parents 540ec0d + 91136d2 commit 771019f
Show file tree
Hide file tree
Showing 48 changed files with 479 additions and 259 deletions.
6 changes: 6 additions & 0 deletions docs/going-to-production.md
Expand Up @@ -69,6 +69,12 @@ export async function getServerSideProps({ req, res }) {
}
```

By default, `Cache-Control` headers will be set differently depending on how your page fetches data.

If the page is using `getServerSideProps` or `getInitialProps`, then it will use the default `Cache-Control` header configured by `next start` in order to prevent accidental caching of responses that cannot be cached. If you want a different cache behavior while using SSR you can use `res.setHeader('Cache-Control', 'value_you_prefer')`.

If the page is using `getStaticProps` or automatic static optimization, then it will have s-maxage=REVALIDATE_SECONDS, stale-while-revalidate or if revalidate is not used s-maxage=31536000, stale-while-revalidate.

> **Note:** Your deployment provider must support edge caching for dynamic responses. If you are self-hosting, you will need to add this logic to the edge yourself using a key/value store. If you are using Vercel, [edge caching works without configuration](https://vercel.com/docs/edge-network/caching).
## Reducing JavaScript Size
Expand Down
3 changes: 0 additions & 3 deletions examples/with-custom-reverse-proxy/.babelrc

This file was deleted.

34 changes: 0 additions & 34 deletions examples/with-custom-reverse-proxy/.gitignore

This file was deleted.

55 changes: 0 additions & 55 deletions examples/with-custom-reverse-proxy/README.md

This file was deleted.

19 changes: 0 additions & 19 deletions examples/with-custom-reverse-proxy/package.json

This file was deleted.

35 changes: 0 additions & 35 deletions examples/with-custom-reverse-proxy/pages/index.js

This file was deleted.

50 changes: 0 additions & 50 deletions examples/with-custom-reverse-proxy/server.js

This file was deleted.

3 changes: 0 additions & 3 deletions packages/next/build/entries.ts
Expand Up @@ -187,9 +187,6 @@ export async function getPageRuntime(
if (!pageRuntime) {
if (isRuntimeRequired) {
pageRuntime = globalRuntimeFallback
} else {
// @TODO: Remove this branch to fully implement the RFC.
pageRuntime = globalRuntimeFallback
}
}

Expand Down
35 changes: 25 additions & 10 deletions packages/next/build/index.ts
Expand Up @@ -76,7 +76,11 @@ import {
} from '../telemetry/events'
import { Telemetry } from '../telemetry/storage'
import { CompilerResult, runCompiler } from './compiler'
import { createEntrypoints, createPagesMapping } from './entries'
import {
createEntrypoints,
createPagesMapping,
getPageRuntime,
} from './entries'
import { generateBuildId } from './generate-build-id'
import { isWriteable } from './is-writeable'
import * as Log from './output/log'
Expand Down Expand Up @@ -153,11 +157,10 @@ export default async function build(
setGlobal('phase', PHASE_PRODUCTION_BUILD)
setGlobal('distDir', distDir)

// Currently, when the runtime option is set (either `nodejs` or `edge`),
// we enable concurrent features (Fizz-related rendering architecture).
const runtime = config.experimental.runtime
// We enable concurrent features (Fizz-related rendering architecture) when
// using React 18 or experimental.
const hasReactRoot = shouldUseReactRoot()
const hasConcurrentFeatures = !!runtime
const hasConcurrentFeatures = hasReactRoot

const hasServerComponents =
hasReactRoot && !!config.experimental.serverComponents
Expand Down Expand Up @@ -622,6 +625,7 @@ export default async function build(
entrypoints: entrypoints.client,
rewrites,
runWebpackSpan,
hasReactRoot,
}),
getBaseWebpackConfig(dir, {
buildId,
Expand All @@ -633,6 +637,7 @@ export default async function build(
entrypoints: entrypoints.server,
rewrites,
runWebpackSpan,
hasReactRoot,
}),
hasReactRoot
? getBaseWebpackConfig(dir, {
Expand All @@ -646,6 +651,7 @@ export default async function build(
entrypoints: entrypoints.edgeServer,
rewrites,
runWebpackSpan,
hasReactRoot,
})
: null,
])
Expand Down Expand Up @@ -954,10 +960,22 @@ export default async function build(
let ssgPageRoutes: string[] | null = null
let isMiddlewareRoute = !!page.match(MIDDLEWARE_ROUTE)

const pagePath = pagePaths.find((_path) =>
_path.startsWith(actualPage + '.')
)
const pageRuntime =
hasConcurrentFeatures && pagePath
? await getPageRuntime(
join(pagesDir, pagePath),
config.experimental.runtime
)
: null

if (
!isMiddlewareRoute &&
!isReservedPage(page) &&
!hasConcurrentFeatures
// We currently don't support staic optimization in the Edge runtime.
pageRuntime !== 'edge'
) {
try {
let isPageStaticSpan =
Expand Down Expand Up @@ -1483,10 +1501,7 @@ export default async function build(

const combinedPages = [...staticPages, ...ssgPages]

if (
!hasConcurrentFeatures &&
(combinedPages.length > 0 || useStatic404 || useDefaultStatic500)
) {
if (combinedPages.length > 0 || useStatic404 || useDefaultStatic500) {
const staticGenerationSpan = nextBuildSpan.traceChild('static-generation')
await staticGenerationSpan.traceAsyncFn(async () => {
detectConflictingPaths(
Expand Down
13 changes: 7 additions & 6 deletions packages/next/build/webpack-config.ts
Expand Up @@ -48,7 +48,6 @@ import type { Span } from '../trace'
import { getRawPageExtensions } from './utils'
import browserslist from 'next/dist/compiled/browserslist'
import loadJsConfig from './load-jsconfig'
import { shouldUseReactRoot } from '../server/config'
import { getMiddlewareSourceMapPlugins } from './webpack/plugins/middleware-source-maps-plugin'

const watchOptions = Object.freeze({
Expand Down Expand Up @@ -310,6 +309,7 @@ export default async function getBaseWebpackConfig(
rewrites,
isDevFallback = false,
runWebpackSpan,
hasReactRoot,
}: {
buildId: string
config: NextConfigComplete
Expand All @@ -323,6 +323,7 @@ export default async function getBaseWebpackConfig(
rewrites: CustomRoutes['rewrites']
isDevFallback?: boolean
runWebpackSpan: Span
hasReactRoot: boolean
}
): Promise<webpack.Configuration> {
const { useTypeScript, jsConfig, resolvedBaseUrl } = await loadJsConfig(
Expand All @@ -335,10 +336,10 @@ export default async function getBaseWebpackConfig(
rewrites.afterFiles.length > 0 ||
rewrites.fallback.length > 0
const hasReactRefresh: boolean = dev && !isServer
const hasReactRoot = shouldUseReactRoot()

const runtime = config.experimental.runtime

// Make sure reactRoot is enabled when react 18 is detected
// Make sure `reactRoot` is enabled when React 18 or experimental is detected.
if (hasReactRoot) {
config.experimental.reactRoot = true
}
Expand All @@ -353,14 +354,14 @@ export default async function getBaseWebpackConfig(
'`experimental.runtime` requires `experimental.reactRoot` to be enabled along with React 18.'
)
}
if (config.experimental.serverComponents && !runtime) {
if (config.experimental.serverComponents && !hasReactRoot) {
throw new Error(
'`experimental.runtime` is required to be set along with `experimental.serverComponents`.'
'`experimental.serverComponents` requires React 18 to be installed.'
)
}

const targetWeb = isEdgeRuntime || !isServer
const hasConcurrentFeatures = !!runtime && hasReactRoot
const hasConcurrentFeatures = hasReactRoot
const hasServerComponents =
hasConcurrentFeatures && !!config.experimental.serverComponents
const disableOptimizedLoading = hasConcurrentFeatures
Expand Down
1 change: 1 addition & 0 deletions packages/next/export/index.ts
Expand Up @@ -588,6 +588,7 @@ export default async function exportApp(
nextConfig.experimental.disableOptimizedLoading,
parentSpanId: pageExportSpan.id,
httpAgentOptions: nextConfig.httpAgentOptions,
serverComponents: nextConfig.experimental.serverComponents,
})

for (const validation of result.ampValidations || []) {
Expand Down

0 comments on commit 771019f

Please sign in to comment.