Unverified Commit 9c5bb5bf authored by Steven's avatar Steven Committed by GitHub

BREAKING CHANGE: Remove `target: serverless` (#41495)

The `target: serverless` config was deprecated a year ago starting in
[Next.js 12](https://nextjs.org/blog/next-12).

Tests were disabled in #41252 so we can now remove `target: serverless`
and all usage of `target` in `next.config.js`.
Co-authored-by: 's avatarBalázs Orbán <info@balazsorban.com>
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent ebae05ee
......@@ -15,6 +15,8 @@ A [codemod is available](/docs/advanced-features/codemods.md#next-image-to-legac
The `next/link` child can no longer be `<a>`. Add the `legacyBehavior` prop to use the legacy behavior or remove the `<a>` to upgrade. A [codemod is available](/docs/advanced-features/codemods.md#new-link) to automatically upgrade your code.
The `target` configuration option has been removed and superseded by [Output File Tracing](https://nextjs.org/docs/advanced-features/output-file-tracing).
## Upgrading to 12.2
If you were using Middleware prior to `12.2`, please see the [upgrade guide](https://nextjs.org/docs/messages/middleware-upgrade-guide) for more information.
......
......@@ -2,11 +2,11 @@
#### Why This Error Occurred
The `target` property in `next.config.js` has been deprecated. Please migrate to leverage the default target instead.
Starting in Next.js 13, the `target` property in `next.config.js` has been removed.
#### Possible Ways to Fix It
For serverless cases, leverage the new output file traces or deploy your application somewhere where they are leveraged automatically like [Vercel](https://vercel.com).
For serverless targets, please use the Output File Tracing or deploy your application somewhere where it can be leveraged automatically, like [Vercel](https://vercel.com).
### Useful Links
......
......@@ -4,7 +4,7 @@
You attempted to statically export your application via `next export`, however, one or more of your pages uses `getServerSideProps`.
The `getServerSideProps` lifecycle is not compatible with `next export`, so you'll need to use `next start` or a [serverless deployment](https://vercel.com).
The `getServerSideProps` lifecycle is not compatible with `next export`, so you'll need to use `next start` when self hosting or deploy to a provider like [Vercel](https://vercel.com).
#### Possible Ways to Fix It
......
......@@ -2,7 +2,6 @@ import type { ClientPagesLoaderOptions } from './webpack/loaders/next-client-pag
import type { MiddlewareLoaderOptions } from './webpack/loaders/next-middleware-loader'
import type { EdgeSSRLoaderQuery } from './webpack/loaders/next-edge-ssr-loader'
import type { NextConfigComplete } from '../server/config-shared'
import type { ServerlessLoaderQuery } from './webpack/loaders/next-serverless-loader'
import type { webpack } from 'next/dist/compiled/webpack/webpack'
import type {
MiddlewareConfig,
......@@ -14,7 +13,6 @@ import { posix, join } from 'path'
import { stringify } from 'querystring'
import {
API_ROUTE,
DOT_NEXT_ALIAS,
PAGES_DIR_ALIAS,
ROOT_DIR_ALIAS,
APP_DIR_ALIAS,
......@@ -33,13 +31,11 @@ import {
EDGE_RUNTIME_WEBPACK,
} from '../shared/lib/constants'
import { __ApiPreviewProps } from '../server/api-utils'
import { isTargetLikeServerless } from '../server/utils'
import { warn } from './output/log'
import {
isMiddlewareFile,
isMiddlewareFilename,
NestedMiddlewareError,
MiddlewareInServerlessTargetError,
} from './utils'
import { getPageStaticInfo } from './analysis/get-page-static-info'
import { normalizePathSep } from '../shared/lib/page-path/normalize-path-sep'
......@@ -147,7 +143,6 @@ interface CreateEntrypointsParams {
previewMode: __ApiPreviewProps
rootDir: string
rootPaths?: Record<string, string>
target: 'server' | 'serverless' | 'experimental-serverless-trace'
appDir?: string
appPaths?: Record<string, string>
pageExtensions: string[]
......@@ -229,48 +224,6 @@ export function getAppEntry(opts: {
}
}
export function getServerlessEntry(opts: {
absolutePagePath: string
buildId: string
config: NextConfigComplete
envFiles: LoadedEnvFiles
page: string
previewMode: __ApiPreviewProps
pages: { [page: string]: string }
}): ObjectValue<webpack.EntryObject> {
const loaderParams: ServerlessLoaderQuery = {
absolute404Path: opts.pages['/404'] || '',
absoluteAppPath: opts.pages['/_app'],
absoluteDocumentPath: opts.pages['/_document'],
absoluteErrorPath: opts.pages['/_error'],
absolutePagePath: opts.absolutePagePath,
assetPrefix: opts.config.assetPrefix,
basePath: opts.config.basePath,
buildId: opts.buildId,
canonicalBase: opts.config.amp.canonicalBase || '',
distDir: DOT_NEXT_ALIAS,
generateEtags: opts.config.generateEtags ? 'true' : '',
i18n: opts.config.i18n ? JSON.stringify(opts.config.i18n) : '',
// base64 encode to make sure contents don't break webpack URL loading
loadedEnvFiles: Buffer.from(JSON.stringify(opts.envFiles)).toString(
'base64'
),
page: opts.page,
poweredByHeader: opts.config.poweredByHeader ? 'true' : '',
previewProps: JSON.stringify(opts.previewMode),
runtimeConfig:
Object.keys(opts.config.publicRuntimeConfig).length > 0 ||
Object.keys(opts.config.serverRuntimeConfig).length > 0
? JSON.stringify({
publicRuntimeConfig: opts.config.publicRuntimeConfig,
serverRuntimeConfig: opts.config.serverRuntimeConfig,
})
: '',
}
return `next-serverless-loader?${stringify(loaderParams)}!`
}
export function getClientEntry(opts: {
absolutePagePath: string
page: string
......@@ -340,7 +293,6 @@ export async function createEntrypoints(params: CreateEntrypointsParams) {
isDev,
rootDir,
rootPaths,
target,
appDir,
appPaths,
pageExtensions,
......@@ -428,10 +380,6 @@ export async function createEntrypoints(params: CreateEntrypointsParams) {
middlewareMatchers = staticInfo.middleware?.matchers ?? [
{ regexp: '.*' },
]
if (target === 'serverless') {
throw new MiddlewareInServerlessTargetError()
}
}
await runDependingOnPageType({
......@@ -459,14 +407,6 @@ export async function createEntrypoints(params: CreateEntrypointsParams) {
appPaths: matchedAppPaths,
pageExtensions,
})
} else if (isTargetLikeServerless(target)) {
if (page !== '/_app' && page !== '/_document') {
server[serverBundlePath] = getServerlessEntry({
...params,
absolutePagePath: mappings[page],
page,
})
}
} else {
server[serverBundlePath] = [mappings[page]]
}
......
......@@ -48,7 +48,6 @@ import {
FLIGHT_MANIFEST,
REACT_LOADABLE_MANIFEST,
ROUTES_MANIFEST,
SERVERLESS_DIRECTORY,
SERVER_DIRECTORY,
SERVER_FILES_MANIFEST,
STATIC_STATUS_PAGES,
......@@ -64,7 +63,6 @@ import {
import { getSortedRoutes, isDynamicRoute } from '../shared/lib/router/utils'
import { __ApiPreviewProps } from '../server/api-utils'
import loadConfig from '../server/config'
import { isTargetLikeServerless } from '../server/utils'
import { BuildManifest } from '../server/get-page-files'
import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'
import { getPagePath } from '../server/require'
......@@ -433,8 +431,6 @@ export default async function build(
prefixText: `${Log.prefixes.info} Creating an optimized production build`,
})
const isLikeServerless = isTargetLikeServerless(target)
const pagesPaths = pagesDir
? await nextBuildSpan
.traceChild('collect-pages')
......@@ -531,7 +527,6 @@ export default async function build(
pages: mappedPages,
pagesDir,
previewMode: previewProps,
target,
rootDir: dir,
rootPaths: mappedRootPaths,
appDir,
......@@ -807,10 +802,7 @@ export default async function build(
)
)
const serverDir = isLikeServerless
? SERVERLESS_DIRECTORY
: SERVER_DIRECTORY
const manifestPath = path.join(distDir, serverDir, PAGES_MANIFEST)
const manifestPath = path.join(distDir, SERVER_DIRECTORY, PAGES_MANIFEST)
const requiredServerFiles = nextBuildSpan
.traceChild('generate-required-server-files')
......@@ -846,9 +838,11 @@ export default async function build(
]
: []),
REACT_LOADABLE_MANIFEST,
config.optimizeFonts ? path.join(serverDir, FONT_MANIFEST) : null,
config.optimizeFonts
? path.join(SERVER_DIRECTORY, FONT_MANIFEST)
: null,
BUILD_ID_FILE,
appDir ? path.join(serverDir, APP_PATHS_MANIFEST) : null,
appDir ? path.join(SERVER_DIRECTORY, APP_PATHS_MANIFEST) : null,
...(config.experimental.fontLoaders
? [
path.join(SERVER_DIRECTORY, FONT_LOADER_MANIFEST + '.js'),
......@@ -924,60 +918,30 @@ export default async function build(
// We run client and server compilation separately to optimize for memory usage
await runWebpackSpan.traceAsyncFn(async () => {
// If we are under the serverless build, we will have to run the client
// compiler first because the server compiler depends on the manifest
// files that are created by the client compiler.
// Otherwise, we run the server compilers first and then the client
// Run the server compilers first and then the client
// compiler to track the boundary of server/client components.
let clientResult: SingleCompilerResult | null = null
let serverResult: SingleCompilerResult | null = null
let edgeServerResult: SingleCompilerResult | null = null
if (isLikeServerless) {
if (appDir) {
throw new Error('`appDir` is not supported in serverless mode.')
}
// During the server compilations, entries of client components will be
// injected to this set and then will be consumed by the client compiler.
injectedClientEntries.clear()
// Build client first
clientResult = await runCompiler(clientConfig, {
runWebpackSpan,
const serverResult = await runCompiler(configs[1], {
runWebpackSpan,
})
const edgeServerResult = configs[2]
? await runCompiler(configs[2], { runWebpackSpan })
: null
// Only continue if there were no errors
if (!serverResult.errors.length && !edgeServerResult?.errors.length) {
injectedClientEntries.forEach((value, key) => {
;(clientConfig.entry as webpack.EntryObject)[key] = value
})
// Only continue if there were no errors
if (!clientResult.errors.length) {
serverResult = await runCompiler(configs[1], {
runWebpackSpan,
})
edgeServerResult = configs[2]
? await runCompiler(configs[2], { runWebpackSpan })
: null
}
} else {
// During the server compilations, entries of client components will be
// injected to this set and then will be consumed by the client compiler.
injectedClientEntries.clear()
serverResult = await runCompiler(configs[1], {
clientResult = await runCompiler(clientConfig, {
runWebpackSpan,
})
edgeServerResult = configs[2]
? await runCompiler(configs[2], { runWebpackSpan })
: null
// Only continue if there were no errors
if (
!serverResult.errors.length &&
!edgeServerResult?.errors.length
) {
injectedClientEntries.forEach((value, key) => {
;(clientConfig.entry as webpack.EntryObject)[key] = value
})
clientResult = await runCompiler(clientConfig, {
runWebpackSpan,
})
}
}
result = {
......@@ -1120,7 +1084,7 @@ export default async function build(
if (appDir) {
appPathsManifest = JSON.parse(
await promises.readFile(
path.join(distDir, serverDir, APP_PATHS_MANIFEST),
path.join(distDir, SERVER_DIRECTORY, APP_PATHS_MANIFEST),
'utf8'
)
)
......@@ -1209,7 +1173,6 @@ export default async function build(
(await staticWorkers.hasCustomGetInitialProps(
'/_error',
distDir,
isLikeServerless,
runtimeEnvConfig,
false
))
......@@ -1221,7 +1184,6 @@ export default async function build(
staticWorkers.isPageStatic({
page: '/_error',
distDir,
serverless: isLikeServerless,
configFileName,
runtimeEnvConfig,
httpAgentOptions: config.httpAgentOptions,
......@@ -1232,15 +1194,12 @@ export default async function build(
})
)
// we don't output _app in serverless mode so use _app export
// from _error instead
const appPageToCheck = isLikeServerless ? '/_error' : '/_app'
const appPageToCheck = '/_app'
const customAppGetInitialPropsPromise =
staticWorkers.hasCustomGetInitialProps(
appPageToCheck,
distDir,
isLikeServerless,
runtimeEnvConfig,
true
)
......@@ -1248,7 +1207,6 @@ export default async function build(
const namedExportsPromise = staticWorkers.getNamedExports(
appPageToCheck,
distDir,
isLikeServerless,
runtimeEnvConfig
)
......@@ -1352,7 +1310,7 @@ export default async function build(
if (pageRuntime === SERVER_RUNTIME.edge) {
const manifest = require(join(
distDir,
serverDir,
SERVER_DIRECTORY,
MIDDLEWARE_MANIFEST
))
const manifestKey =
......@@ -1369,7 +1327,6 @@ export default async function build(
page,
originalAppPath,
distDir,
serverless: isLikeServerless,
configFileName,
runtimeEnvConfig,
httpAgentOptions: config.httpAgentOptions,
......@@ -1931,7 +1888,7 @@ export default async function build(
const middlewareManifest: MiddlewareManifest = JSON.parse(
await promises.readFile(
path.join(distDir, serverDir, MIDDLEWARE_MANIFEST),
path.join(distDir, SERVER_DIRECTORY, MIDDLEWARE_MANIFEST),
'utf8'
)
)
......@@ -2145,7 +2102,7 @@ export default async function build(
// remove server bundles that were exported
for (const page of staticPages) {
const serverBundle = getPagePath(page, distDir, isLikeServerless)
const serverBundle = getPagePath(page, distDir)
await promises.unlink(serverBundle)
}
......@@ -2214,15 +2171,11 @@ export default async function build(
.traceAsyncFn(async () => {
file = `${file}.${ext}`
const orig = path.join(exportOptions.outdir, file)
const pagePath = getPagePath(
originPage,
distDir,
isLikeServerless
)
const pagePath = getPagePath(originPage, distDir)
const relativeDest = path
.relative(
path.join(distDir, serverDir),
path.join(distDir, SERVER_DIRECTORY),
path.join(
path.join(
pagePath,
......@@ -2253,7 +2206,7 @@ export default async function build(
pagesManifest[page] = relativeDest
}
const dest = path.join(distDir, serverDir, relativeDest)
const dest = path.join(distDir, SERVER_DIRECTORY, relativeDest)
const isNotFound = ssgNotFoundPaths.includes(page)
// for SSG files with i18n the non-prerendered variants are
......@@ -2299,7 +2252,7 @@ export default async function build(
)
const updatedDest = path.join(
distDir,
serverDir,
SERVER_DIRECTORY,
updatedRelativeDest
)
......@@ -2651,7 +2604,7 @@ export default async function build(
})
await nextBuildSpan.traceChild('print-tree-view').traceAsyncFn(() =>
printTreeView(pageKeys, allPageInfos, isLikeServerless, {
printTreeView(pageKeys, allPageInfos, {
distPath: distDir,
buildId: buildId,
pagesDir,
......
......@@ -293,7 +293,6 @@ export async function printTreeView(
app?: ReadonlyArray<string>
},
pageInfos: Map<string, PageInfo>,
serverless: boolean,
{
distPath,
buildId,
......@@ -627,7 +626,7 @@ export async function printTreeView(
],
usedSymbols.has('λ') && [
'λ',
serverless ? '(Lambda)' : '(Server)',
'(Server)',
`server-side renders at runtime (uses ${chalk.cyan(
'getInitialProps'
)} or ${chalk.cyan('getServerSideProps')})`,
......@@ -1165,7 +1164,6 @@ export async function buildAppStaticPaths({
export async function isPageStatic({
page,
distDir,
serverless,
configFileName,
runtimeEnvConfig,
httpAgentOptions,
......@@ -1181,7 +1179,6 @@ export async function isPageStatic({
}: {
page: string
distDir: string
serverless: boolean
configFileName: string
runtimeEnvConfig: any
httpAgentOptions: NextConfigComplete['httpAgentOptions']
......@@ -1250,7 +1247,6 @@ export async function isPageStatic({
componentsResult = await loadComponents({
distDir,
pathname: originalAppPath || page,
serverless,
hasServerComponents: !!hasServerComponents,
isAppPath: pageType === 'app',
})
......@@ -1430,7 +1426,6 @@ export async function isPageStatic({
export async function hasCustomGetInitialProps(
page: string,
distDir: string,
isLikeServerless: boolean,
runtimeEnvConfig: any,
checkingApp: boolean
): Promise<boolean> {
......@@ -1439,7 +1434,6 @@ export async function hasCustomGetInitialProps(
const components = await loadComponents({
distDir,
pathname: page,
serverless: isLikeServerless,
hasServerComponents: false,
isAppPath: false,
})
......@@ -1457,14 +1451,12 @@ export async function hasCustomGetInitialProps(
export async function getNamedExports(
page: string,
distDir: string,
isLikeServerless: boolean,
runtimeEnvConfig: any
): Promise<Array<string>> {
require('../shared/lib/runtime-config').setConfig(runtimeEnvConfig)
const components = await loadComponents({
distDir,
pathname: page,
serverless: isLikeServerless,
hasServerComponents: false,
isAppPath: false,
})
......@@ -1726,16 +1718,6 @@ export function getPossibleMiddlewareFilenames(
)
}
export class MiddlewareInServerlessTargetError extends Error {
constructor() {
super(
'Next.js Middleware is not supported in the deprecated serverless target.\n' +
'Please remove `target: "serverless" from your next.config.js to use Middleware.'
)
this.name = 'MiddlewareInServerlessTargetError'
}
}
export class NestedMiddlewareError extends Error {
constructor(nestedFileNames: string[], mainDir: string, pagesDir: string) {
super(
......
......@@ -24,7 +24,6 @@ import {
CLIENT_STATIC_FILES_RUNTIME_WEBPACK,
MIDDLEWARE_REACT_LOADABLE_MANIFEST,
REACT_LOADABLE_MANIFEST,
SERVERLESS_DIRECTORY,
SERVER_DIRECTORY,
COMPILER_NAMES,
CompilerNameValues,
......@@ -43,7 +42,6 @@ import { DropClientPage } from './webpack/plugins/next-drop-client-page-plugin'
import PagesManifestPlugin from './webpack/plugins/pages-manifest-plugin'
import { ProfilingPlugin } from './webpack/plugins/profiling-plugin'
import { ReactLoadablePlugin } from './webpack/plugins/react-loadable-plugin'
import { ServerlessPlugin } from './webpack/plugins/serverless-plugin'
import { WellKnownErrorsPlugin } from './webpack/plugins/wellknown-errors-plugin'
import { regexLikeCss } from './webpack/config/blocks/css'
import { CopyFilePlugin } from './webpack/plugins/copy-file-plugin'
......@@ -719,16 +717,9 @@ export default async function getBaseWebpackConfig(
const pageExtensions = config.pageExtensions
// Intentionally not using isTargetLikeServerless helper
const isLikeServerless =
target === 'serverless' || target === 'experimental-serverless-trace'
const outputPath =
isNodeServer || isEdgeServer
? path.join(
distDir,
isLikeServerless ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY
)
? path.join(distDir, SERVER_DIRECTORY)
: distDir
const clientEntries = isClient
......@@ -1242,8 +1233,7 @@ export default async function getBaseWebpackConfig(
]
: []),
]
: target !== 'serverless'
? [
: [
({
context,
request,
......@@ -1296,15 +1286,6 @@ export default async function getBaseWebpackConfig(
})
}
),
]
: [
// When the 'serverless' target is used all node_modules will be compiled into the output bundles
// So that the 'serverless' bundles have 0 runtime dependencies
'next/dist/compiled/@ampproject/toolbox-optimizer', // except this one
// Mark this as external if not enabled so it doesn't cause a
// webpack error from being missing
...(config.experimental.optimizeCss ? [] : ['critters']),
],
optimization: {
emitOnErrors: !dev,
......@@ -1848,7 +1829,6 @@ export default async function getBaseWebpackConfig(
}),
(isClient || isEdgeServer) && new DropClientPage(),
config.outputFileTracing &&
!isLikeServerless &&
(isNodeServer || isEdgeServer) &&
!dev &&
new (require('./webpack/plugins/next-trace-entrypoints-plugin').TraceEntryPointsPlugin)(
......@@ -1893,12 +1873,8 @@ export default async function getBaseWebpackConfig(
resourceRegExp: /react-is/,
contextRegExp: /next[\\/]dist[\\/]/,
}),
target === 'serverless' &&
(isNodeServer || isEdgeServer) &&
new ServerlessPlugin(),
(isNodeServer || isEdgeServer) &&
new PagesManifestPlugin({
serverless: isLikeServerless,
dev,
isEdgeRuntime: isEdgeServer,
appDirEnabled: hasAppDir,
......@@ -1931,7 +1907,6 @@ export default async function getBaseWebpackConfig(
FontStylesheetGatheringPlugin: typeof import('./webpack/plugins/font-stylesheet-gathering-plugin').FontStylesheetGatheringPlugin
}
return new FontStylesheetGatheringPlugin({
isLikeServerless,
adjustFontFallbacks: config.experimental.adjustFontFallbacks,
adjustFontFallbacksWithSizeAdjust:
config.experimental.adjustFontFallbacksWithSizeAdjust,
......
......@@ -52,20 +52,16 @@ export class FontStylesheetGatheringPlugin {
compiler?: webpack.Compiler
gatheredStylesheets: Array<string> = []
manifestContent: FontManifest = []
isLikeServerless: boolean
adjustFontFallbacks?: boolean
adjustFontFallbacksWithSizeAdjust?: boolean
constructor({
isLikeServerless,
adjustFontFallbacks,
adjustFontFallbacksWithSizeAdjust,
}: {
isLikeServerless: boolean
adjustFontFallbacks?: boolean
adjustFontFallbacksWithSizeAdjust?: boolean
}) {
this.isLikeServerless = isLikeServerless
this.adjustFontFallbacks = adjustFontFallbacks
this.adjustFontFallbacksWithSizeAdjust = adjustFontFallbacksWithSizeAdjust
}
......@@ -184,25 +180,6 @@ export class FontStylesheetGatheringPlugin {
this.parserHandler
)
compiler.hooks.make.tapAsync(this.constructor.name, (compilation, cb) => {
if (this.isLikeServerless) {
/**
* Inline font manifest for serverless case only.
* For target: server drive the manifest through physical file and less of webpack magic.
*/
const mainTemplate = compilation.mainTemplate
mainTemplate.hooks.requireExtensions.tap(
this.constructor.name,
(source: string) => {
return `${source}
// Font manifest declaration
__webpack_require__.__NEXT_FONT_MANIFEST__ = ${JSON.stringify(
this.manifestContent
)};
// Enable feature:
process.env.__NEXT_OPTIMIZE_FONTS = JSON.stringify(true);`
}
)
}
compilation.hooks.finishModules.tapAsync(
this.constructor.name,
async (modules: any, modulesFinished: Function) => {
......
......@@ -19,23 +19,19 @@ let nodeServerAppPaths = {}
export default class PagesManifestPlugin
implements webpack.WebpackPluginInstance
{
serverless: boolean
dev: boolean
isEdgeRuntime: boolean
appDirEnabled: boolean
constructor({
serverless,
dev,
isEdgeRuntime,
appDirEnabled,
}: {
serverless: boolean
dev: boolean
isEdgeRuntime: boolean
appDirEnabled: boolean
}) {
this.serverless = serverless
this.dev = dev
this.isEdgeRuntime = isEdgeRuntime
this.appDirEnabled = appDirEnabled
......
import { webpack } from 'next/dist/compiled/webpack/webpack'
/**
* Makes sure there are no dynamic chunks when the target is serverless
* The dynamic chunks are integrated back into their parent chunk
* This is to make sure there is a single render bundle instead of that bundle importing dynamic chunks
*/
export class ServerlessPlugin {
apply(compiler: webpack.Compiler) {
compiler.hooks.compilation.tap('ServerlessPlugin', (compilation) => {
const hook = compilation.hooks.optimizeChunks
hook.tap('ServerlessPlugin', (chunks) => {
for (const chunk of chunks) {
// If chunk is not an entry point skip them
if (compilation.chunkGraph.getNumberOfEntryModules(chunk) === 0) {
continue
}
// Async chunks are usages of import() for example
const dynamicChunks = chunk.getAllAsyncChunks()
for (const dynamicChunk of dynamicChunks) {
for (const module of compilation.chunkGraph.getChunkModulesIterable(
dynamicChunk
)) {
// Add module back into the entry chunk
if (!compilation.chunkGraph.isModuleInChunk(module, chunk)) {
compilation.chunkGraph.connectChunkAndModule(chunk, module)
}
}
}
}
})
})
}
}
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -28,11 +28,9 @@ import {
PAGES_MANIFEST,
PHASE_EXPORT,
PRERENDER_MANIFEST,
SERVERLESS_DIRECTORY,
SERVER_DIRECTORY,
} from '../shared/lib/constants'
import loadConfig from '../server/config'
import { isTargetLikeServerless } from '../server/utils'
import { ExportPathMap, NextConfigComplete } from '../server/config-shared'
import { eventCliSession } from '../telemetry/events'
import { hasNextSupport } from '../telemetry/ci-info'
......@@ -180,7 +178,6 @@ export default async function exportApp(
}
const subFolders = nextConfig.trailingSlash && !options.buildExport
const isLikeServerless = nextConfig.target !== 'server'
if (!options.silent && !options.buildExport) {
Log.info(`using build directory: ${distDir}`)
......@@ -215,7 +212,7 @@ export default async function exportApp(
!options.pages &&
(require(join(
distDir,
isLikeServerless ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY,
SERVER_DIRECTORY,
PAGES_MANIFEST
)) as PagesManifest)
......@@ -611,7 +608,6 @@ export default async function exportApp(
serverRuntimeConfig,
subFolders,
buildExport: options.buildExport,
serverless: isTargetLikeServerless(nextConfig.target),
optimizeFonts: nextConfig.optimizeFonts as FontConfig,
optimizeCss: nextConfig.experimental.optimizeCss,
disableOptimizedLoading:
......@@ -673,7 +669,7 @@ export default async function exportApp(
}
route = normalizePagePath(route)
const pagePath = getPagePath(pageName, distDir, isLikeServerless)
const pagePath = getPagePath(pageName, distDir)
const distPagesDir = join(
pagePath,
// strip leading / and then recurse number of nested dirs
......
import type { ComponentType } from 'react'
import type { FontManifest, FontConfig } from '../server/font-utils'
import type { GetStaticProps } from '../types'
import type { IncomingMessage, ServerResponse } from 'http'
import type { DomainLocale, NextConfigComplete } from '../server/config-shared'
import type { NextParsedUrlQuery } from '../server/request-meta'
......@@ -8,7 +6,6 @@ import type { NextParsedUrlQuery } from '../server/request-meta'
import '../server/node-polyfill-fetch'
import loadRequireHook from '../build/webpack/require-hook'
import url from 'url'
import { extname, join, dirname, sep } from 'path'
import { renderToHTML } from '../server/render'
import { promises } from 'fs'
......@@ -62,7 +59,6 @@ interface ExportPageInput {
buildExport?: boolean
serverRuntimeConfig: { [key: string]: any }
subFolders?: boolean
serverless: boolean
optimizeFonts: FontConfig
optimizeCss: any
disableOptimizedLoading: any
......@@ -99,11 +95,6 @@ interface RenderOpts {
supportsDynamicHTML?: boolean
}
type ComponentModule = ComponentType<{}> & {
renderReqToHTML: typeof renderToHTML
getStaticProps?: GetStaticProps
}
export default async function exportPage({
parentSpanId,
path,
......@@ -115,7 +106,6 @@ export default async function exportPage({
buildExport,
serverRuntimeConfig,
subFolders,
serverless,
optimizeFonts,
optimizeCss,
disableOptimizedLoading,
......@@ -191,12 +181,9 @@ export default async function exportPage({
getRouteMatcher(getRouteRegex(normalizedPage))(updatedPath) ||
undefined
if (params) {
// we have to pass these separately for serverless
if (!serverless) {
query = {
...query,
...params,
}
query = {
...query,
...params,
}
} else {
throw new Error(
......@@ -267,8 +254,7 @@ export default async function exportPage({
)
// If the ssg path has .html extension, and it's not builtin paths, use it directly
// Otherwise, use that as the filename instead
const isHtmlExtPath =
!serverless && !isBuiltinPaths && path.endsWith('.html')
const isHtmlExtPath = !isBuiltinPaths && path.endsWith('.html')
htmlFilename = isHtmlExtPath ? getHtmlFilename(path) : path
} else if (path === '/') {
// If the path is the root, just use index.html
......@@ -289,205 +275,118 @@ export default async function exportPage({
return !buildExport && getStaticProps && !isDynamicRoute(path)
}
if (serverless) {
const curUrl = url.parse(req.url!, true)
req.url = url.format({
...curUrl,
query: {
...curUrl.query,
...query,
},
})
const {
Component,
ComponentMod,
getServerSideProps,
getStaticProps,
pageConfig,
} = await loadComponents({
distDir,
pathname: page,
serverless,
hasServerComponents: !!serverComponents,
isAppPath: isAppDir,
})
const ampState = {
ampFirst: pageConfig?.amp === true,
hasQuery: Boolean(query.amp),
hybrid: pageConfig?.amp === 'hybrid',
}
inAmpMode = isInAmpMode(ampState)
hybridAmp = ampState.hybrid
const components = await loadComponents({
distDir,
pathname: page,
hasServerComponents: !!serverComponents,
isAppPath: isAppDir,
})
curRenderOpts = {
...components,
...renderOpts,
ampPath: renderAmpPath,
params,
optimizeFonts,
optimizeCss,
disableOptimizedLoading,
fontManifest: optimizeFonts ? requireFontManifest(distDir) : null,
locale: locale as string,
supportsDynamicHTML: false,
}
if (getServerSideProps) {
throw new Error(
`Error for page ${page}: ${SERVER_PROPS_EXPORT_ERROR}`
)
}
// during build we attempt rendering app dir paths
// and bail when dynamic dependencies are detected
// only fully static paths are fully generated here
if (isAppDir) {
const { renderToHTMLOrFlight } =
require('../server/app-render') as typeof import('../server/app-render')
// if it was auto-exported the HTML is loaded here
if (typeof Component === 'string') {
renderResult = RenderResult.fromStatic(Component)
queryWithAutoExportWarn()
} else {
// for non-dynamic SSG pages we should have already
// prerendered the file
if (renderedDuringBuild(getStaticProps))
return { ...results, duration: Date.now() - start }
if (getStaticProps && !htmlFilepath.endsWith('.html')) {
// make sure it ends with .html if the name contains a dot
htmlFilename += '.html'
htmlFilepath += '.html'
}
try {
curRenderOpts.params ||= {}
renderMethod = (ComponentMod as ComponentModule).renderReqToHTML
const result = await renderMethod(
req,
res,
'export',
{
ampPath: renderAmpPath,
/// @ts-ignore
optimizeFonts,
/// @ts-ignore
optimizeCss,
disableOptimizedLoading,
distDir,
fontManifest: optimizeFonts
? requireFontManifest(distDir, serverless)
: null,
locale: locale!,
locales: renderOpts.locales!,
},
// @ts-ignore
params
const result = await renderToHTMLOrFlight(
req as any,
res as any,
page,
query,
curRenderOpts as any
)
curRenderOpts = (result as any).renderOpts || {}
renderResult = (result as any).html
}
if (!renderResult && !(curRenderOpts as any).isNotFound) {
throw new Error(`Failed to render serverless page`)
}
} else {
const components = await loadComponents({
distDir,
pathname: page,
serverless,
hasServerComponents: !!serverComponents,
isAppPath: isAppDir,
})
curRenderOpts = {
...components,
...renderOpts,
ampPath: renderAmpPath,
params,
optimizeFonts,
optimizeCss,
disableOptimizedLoading,
fontManifest: optimizeFonts
? requireFontManifest(distDir, serverless)
: null,
locale: locale as string,
supportsDynamicHTML: false,
}
// during build we attempt rendering app dir paths
// and bail when dynamic dependencies are detected
// only fully static paths are fully generated here
if (isAppDir) {
const { renderToHTMLOrFlight } =
require('../server/app-render') as typeof import('../server/app-render')
try {
curRenderOpts.params ||= {}
const result = await renderToHTMLOrFlight(
req as any,
res as any,
page,
query,
curRenderOpts as any
const html = result?.toUnchunkedString()
const flightData = (curRenderOpts as any).pageData
const revalidate = (curRenderOpts as any).revalidate
results.fromBuildExportRevalidate = revalidate
if (revalidate !== 0) {
await promises.writeFile(htmlFilepath, html, 'utf8')
await promises.writeFile(
htmlFilepath.replace(/\.html$/, '.rsc'),
flightData
)
const html = result?.toUnchunkedString()
const flightData = (curRenderOpts as any).pageData
const revalidate = (curRenderOpts as any).revalidate
results.fromBuildExportRevalidate = revalidate
if (revalidate !== 0) {
await promises.writeFile(htmlFilepath, html, 'utf8')
await promises.writeFile(
htmlFilepath.replace(/\.html$/, '.rsc'),
flightData
)
}
} catch (err: any) {
if (
err.digest !== DYNAMIC_ERROR_CODE &&
err.digest !== NOT_FOUND_ERROR_CODE &&
!err.digest?.startsWith(REDIRECT_ERROR_CODE)
) {
throw err
}
}
return { ...results, duration: Date.now() - start }
} catch (err: any) {
if (
err.digest !== DYNAMIC_ERROR_CODE &&
err.digest !== NOT_FOUND_ERROR_CODE &&
!err.digest?.startsWith(REDIRECT_ERROR_CODE)
) {
throw err
}
}
const ampState = {
ampFirst: components.pageConfig?.amp === true,
hasQuery: Boolean(query.amp),
hybrid: components.pageConfig?.amp === 'hybrid',
}
inAmpMode = isInAmpMode(ampState)
hybridAmp = ampState.hybrid
return { ...results, duration: Date.now() - start }
}
if (components.getServerSideProps) {
throw new Error(
`Error for page ${page}: ${SERVER_PROPS_EXPORT_ERROR}`
)
}
const ampState = {
ampFirst: components.pageConfig?.amp === true,
hasQuery: Boolean(query.amp),
hybrid: components.pageConfig?.amp === 'hybrid',
}
inAmpMode = isInAmpMode(ampState)
hybridAmp = ampState.hybrid
// for non-dynamic SSG pages we should have already
// prerendered the file
if (renderedDuringBuild(components.getStaticProps)) {
return { ...results, duration: Date.now() - start }
}
if (components.getServerSideProps) {
throw new Error(`Error for page ${page}: ${SERVER_PROPS_EXPORT_ERROR}`)
}
// TODO: de-dupe the logic here between serverless and server mode
if (components.getStaticProps && !htmlFilepath.endsWith('.html')) {
// make sure it ends with .html if the name contains a dot
htmlFilepath += '.html'
htmlFilename += '.html'
}
// for non-dynamic SSG pages we should have already
// prerendered the file
if (renderedDuringBuild(components.getStaticProps)) {
return { ...results, duration: Date.now() - start }
}
if (typeof components.Component === 'string') {
renderResult = RenderResult.fromStatic(components.Component)
queryWithAutoExportWarn()
} else {
/**
* This sets environment variable to be used at the time of static export by head.tsx.
* Using this from process.env allows targeting both serverless and SSR by calling
* `process.env.__NEXT_OPTIMIZE_FONTS`.
* TODO(prateekbh@): Remove this when experimental.optimizeFonts are being cleaned up.
*/
if (optimizeFonts) {
process.env.__NEXT_OPTIMIZE_FONTS = JSON.stringify(optimizeFonts)
}
if (optimizeCss) {
process.env.__NEXT_OPTIMIZE_CSS = JSON.stringify(true)
}
renderResult = await renderMethod(
req,
res,
page,
query,
// @ts-ignore
curRenderOpts
)
// TODO: de-dupe the logic here between serverless and server mode
if (components.getStaticProps && !htmlFilepath.endsWith('.html')) {
// make sure it ends with .html if the name contains a dot
htmlFilepath += '.html'
htmlFilename += '.html'
}
if (typeof components.Component === 'string') {
renderResult = RenderResult.fromStatic(components.Component)
queryWithAutoExportWarn()
} else {
/**
* This sets environment variable to be used at the time of static export by head.tsx.
* Using this from process.env allows targeting both serverless and SSR by calling
* `process.env.__NEXT_OPTIMIZE_FONTS`.
* TODO(prateekbh@): Remove this when experimental.optimizeFonts are being cleaned up.
*/
if (optimizeFonts) {
process.env.__NEXT_OPTIMIZE_FONTS = JSON.stringify(optimizeFonts)
}
if (optimizeCss) {
process.env.__NEXT_OPTIMIZE_CSS = JSON.stringify(true)
}
renderResult = await renderMethod(
req,
res,
page,
query,
// @ts-ignore
curRenderOpts
)
}
results.ssgNotFound = (curRenderOpts as any).isNotFound
const validateAmp = async (
......@@ -529,29 +428,14 @@ export default async function exportPage({
await promises.access(ampHtmlFilepath)
} catch (_) {
// make sure it doesn't exist from manual mapping
let ampRenderResult
if (serverless) {
req.url += (req.url!.includes('?') ? '&' : '?') + 'amp=1'
let ampRenderResult = await renderMethod(
req,
res,
page,
// @ts-ignore
ampRenderResult = (
await (renderMethod as any)(
req,
res,
'export',
curRenderOpts,
params
)
).html
} else {
ampRenderResult = await renderMethod(
req,
res,
page,
// @ts-ignore
{ ...query, amp: '1' },
curRenderOpts as any
)
}
{ ...query, amp: '1' },
curRenderOpts as any
)
const ampHtml = ampRenderResult
? ampRenderResult.toUnchunkedString()
......
......@@ -166,21 +166,6 @@ type ResponsePayload = {
revalidateOptions?: any
}
export function prepareServerlessUrl(
req: BaseNextRequest,
query: ParsedUrlQuery
): void {
const curUrl = parseUrl(req.url!, true)
req.url = formatUrl({
...curUrl,
search: undefined,
query: {
...curUrl.query,
...query,
},
})
}
export default abstract class Server<ServerOptions extends Options = Options> {
protected dir: string
protected quiet: boolean
......@@ -1000,10 +985,6 @@ export default abstract class Server<ServerOptions extends Options = Options> {
const is404Page = pathname === '/404'
const is500Page = pathname === '/500'
const isAppPath = components.isAppPath
const isLikeServerless =
typeof components.ComponentMod === 'object' &&
typeof (components.ComponentMod as any).renderReqToHTML === 'function'
const hasServerProps = !!components.getServerSideProps
let hasStaticPaths = !!components.getStaticPaths
......@@ -1157,11 +1138,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
// be static so we can collect revalidate and populate the
// cache if there are no dynamic data requirements
opts.supportsDynamicHTML =
!isSSG &&
!isLikeServerless &&
!isBotRequest &&
!query.amp &&
isSupportedDocument
!isSSG && !isBotRequest && !query.amp && isSupportedDocument
}
const defaultLocale = isSSG
......@@ -1285,88 +1262,65 @@ export default abstract class Server<ServerOptions extends Options = Options> {
let isNotFound: boolean | undefined
let isRedirect: boolean | undefined
// handle serverless
if (isLikeServerless) {
const renderResult = await (
components.ComponentMod as any
).renderReqToHTML(req, res, 'passthrough', {
locale,
locales,
defaultLocale,
optimizeFonts: this.renderOpts.optimizeFonts,
optimizeCss: this.renderOpts.optimizeCss,
nextScriptWorkers: this.renderOpts.nextScriptWorkers,
distDir: this.distDir,
fontManifest: this.renderOpts.fontManifest,
domainLocales: this.renderOpts.domainLocales,
})
body = renderResult.html
pageData = renderResult.renderOpts.pageData
sprRevalidate = renderResult.renderOpts.revalidate
isNotFound = renderResult.renderOpts.isNotFound
isRedirect = renderResult.renderOpts.isRedirect
} else {
const origQuery = parseUrl(req.url || '', true).query
// clear any dynamic route params so they aren't in
// the resolvedUrl
if (opts.params) {
Object.keys(opts.params).forEach((key) => {
delete origQuery[key]
})
}
const hadTrailingSlash =
urlPathname !== '/' && this.nextConfig.trailingSlash
const origQuery = parseUrl(req.url || '', true).query
const resolvedUrl = formatUrl({
pathname: `${resolvedUrlPathname}${hadTrailingSlash ? '/' : ''}`,
// make sure to only add query values from original URL
query: origQuery,
// clear any dynamic route params so they aren't in
// the resolvedUrl
if (opts.params) {
Object.keys(opts.params).forEach((key) => {
delete origQuery[key]
})
}
const hadTrailingSlash =
urlPathname !== '/' && this.nextConfig.trailingSlash
const renderOpts: RenderOpts = {
...components,
...opts,
isDataReq,
resolvedUrl,
locale,
locales,
defaultLocale,
// For getServerSideProps and getInitialProps we need to ensure we use the original URL
// and not the resolved URL to prevent a hydration mismatch on
// asPath
resolvedAsPath:
hasServerProps || hasGetInitialProps
? formatUrl({
// we use the original URL pathname less the _next/data prefix if
// present
pathname: `${urlPathname}${hadTrailingSlash ? '/' : ''}`,
query: origQuery,
})
: resolvedUrl,
}
if (isSSG || hasStaticPaths) {
renderOpts.supportsDynamicHTML = false
}
const resolvedUrl = formatUrl({
pathname: `${resolvedUrlPathname}${hadTrailingSlash ? '/' : ''}`,
// make sure to only add query values from original URL
query: origQuery,
})
const renderResult = await this.renderHTML(
req,
res,
pathname,
query,
renderOpts
)
const renderOpts: RenderOpts = {
...components,
...opts,
isDataReq,
resolvedUrl,
locale,
locales,
defaultLocale,
// For getServerSideProps and getInitialProps we need to ensure we use the original URL
// and not the resolved URL to prevent a hydration mismatch on
// asPath
resolvedAsPath:
hasServerProps || hasGetInitialProps
? formatUrl({
// we use the original URL pathname less the _next/data prefix if
// present
pathname: `${urlPathname}${hadTrailingSlash ? '/' : ''}`,
query: origQuery,
})
: resolvedUrl,
}
body = renderResult
// TODO: change this to a different passing mechanism
pageData = (renderOpts as any).pageData
sprRevalidate = (renderOpts as any).revalidate
isNotFound = (renderOpts as any).isNotFound
isRedirect = (renderOpts as any).isRedirect
if (isSSG || hasStaticPaths) {
renderOpts.supportsDynamicHTML = false
}
const renderResult = await this.renderHTML(
req,
res,
pathname,
query,
renderOpts
)
body = renderResult
// TODO: change this to a different passing mechanism
pageData = (renderOpts as any).pageData
sprRevalidate = (renderOpts as any).revalidate
isNotFound = (renderOpts as any).isNotFound
isRedirect = (renderOpts as any).isRedirect
let value: ResponseCacheValue | null
if (isNotFound) {
value = null
......@@ -1479,9 +1433,6 @@ export default abstract class Server<ServerOptions extends Options = Options> {
// We need to generate the fallback on-demand for development.
else {
query.__nextFallback = 'true'
if (isLikeServerless) {
prepareServerlessUrl(req, query)
}
const result = await doRender()
if (!result) {
return null
......
......@@ -22,13 +22,10 @@ import {
VALID_LOADERS,
} from '../shared/lib/image-config'
import { loadEnvConfig } from '@next/env'
import { hasNextSupport } from '../telemetry/ci-info'
import { gte as semverGte } from 'next/dist/compiled/semver'
export { DomainLocale, NextConfig, normalizeConfig } from './config-shared'
const targets = ['server', 'serverless', 'experimental-serverless-trace']
const experimentalWarning = execOnce(
(configFileName: string, features: string[]) => {
const s = features.length > 1 ? 's' : ''
......@@ -805,17 +802,9 @@ export default async function loadConfig(
)
}
if (userConfig.target && !targets.includes(userConfig.target)) {
throw new Error(
`Specified target is invalid. Provided: "${
userConfig.target
}" should be one of ${targets.join(', ')}`
)
}
if (userConfig.target && userConfig.target !== 'server') {
Log.warn(
'The `target` config is deprecated and will be removed in a future version.\n' +
throw new Error(
`The "target" property is no longer supported in ${configFileName}.\n` +
'See more info here https://nextjs.org/docs/messages/deprecated-target-config'
)
}
......@@ -829,10 +818,6 @@ export default async function loadConfig(
: canonicalBase) || ''
}
if (process.env.NEXT_PRIVATE_TARGET || hasNextSupport) {
userConfig.target = process.env.NEXT_PRIVATE_TARGET || 'server'
}
return assignDefaults({
configOrigin: relative(dir, path),
configFile: path,
......
......@@ -448,7 +448,6 @@ export default class HotReloader {
pagesDir: this.pagesDir,
previewMode: this.previewProps,
rootDir: this.dir,
target: 'server',
pageExtensions: this.config.pageExtensions,
})
)
......@@ -519,7 +518,6 @@ export default class HotReloader {
pagesDir: this.pagesDir,
previewMode: this.previewProps,
rootDir: this.dir,
target: 'server',
pageExtensions: this.config.pageExtensions,
})
).client,
......
......@@ -1297,7 +1297,6 @@ export default class DevServer extends Server {
const pathsResult = await this.getStaticPathsWorker().loadStaticPaths({
distDir: this.distDir,
pathname,
serverless: !this.renderOpts.dev && this._isLikeServerless,
config: {
configFileName,
publicRuntimeConfig,
......
......@@ -19,7 +19,6 @@ let workerWasUsed = false
export async function loadStaticPaths({
distDir,
pathname,
serverless,
config,
httpAgentOptions,
enableUndici,
......@@ -30,7 +29,6 @@ export async function loadStaticPaths({
}: {
distDir: string
pathname: string
serverless: boolean
config: RuntimeConfig
httpAgentOptions: NextConfigComplete['httpAgentOptions']
enableUndici: NextConfigComplete['enableUndici']
......@@ -59,7 +57,6 @@ export async function loadStaticPaths({
const components = await loadComponents({
distDir,
pathname: originalAppPath || pathname,
serverless,
hasServerComponents: false,
isAppPath: !!isAppPath,
})
......
......@@ -65,63 +65,24 @@ export async function loadDefaultErrorComponents(distDir: string) {
export async function loadComponents({
distDir,
pathname,
serverless,
hasServerComponents,
isAppPath,
}: {
distDir: string
pathname: string
serverless: boolean
hasServerComponents: boolean
isAppPath: boolean
}): Promise<LoadComponentsReturnType> {
if (serverless) {
const ComponentMod = await requirePage(pathname, distDir, serverless)
if (typeof ComponentMod === 'string') {
return {
Component: ComponentMod as any,
pageConfig: {},
ComponentMod,
} as LoadComponentsReturnType
}
let {
default: Component,
getStaticProps,
getStaticPaths,
getServerSideProps,
} = ComponentMod
Component = await Component
getStaticProps = await getStaticProps
getStaticPaths = await getStaticPaths
getServerSideProps = await getServerSideProps
const pageConfig = (await ComponentMod.config) || {}
return {
Component,
pageConfig,
getStaticProps,
getStaticPaths,
getServerSideProps,
ComponentMod,
} as LoadComponentsReturnType
}
let DocumentMod = {}
let AppMod = {}
if (!isAppPath) {
;[DocumentMod, AppMod] = await Promise.all([
Promise.resolve().then(() =>
requirePage('/_document', distDir, serverless, false)
),
Promise.resolve().then(() =>
requirePage('/_app', distDir, serverless, false)
),
Promise.resolve().then(() => requirePage('/_document', distDir, false)),
Promise.resolve().then(() => requirePage('/_app', distDir, false)),
])
}
const ComponentMod = await Promise.resolve().then(() =>
requirePage(pathname, distDir, serverless, isAppPath)
requirePage(pathname, distDir, isAppPath)
)
const [buildManifest, reactLoadableManifest, serverComponentManifest] =
......
......@@ -43,7 +43,6 @@ import {
CLIENT_PUBLIC_FILES_PATH,
APP_PATHS_MANIFEST,
FLIGHT_SERVER_CSS_MANIFEST,
SERVERLESS_DIRECTORY,
SERVER_DIRECTORY,
FONT_LOADER_MANIFEST,
} from '../shared/lib/constants'
......@@ -72,7 +71,6 @@ import loadRequireHook from '../build/webpack/require-hook'
import BaseServer, {
Options,
FindComponentsResult,
prepareServerlessUrl,
MiddlewareRoutingItem,
RoutingItem,
NoFallbackError,
......@@ -97,7 +95,7 @@ import { removeTrailingSlash } from '../shared/lib/router/utils/remove-trailing-
import { getNextPathnameInfo } from '../shared/lib/router/utils/get-next-pathname-info'
import { getClonableBody } from './body-streams'
import { checkIsManualRevalidate } from './api-utils'
import { shouldUseReactRoot, isTargetLikeServerless } from './utils'
import { shouldUseReactRoot } from './utils'
import ResponseCache from './response-cache'
import { IncrementalCache } from './lib/incremental-cache'
import { normalizeAppPath } from '../shared/lib/router/utils/app-paths'
......@@ -252,24 +250,21 @@ export default class NextNodeServer extends BaseServer {
loadComponents({
distDir: this.distDir,
pathname: '/_document',
serverless: this._isLikeServerless,
hasServerComponents: false,
isAppPath: false,
}).catch(() => {})
loadComponents({
distDir: this.distDir,
pathname: '/_app',
serverless: this._isLikeServerless,
hasServerComponents: false,
isAppPath: false,
}).catch(() => {})
}
}
private compression =
this.nextConfig.compress && this.nextConfig.target === 'server'
? (compression() as ExpressMiddleware)
: undefined
private compression = this.nextConfig.compress
? (compression() as ExpressMiddleware)
: undefined
protected loadEnvConfig({
dev,
......@@ -785,14 +780,6 @@ export default class NextNodeServer extends BaseServer {
delete query.__nextLocale
delete query.__nextDefaultLocale
if (!this.renderOpts.dev && this._isLikeServerless) {
if (typeof pageModule.default === 'function') {
prepareServerlessUrl(req, query)
await pageModule.default(req, res)
return true
}
}
await apiResolver(
(req as NodeNextRequest).originalRequest,
(res as NodeNextResponse).originalResponse,
......@@ -882,14 +869,7 @@ export default class NextNodeServer extends BaseServer {
}
protected getPagePath(pathname: string, locales?: string[]): string {
return getPagePath(
pathname,
this.distDir,
this._isLikeServerless,
this.renderOpts.dev,
locales,
this.hasAppDir
)
return getPagePath(pathname, this.distDir, locales, this.hasAppDir)
}
protected async renderPageComponent(
......@@ -958,7 +938,6 @@ export default class NextNodeServer extends BaseServer {
const components = await loadComponents({
distDir: this.distDir,
pathname: pagePath,
serverless: !this.renderOpts.dev && this._isLikeServerless,
hasServerComponents: !!this.renderOpts.serverComponents,
isAppPath,
})
......@@ -1000,7 +979,7 @@ export default class NextNodeServer extends BaseServer {
}
protected getFontManifest(): FontManifest {
return requireFontManifest(this.distDir, this._isLikeServerless)
return requireFontManifest(this.distDir)
}
protected getServerComponentManifest() {
......@@ -2156,14 +2135,7 @@ export default class NextNodeServer extends BaseServer {
return result
}
protected get _isLikeServerless(): boolean {
return isTargetLikeServerless(this.nextConfig.target)
}
protected get serverDistDir() {
return join(
this.distDir,
this._isLikeServerless ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY
)
return join(this.distDir, SERVER_DIRECTORY)
}
}
......@@ -4,7 +4,6 @@ import {
FONT_MANIFEST,
PAGES_MANIFEST,
SERVER_DIRECTORY,
SERVERLESS_DIRECTORY,
APP_PATHS_MANIFEST,
} from '../shared/lib/constants'
import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
......@@ -16,15 +15,10 @@ import { PageNotFoundError, MissingStaticPage } from '../shared/lib/utils'
export function getPagePath(
page: string,
distDir: string,
serverless: boolean,
dev?: boolean,
locales?: string[],
appDirEnabled?: boolean
): string {
const serverBuildPath = join(
distDir,
serverless && !dev ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY
)
const serverBuildPath = join(distDir, SERVER_DIRECTORY)
let appPathsManifest: undefined | PagesManifest
if (appDirEnabled) {
......@@ -75,17 +69,9 @@ export function getPagePath(
export function requirePage(
page: string,
distDir: string,
serverless: boolean,
appDirEnabled?: boolean
): any {
const pagePath = getPagePath(
page,
distDir,
serverless,
false,
undefined,
appDirEnabled
)
const pagePath = getPagePath(page, distDir, undefined, appDirEnabled)
if (pagePath.endsWith('.html')) {
return promises.readFile(pagePath, 'utf8').catch((err) => {
throw new MissingStaticPage(page, err.message)
......@@ -94,11 +80,8 @@ export function requirePage(
return require(pagePath)
}
export function requireFontManifest(distDir: string, serverless: boolean) {
const serverBuildPath = join(
distDir,
serverless ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY
)
export function requireFontManifest(distDir: string) {
const serverBuildPath = join(distDir, SERVER_DIRECTORY)
const fontManifest = require(join(serverBuildPath, FONT_MANIFEST))
return fontManifest
}
......@@ -16,11 +16,5 @@ export function cleanAmpPath(pathname: string): string {
return pathname
}
export function isTargetLikeServerless(target: string) {
const isServerless = target === 'serverless'
const isServerlessTrace = target === 'experimental-serverless-trace'
return isServerless || isServerlessTrace
}
// When react version is >= 18 opt-in using reactRoot
export const shouldUseReactRoot = parseInt(React.version) >= 18
......@@ -40,7 +40,6 @@ export const DEV_MIDDLEWARE_MANIFEST = '_devMiddlewareManifest.json'
export const REACT_LOADABLE_MANIFEST = 'react-loadable-manifest.json'
export const FONT_MANIFEST = 'font-manifest.json'
export const SERVER_DIRECTORY = 'server'
export const SERVERLESS_DIRECTORY = 'serverless'
export const CONFIG_FILES = ['next.config.js', 'next.config.mjs']
export const BUILD_ID_FILE = 'BUILD_ID'
export const BLOCKED_PAGES = ['/_document', '/_app', '/_error']
......
......@@ -7,8 +7,6 @@ const glob = require('glob')
// eslint-disable-next-line import/no-extraneous-dependencies
const fs = require('fs-extra')
// eslint-disable-next-line import/no-extraneous-dependencies
const escapeRegex = require('escape-string-regexp')
// eslint-disable-next-line import/no-extraneous-dependencies
const resolveFrom = require('resolve-from')
export async function next__polyfill_nomodule(task, opts) {
......@@ -98,26 +96,6 @@ export async function ncc_node_html_parser(task, opts) {
)
.ncc({ packageName: 'node-html-parser', externals, target: 'es5' })
.target('compiled/node-html-parser')
const filePath = join(__dirname, 'compiled/node-html-parser/index.js')
const content = fs.readFileSync(filePath, 'utf8')
// remove AMD define branch as this forces the module to not
// be treated as commonjs in serverless mode
// TODO: this can be removed after serverless target is removed
fs.writeFileSync(
filePath,
content.replace(
new RegExp(
escapeRegex(
'if(typeof define=="function"&&typeof define.amd=="object"&&define.amd){define((function(){return '
) +
'\\w' +
escapeRegex('}))}else '),
'g'
),
''
)
)
}
// eslint-disable-next-line camelcase
......
......@@ -91,7 +91,7 @@ export function eventCliSession(
hasNowJson: event.hasNowJson,
isCustomServer: event.isCustomServer,
hasNextConfig: nextConfig.configOrigin !== 'default',
buildTarget: nextConfig.target === 'server' ? 'default' : nextConfig.target,
buildTarget: 'default',
hasWebpackConfig: typeof nextConfig?.webpack === 'function',
hasBabelConfig: hasBabelConfig(dir),
imageEnabled: !!images,
......
......@@ -14,7 +14,6 @@ import {
} from 'next-test-utils'
const appDir = join(__dirname, '../')
const nextConfig = join(appDir, 'next.config.js')
let appPort
let app
......@@ -71,33 +70,6 @@ describe('Default 404 Page with custom _error', () => {
runTests('server')
})
describe.skip('serverless mode', () => {
afterAll(async () => {
await fs.remove(nextConfig)
await killApp(app)
})
it('should build successfully', async () => {
await fs.writeFile(
nextConfig,
`
module.exports = { target: 'experimental-serverless-trace' }
`
)
const { code } = await nextBuild(appDir, [], {
stderr: true,
stdout: true,
})
expect(code).toBe(0)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
runTests('serverless')
})
describe('dev mode', () => {
beforeAll(async () => {
appPort = await findPort()
......
......@@ -13,11 +13,9 @@ import {
} from 'next-test-utils'
const appDir = join(__dirname, '../')
const nextConfig = join(appDir, 'next.config.js')
const gip404Err =
/`pages\/404` can not have getInitialProps\/getServerSideProps/
let nextConfigContent
let stdout
let stderr
let buildId
......@@ -82,7 +80,6 @@ describe('404 Page Support SSG', () => {
afterAll(() => killApp(app))
it('should build successfully', async () => {
nextConfigContent = await fs.readFile(nextConfig, 'utf8')
const {
code,
stderr: buildStderr,
......@@ -114,50 +111,6 @@ describe('404 Page Support SSG', () => {
runTests()
})
describe.skip('serverless mode', () => {
afterAll(async () => {
await fs.writeFile(nextConfig, nextConfigContent)
await killApp(app)
})
it('should build successfully', async () => {
nextConfigContent = await fs.readFile(nextConfig, 'utf8')
await fs.writeFile(
nextConfig,
`
module.exports = { target: 'experimental-serverless-trace' }
`
)
const {
code,
stderr: buildStderr,
stdout: buildStdout,
} = await nextBuild(appDir, [], {
stderr: true,
stdout: true,
})
expect(code).toBe(0)
expect(buildStderr).not.toMatch(gip404Err)
expect(buildStdout).not.toMatch(gip404Err)
appPort = await findPort()
stderr = ''
stdout = ''
app = await nextStart(appDir, appPort, {
onStdout(msg) {
stdout += msg
},
onStderr(msg) {
stderr += msg
},
})
buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8')
})
runTests()
})
describe('dev mode', () => {
beforeAll(async () => {
appPort = await findPort()
......
......@@ -16,11 +16,9 @@ import {
const appDir = join(__dirname, '../')
const pages404 = join(appDir, 'pages/404.js')
const nextConfig = join(appDir, 'next.config.js')
const gip404Err =
/`pages\/404` can not have getInitialProps\/getServerSideProps/
let nextConfigContent
let appPort
let app
......@@ -92,29 +90,6 @@ describe('404 Page Support', () => {
runTests('server')
})
describe.skip('serverless mode', () => {
beforeAll(async () => {
nextConfigContent = await fs.readFile(nextConfig, 'utf8')
await fs.writeFile(
nextConfig,
`
module.exports = {
target: 'serverless'
}
`
)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await fs.writeFile(nextConfig, nextConfigContent)
await killApp(app)
})
runTests('serverless')
})
it('should not cache for custom 404 page with gssp and revalidate disabled', async () => {
await fs.move(pages404, `${pages404}.bak`)
await fs.writeFile(
......
......@@ -21,11 +21,9 @@ const appDir = join(__dirname, '../')
const pages500 = join(appDir, 'pages/500.js')
const pagesApp = join(appDir, 'pages/_app.js')
const pagesError = join(appDir, 'pages/_error.js')
const nextConfig = join(appDir, 'next.config.js')
const gip500Err =
/`pages\/500` can not have getInitialProps\/getServerSideProps/
let nextConfigContent
let appPort
let app
......@@ -85,30 +83,6 @@ describe('500 Page Support', () => {
runTests('server')
})
describe.skip('serverless mode', () => {
beforeAll(async () => {
nextConfigContent = await fs.readFile(nextConfig, 'utf8')
await fs.writeFile(
nextConfig,
`
module.exports = {
target: 'serverless'
}
`
)
await fs.remove(join(appDir, '.next'))
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await fs.writeFile(nextConfig, nextConfigContent)
await killApp(app)
})
runTests('serverless')
})
it('does not build 500 statically with getInitialProps in _app', async () => {
await fs.writeFile(
pagesApp,
......
......@@ -15,7 +15,6 @@ import {
} from 'next-test-utils'
const appDir = join(__dirname, '../')
const nextConfig = join(appDir, 'next.config.js')
let builtServerPagesDir
let appPort
let app
......@@ -99,27 +98,6 @@ const runTests = (isDev = false) => {
}
describe('AMP SSG Support', () => {
describe.skip('serverless mode', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfig,
`
module.exports = {
target: 'experimental-serverless-trace'
}
`
)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
builtServerPagesDir = join(appDir, '.next/serverless/pages')
})
afterAll(async () => {
await fs.remove(nextConfig)
await killApp(app)
})
runTests()
})
describe('server mode', () => {
beforeAll(async () => {
await nextBuild(appDir)
......
/* eslint-env jest */
import fs from 'fs-extra'
import { join } from 'path'
import {
killApp,
......@@ -11,7 +10,6 @@ import {
} from 'next-test-utils'
const appDir = join(__dirname, '../')
const nextConfig = join(appDir, 'next.config.js')
let appPort
let app
......@@ -72,22 +70,4 @@ describe('API routes', () => {
runTests()
})
describe.skip('Serverless support', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfig,
`module.exports = { target: 'serverless' }`
)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
await fs.remove(nextConfig)
})
runTests()
})
})
......@@ -19,7 +19,6 @@ import {
import json from '../big.json'
const appDir = join(__dirname, '../')
const nextConfig = join(appDir, 'next.config.js')
let appPort
let stderr
let mode
......@@ -619,23 +618,4 @@ describe('API routes', () => {
runTests()
})
describe.skip('Serverless support', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfig,
`module.exports = { target: 'serverless' }`
)
await nextBuild(appDir)
mode = 'serverless'
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
await fs.remove(nextConfig)
})
runTests()
})
})
/* eslint-env jest */
import path from 'path'
import fs from 'fs-extra'
import webdriver from 'next-webdriver'
import {
nextBuild,
......@@ -14,7 +13,6 @@ import {
} from 'next-test-utils'
const appDir = path.join(__dirname, '..')
const nextConfig = path.join(appDir, 'next.config.js')
let appPort
let app
......@@ -68,21 +66,4 @@ describe('AppTree', () => {
afterAll(() => killApp(app))
runTests()
})
describe.skip('serverless mode', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfig,
`module.exports = { target: 'serverless' }`
)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
await fs.remove(nextConfig)
})
runTests()
})
})
module.exports = {
// target: 'experimental-serverless-trace',
webpack: (config) => {
config.experiments = config.experiments || {}
config.experiments.topLevelAwait = true
......
......@@ -11,7 +11,6 @@ import {
launchApp,
nextBuild,
nextStart,
File,
check,
} from 'next-test-utils'
import { join } from 'path'
......@@ -19,7 +18,6 @@ import { join } from 'path'
let app
let appPort
const appDir = join(__dirname, '../')
const nextConfig = new File(join(appDir, 'next.config.js'))
function runTests(dev = false) {
it('ssr async page modules', async () => {
......@@ -130,19 +128,4 @@ describe('Async modules', () => {
runTests()
})
describe.skip('serverless mode', () => {
beforeAll(async () => {
nextConfig.replace('// target:', 'target:')
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await nextConfig.restore()
await killApp(app)
})
runTests()
})
})
/* eslint-env jest */
import path from 'path'
import fs from 'fs-extra'
import { nextBuild } from 'next-test-utils'
const appDir = path.join(__dirname, '..')
const nextConfig = path.join(appDir, 'next.config.js')
const runTests = () => {
it('should not opt-out of auto static optimization from invalid _error', async () => {
......@@ -35,20 +33,4 @@ describe('Auto Export _error bail', () => {
describe('server mode', () => {
runTests()
})
describe.skip('serverless mode', () => {
beforeAll(() =>
fs.writeFile(
nextConfig,
`
module.exports = {
target: 'experimental-serverless-trace'
}
`
)
)
afterAll(() => fs.remove(nextConfig))
runTests()
})
})
module.exports = {
// target: 'serverless',
exportPathMap() {
return {
'/': { page: '/hello', query: { first: 'second' } },
......
/* eslint-env jest */
import path from 'path'
import fs from 'fs-extra'
import { nextBuild, nextExport } from 'next-test-utils'
const appDir = path.join(__dirname, '..')
const outdir = path.join(__dirname, 'out')
const nextConfig = path.join(appDir, 'next.config.js')
let stderr
let exitCode
......@@ -23,8 +21,6 @@ const runTests = () => {
})
}
let origNextConfig
describe('Auto Export', () => {
describe('server mode', () => {
beforeAll(async () => {
......@@ -40,23 +36,4 @@ describe('Auto Export', () => {
runTests()
})
describe.skip('serverless mode', () => {
beforeAll(async () => {
origNextConfig = await fs.readFile(nextConfig, 'utf8')
await nextBuild(appDir)
const { stderr: curStderr, code: curCode } = await nextExport(
appDir,
{ outdir },
{ stderr: true }
)
stderr = curStderr
exitCode = curCode
})
afterAll(async () => {
await fs.writeFile(nextConfig, origNextConfig)
})
runTests()
})
})
// eslint-disable-next-line
export default () => something.error
/* eslint-env jest */
import fs from 'fs'
import path from 'path'
import { nextBuild } from 'next-test-utils'
const appDir = path.join(__dirname, '..')
describe.skip('Auto Export Error Serverless', () => {
it('fails to emit the page', async () => {
const { stderr } = await nextBuild(appDir, [], {
stderr: true,
})
expect(
fs.existsSync(path.join(appDir, '.next/serverless/pages/index.html'))
).toBe(false)
expect(stderr).toContain('ReferenceError')
expect(stderr).toContain('Build error occurred')
})
})
import { useRouter } from 'next/router'
export default () => {
const { query } = useRouter()
return <p>post: {query.post}</p>
}
/* eslint-env jest */
import webdriver from 'next-webdriver'
import path from 'path'
import { nextBuild, nextStart, findPort, killApp } from 'next-test-utils'
const appDir = path.join(__dirname, '..')
let appPort
let app
describe.skip('Auto Export Serverless', () => {
it('Refreshes query on mount', async () => {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
const browser = await webdriver(appPort, '/post-1')
const html = await browser.eval('document.body.innerHTML')
expect(html).toMatch(/post.*post-1/)
expect(html).toMatch(/nextExport/)
await killApp(app)
await browser.close()
})
})
module.exports = {
// target: 'experimental-serverless-trace',
basePath: '/docs',
}
......@@ -3,7 +3,6 @@ import {
findPort,
killApp,
nextStart,
File,
launchApp,
} from 'next-test-utils'
import webdriver from 'next-webdriver'
......@@ -15,7 +14,6 @@ let app
let appPort
let buildId
const appDir = join(__dirname, '..')
const nextConfig = new File(join(appDir, 'next.config.js'))
const runTests = () => {
it('should use correct data URL for root catch-all', async () => {
......@@ -50,18 +48,3 @@ describe('production mode', () => {
afterAll(() => killApp(app))
runTests()
})
describe.skip('serverless mode', () => {
beforeAll(async () => {
nextConfig.replace('// target', 'target')
await nextBuild(appDir)
buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8')
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
nextConfig.restore()
await killApp(app)
})
runTests()
})
......@@ -54,22 +54,4 @@ describe('bigint API route support', () => {
runTests()
})
describe.skip('serverless mode', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfig,
`module.exports = { target: 'serverless' }`
)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
await fs.remove(nextConfig)
})
runTests()
})
})
/* eslint-env jest */
import { join } from 'path'
import fs from 'fs-extra'
import {
renderViaHTTP,
nextBuild,
......@@ -11,12 +10,9 @@ import {
} from 'next-test-utils'
const appDir = join(__dirname, '../')
const nextConfig = join(appDir, 'next.config.js')
const errorRegex = /getStaticPaths was added without a getStaticProps in/
describe('Catches Missing getStaticProps', () => {
afterAll(() => fs.remove(nextConfig))
it('should catch it in dev mode', async () => {
const appPort = await findPort()
const app = await launchApp(appDir, appPort)
......@@ -27,19 +23,6 @@ describe('Catches Missing getStaticProps', () => {
})
it('should catch it in server build mode', async () => {
await fs.remove(nextConfig)
const { stderr } = await nextBuild(appDir, [], {
stderr: true,
})
expect(stderr).toMatch(errorRegex)
})
it('should catch it in serverless mode', async () => {
await fs.writeFile(
nextConfig,
`module.exports = { target: 'serverless' }`,
'utf8'
)
const { stderr } = await nextBuild(appDir, [], {
stderr: true,
})
......
......@@ -33,25 +33,6 @@ describe('Cleaning distDir', () => {
runTests()
})
describe.skip('serverless mode', () => {
beforeAll(async () => {
nextConfigContent = await fs.readFile(nextConfig, 'utf8')
await fs.writeFile(
nextConfig,
`
module.exports = {
target: 'serverless'
}
`
)
})
afterAll(async () => {
await fs.writeFile(nextConfig, nextConfigContent)
})
runTests()
})
describe('disabled write', () => {
beforeAll(async () => {
nextConfigContent = await fs.readFile(nextConfig, 'utf8')
......
......@@ -19,7 +19,6 @@ describe('Config Experimental Warning', () => {
configFile.write(`
module.exports = (phase, { defaultConfig }) => {
return {
target: 'server',
...defaultConfig,
}
}
......@@ -32,7 +31,7 @@ describe('Config Experimental Warning', () => {
it('should not show warning with config from object', async () => {
configFile.write(`
module.exports = {
target: 'server'
images: {},
}
`)
const { stderr } = await nextBuild(appDir, [], { stderr: true })
......@@ -42,7 +41,6 @@ describe('Config Experimental Warning', () => {
it('should show warning with config from object with experimental', async () => {
configFile.write(`
module.exports = {
target: 'server',
experimental: {
workerThreads: true
}
......@@ -57,7 +55,6 @@ describe('Config Experimental Warning', () => {
it('should show warning with config from function with experimental', async () => {
configFile.write(`
module.exports = (phase) => ({
target: 'server',
experimental: {
workerThreads: true
}
......@@ -72,7 +69,6 @@ describe('Config Experimental Warning', () => {
it('should not show warning with default value', async () => {
configFile.write(`
module.exports = (phase) => ({
target: 'server',
experimental: {
workerThreads: false
}
......
......@@ -77,21 +77,3 @@ describe('CSS optimization for SSR apps', () => {
})
runTests()
})
describe.skip('Font optimization for emulated serverless apps', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfig,
`module.exports = { target: 'experimental-serverless-trace', experimental: {optimizeCss: true} }`,
'utf8'
)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
await fs.remove(nextConfig)
})
runTests()
})
......@@ -13,7 +13,6 @@ import {
const appDir = join(__dirname, '..')
const page404 = join(appDir, 'pages/404.js')
const nextConfig = join(appDir, 'next.config.js')
let appPort
let app
......@@ -102,22 +101,4 @@ describe('Custom _error', () => {
runTests()
})
describe.skip('serverless mode', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfig,
`module.exports = { target: 'serverless' }`
)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
await fs.remove(nextConfig)
})
runTests()
})
})
module.exports = {
target: 'server',
pageExtensions: ['page.js'],
}
......@@ -8,7 +8,6 @@ import {
launchApp,
killApp,
renderViaHTTP,
File,
} from 'next-test-utils'
const appDir = join(__dirname, '..')
......@@ -46,19 +45,4 @@ describe('Custom page extension', () => {
afterAll(() => killApp(app))
runTests()
})
describe.skip('serverless mode', () => {
const nextConfig = new File(join(appDir, 'next.config.js'))
beforeAll(async () => {
nextConfig.replace('server', 'serverless')
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
nextConfig.restore()
})
runTests()
})
})
module.exports = {
// target: 'serverless',
async rewrites() {
// no-rewrites comment
return {
......
......@@ -20,7 +20,6 @@ import {
getBrowserBodyText,
waitFor,
normalizeRegEx,
initNextServerScript,
nextExport,
hasRedbox,
check,
......@@ -2426,96 +2425,6 @@ describe('Custom routes', () => {
})
})
describe.skip('serverless mode', () => {
beforeAll(async () => {
nextConfigContent = await fs.readFile(nextConfigPath, 'utf8')
await fs.writeFile(
nextConfigPath,
nextConfigContent.replace(/\/\/ target/, 'target'),
'utf8'
)
const { stdout: buildStdout } = await nextBuild(appDir, ['-d'], {
stdout: true,
})
stdout = buildStdout
appPort = await findPort()
app = await nextStart(appDir, appPort, {
onStdout: (msg) => {
stdout += msg
},
})
buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8')
})
afterAll(async () => {
await fs.writeFile(nextConfigPath, nextConfigContent, 'utf8')
await killApp(app)
})
runTests()
})
describe.skip('raw serverless mode', () => {
beforeAll(async () => {
nextConfigContent = await fs.readFile(nextConfigPath, 'utf8')
await fs.writeFile(
nextConfigPath,
nextConfigContent.replace(/\/\/ target/, 'target'),
'utf8'
)
await nextBuild(appDir)
appPort = await findPort()
app = await initNextServerScript(join(appDir, 'server.js'), /ready on/, {
...process.env,
PORT: appPort,
})
})
afterAll(async () => {
await fs.writeFile(nextConfigPath, nextConfigContent, 'utf8')
await killApp(app)
})
it('should apply rewrites in lambda correctly for page route', async () => {
const html = await renderViaHTTP(appPort, '/query-rewrite/first/second')
const data = JSON.parse(cheerio.load(html)('p').text())
expect(data).toEqual({
first: 'first',
second: 'second',
section: 'first',
name: 'second',
})
})
it('should apply rewrites in lambda correctly for dynamic route', async () => {
const html = await renderViaHTTP(appPort, '/blog/post-1')
expect(html).toContain('post-2')
})
it('should apply rewrites in lambda correctly for API route', async () => {
const data = JSON.parse(
await renderViaHTTP(appPort, '/api-hello-param/first')
)
expect(data).toEqual({
query: {
name: 'first',
hello: 'first',
},
})
})
it('should apply rewrites in lambda correctly for dynamic API route', async () => {
const data = JSON.parse(
await renderViaHTTP(appPort, '/api-dynamic-param/first')
)
expect(data).toEqual({
query: {
slug: 'first',
hello: 'first',
},
})
})
})
describe('should load custom routes when only one type is used', () => {
const runSoloTests = (isDev) => {
const buildAndStart = async () => {
......
......@@ -81,29 +81,4 @@ describe('Dynamic Optional Routing Root Fallback', () => {
runTests()
})
describe.skip('serverless mode', () => {
let origNextConfig
beforeAll(async () => {
await fs.remove(join(appDir, '.next'))
origNextConfig = await fs.readFile(nextConfig, 'utf8')
await fs.writeFile(
nextConfig,
`module.exports = { target: 'serverless' }`
)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await fs.writeFile(nextConfig, origNextConfig)
await killApp(app)
})
runTests()
})
})
......@@ -65,27 +65,4 @@ describe('Dynamic Optional Routing', () => {
runTests()
})
describe.skip('serverless mode', () => {
let origNextConfig
beforeAll(async () => {
origNextConfig = await fs.readFile(nextConfig, 'utf8')
await fs.writeFile(
nextConfig,
`module.exports = { target: 'serverless' }`
)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await fs.writeFile(nextConfig, origNextConfig)
await killApp(app)
})
runTests()
})
})
......@@ -11,7 +11,6 @@ import {
nextStart,
renderViaHTTP,
check,
initNextServerScript,
} from 'next-test-utils'
import { join } from 'path'
......@@ -313,133 +312,4 @@ describe('Dynamic Optional Routing', () => {
}
})
})
describe.skip('serverless mode', () => {
let origNextConfig
beforeAll(async () => {
origNextConfig = await fs.readFile(nextConfig, 'utf8')
await fs.writeFile(
nextConfig,
`module.exports = { target: 'experimental-serverless-trace' }`
)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await fs.writeFile(nextConfig, origNextConfig)
await killApp(app)
})
runTests()
})
describe.skip('raw serverless mode', () => {
let origNextConfig
beforeAll(async () => {
origNextConfig = await fs.readFile(nextConfig, 'utf8')
await fs.writeFile(
nextConfig,
`module.exports = { target: 'experimental-serverless-trace' }`
)
await nextBuild(appDir)
appPort = await findPort()
app = await initNextServerScript(join(appDir, 'server.js'), /ready on/, {
...process.env,
PORT: appPort,
})
})
afterAll(async () => {
await fs.writeFile(nextConfig, origNextConfig)
await killApp(app)
})
const render = (path, query) => {
return fetchViaHTTP(appPort, path, query, {
headers: {
// force relying on query values
'x-vercel-id': 'hi',
},
}).then((res) => res.text())
}
it('should render normal (non-dynamic) page', async () => {
const html = await render('/about')
const $ = cheerio.load(html)
expect($('#content').text()).toBe('about')
})
it('should render top level optional catch-all root', async () => {
const html = await render('/', { optionalName: '' })
const $ = cheerio.load(html)
expect($('#route').text()).toBe('top level route param: undefined')
expect($('#keys').text()).toBe('[]')
expect($('#asPath').text()).toBe('/')
})
it('should render top level optional catch-all one level', async () => {
const html = await render('/hello', { optionalName: 'hello' })
const $ = cheerio.load(html)
expect($('#route').text()).toBe('top level route param: [hello]')
expect($('#keys').text()).toBe('["optionalName"]')
expect($('#asPath').text()).toBe('/hello')
})
it('should render top level optional catch-all two levels', async () => {
const html = await render('/hello/world', { optionalName: 'hello/world' })
const $ = cheerio.load(html)
expect($('#route').text()).toBe('top level route param: [hello|world]')
expect($('#keys').text()).toBe('["optionalName"]')
expect($('#asPath').text()).toBe('/hello/world')
})
it('should render nested optional catch-all root', async () => {
const html = await render('/nested', { optionalName: '' })
const $ = cheerio.load(html)
expect($('#route').text()).toBe('nested route param: undefined')
expect($('#keys').text()).toBe('[]')
expect($('#asPath').text()).toBe('/nested')
})
it('should render nested optional catch-all one level', async () => {
const html = await render('/nested/hello', { optionalName: 'hello' })
const $ = cheerio.load(html)
expect($('#route').text()).toBe('nested route param: [hello]')
expect($('#keys').text()).toBe('["optionalName"]')
expect($('#asPath').text()).toBe('/nested/hello')
})
it('should render nested optional catch-all two levels', async () => {
const html = await render('/nested/hello/world', {
optionalName: 'hello/world',
})
const $ = cheerio.load(html)
expect($('#route').text()).toBe('nested route param: [hello|world]')
expect($('#keys').text()).toBe('["optionalName"]')
expect($('#asPath').text()).toBe('/nested/hello/world')
})
it('should render optional catch-all api root', async () => {
const text = await render('/api/post', { slug: '' })
const data = JSON.parse(text)
expect(data).toEqual({})
})
it('should render optional catch-all api root one level', async () => {
const text = await render('/api/post/hello', { slug: 'hello' })
const data = JSON.parse(text)
expect(data).toEqual({ slug: ['hello'] })
})
it('should render optional catch-all api root two levels', async () => {
const text = await render('/api/post/hello', { slug: 'hello/world' })
const data = JSON.parse(text)
expect(data).toEqual({ slug: ['hello', 'world'] })
})
})
})
......@@ -27,7 +27,7 @@ let buildId
const appDir = join(__dirname, '../')
const buildIdPath = join(appDir, '.next/BUILD_ID')
function runTests({ dev, serverless }) {
function runTests({ dev }) {
if (!dev) {
it('should have correct cache entries on prefetch', async () => {
const browser = await webdriver(appPort, '/')
......@@ -1429,42 +1429,40 @@ function runTests({ dev, serverless }) {
})
})
if (!serverless) {
it('should output a pages-manifest correctly', async () => {
const manifest = await fs.readJson(
join(appDir, '.next/server/pages-manifest.json')
)
it('should output a pages-manifest correctly', async () => {
const manifest = await fs.readJson(
join(appDir, '.next/server/pages-manifest.json')
)
expect(manifest).toEqual({
'/[name]/[comment]': 'pages/[name]/[comment].js',
'/[name]/comments': 'pages/[name]/comments.js',
'/[name]': 'pages/[name].js',
'/[name]/on-mount-redir': 'pages/[name]/on-mount-redir.html',
'/another': 'pages/another.html',
'/b/[123]': 'pages/b/[123].js',
'/blog/[name]/comment/[id]': 'pages/blog/[name]/comment/[id].js',
'/c/[alongparamnameshouldbeallowedeventhoughweird]':
'pages/c/[alongparamnameshouldbeallowedeventhoughweird].js',
'/catchall-dash/[...hello-world]':
'pages/catchall-dash/[...hello-world].html',
'/d/[id]': 'pages/d/[id].html',
'/dash/[hello-world]': 'pages/dash/[hello-world].html',
'/': 'pages/index.html',
'/index/[...slug]': 'pages/index/[...slug].html',
'/on-mount/[post]': 'pages/on-mount/[post].html',
'/p1/p2/all-ssg/[...rest]': 'pages/p1/p2/all-ssg/[...rest].js',
'/p1/p2/all-ssr/[...rest]': 'pages/p1/p2/all-ssr/[...rest].js',
'/p1/p2/nested-all-ssg/[...rest]':
'pages/p1/p2/nested-all-ssg/[...rest].js',
'/p1/p2/predefined-ssg/[...rest]':
'pages/p1/p2/predefined-ssg/[...rest].js',
'/_app': 'pages/_app.js',
'/_error': 'pages/_error.js',
'/_document': 'pages/_document.js',
'/404': 'pages/404.html',
})
expect(manifest).toEqual({
'/[name]/[comment]': 'pages/[name]/[comment].js',
'/[name]/comments': 'pages/[name]/comments.js',
'/[name]': 'pages/[name].js',
'/[name]/on-mount-redir': 'pages/[name]/on-mount-redir.html',
'/another': 'pages/another.html',
'/b/[123]': 'pages/b/[123].js',
'/blog/[name]/comment/[id]': 'pages/blog/[name]/comment/[id].js',
'/c/[alongparamnameshouldbeallowedeventhoughweird]':
'pages/c/[alongparamnameshouldbeallowedeventhoughweird].js',
'/catchall-dash/[...hello-world]':
'pages/catchall-dash/[...hello-world].html',
'/d/[id]': 'pages/d/[id].html',
'/dash/[hello-world]': 'pages/dash/[hello-world].html',
'/': 'pages/index.html',
'/index/[...slug]': 'pages/index/[...slug].html',
'/on-mount/[post]': 'pages/on-mount/[post].html',
'/p1/p2/all-ssg/[...rest]': 'pages/p1/p2/all-ssg/[...rest].js',
'/p1/p2/all-ssr/[...rest]': 'pages/p1/p2/all-ssr/[...rest].js',
'/p1/p2/nested-all-ssg/[...rest]':
'pages/p1/p2/nested-all-ssg/[...rest].js',
'/p1/p2/predefined-ssg/[...rest]':
'pages/p1/p2/predefined-ssg/[...rest].js',
'/_app': 'pages/_app.js',
'/_error': 'pages/_error.js',
'/_document': 'pages/_document.js',
'/404': 'pages/404.html',
})
}
})
}
}
......@@ -1498,7 +1496,7 @@ describe('Dynamic Routing', () => {
})
afterAll(() => killApp(app))
runTests({ dev: true, serverless: false })
runTests({ dev: true })
})
describe('production mode', () => {
......@@ -1513,6 +1511,6 @@ describe('Dynamic Routing', () => {
})
afterAll(() => killApp(app))
runTests({ dev: false, serverless: false })
runTests({ dev: false })
})
})
......@@ -384,60 +384,4 @@ describe('Env Config', () => {
runTests('server')
})
describe.skip('serverless mode', () => {
let nextConfigContent = ''
const nextConfigPath = join(appDir, 'next.config.js')
const envFiles = [
'.env',
'.env.development',
'.env.development.local',
'.env.local',
'.env.production',
'.env.production.local',
'.env.test',
'.env.test.local',
].map((file) => join(appDir, file))
beforeAll(async () => {
nextConfigContent = await fs.readFile(nextConfigPath, 'utf8')
await fs.writeFile(
nextConfigPath,
nextConfigContent.replace(
'// update me',
`target: 'experimental-serverless-trace',`
)
)
const { code } = await nextBuild(appDir, [], {
env: {
PROCESS_ENV_KEY: 'processenvironment',
ENV_FILE_PROCESS_ENV: 'env-cli',
},
})
if (code !== 0) throw new Error(`Build failed with exit code ${code}`)
appPort = await findPort()
// rename the files so they aren't loaded by `next start`
// to test that they were bundled into the serverless files
for (const file of envFiles) {
await fs.rename(file, `${file}.bak`)
}
app = await nextStart(appDir, appPort, {
env: {
ENV_FILE_PROCESS_ENV: 'env-cli',
},
})
})
afterAll(async () => {
for (const file of envFiles) {
await fs.rename(`${file}.bak`, file)
}
await fs.writeFile(nextConfigPath, nextConfigContent)
await killApp(app)
})
runTests('serverless')
})
})
import { useAmp } from 'next/amp'
export const config = { amp: 'hybrid' }
export default () => <p>I'm an {useAmp() ? 'AMP' : 'normal'} page</p>
export default () => <p>Simple hybrid amp/non-amp page</p>
export const config = { amp: 'hybrid' }
import { useAmp } from 'next/amp'
export const config = { amp: 'hybrid' }
export default () => <p>I'm an {useAmp() ? 'AMP' : 'normal'} page</p>
export default () => <p>I am an AMP only page</p>
export const config = { amp: true }
export const config = { amp: 'hybrid' }
export default () => <p>I'm an AMP page</p>
export default function Docs(props) {
return <div>Hello again 👋</div>
}
export default function Index(props) {
return <div>Hello 👋</div>
}
/* eslint-env jest */
import { promises } from 'fs'
import { join } from 'path'
import cheerio from 'cheerio'
import { nextBuild, nextExport } from 'next-test-utils'
const { access, readFile } = promises
const appDir = join(__dirname, '../')
const outdir = join(appDir, 'out')
describe('Export with default map', () => {
beforeAll(async () => {
await nextBuild(appDir)
await nextExport(appDir, { outdir })
})
it('should export with folder that has dot in name', async () => {
expect.assertions(1)
await expect(access(join(outdir, 'v1.12.html'))).resolves.toBe(undefined)
})
it('should export an amp only page to clean path', async () => {
expect.assertions(1)
await expect(access(join(outdir, 'docs.html'))).resolves.toBe(undefined)
})
it('should export hybrid amp page correctly', async () => {
expect.assertions(2)
await expect(access(join(outdir, 'some.html'))).resolves.toBe(undefined)
await expect(access(join(outdir, 'some.amp.html'))).resolves.toBe(undefined)
})
it('should export nested hybrid amp page correctly', async () => {
expect.assertions(3)
await expect(access(join(outdir, 'docs.html'))).resolves.toBe(undefined)
await expect(access(join(outdir, 'docs.amp.html'))).resolves.toBe(undefined)
const html = await readFile(join(outdir, 'docs.html'))
const $ = cheerio.load(html)
expect($('link[rel=amphtml]').attr('href')).toBe('/docs.amp')
})
it('should export nested hybrid amp page correctly with folder', async () => {
expect.assertions(3)
await expect(access(join(outdir, 'info.html'))).resolves.toBe(undefined)
await expect(access(join(outdir, 'info.amp.html'))).resolves.toBe(undefined)
const html = await readFile(join(outdir, 'info.html'))
const $ = cheerio.load(html)
expect($('link[rel=amphtml]').attr('href')).toBe('/info.amp')
})
it('should export hybrid index amp page correctly', async () => {
expect.assertions(3)
await expect(access(join(outdir, 'index.html'))).resolves.toBe(undefined)
await expect(access(join(outdir, 'index.amp.html'))).resolves.toBe(
undefined
)
const html = await readFile(join(outdir, 'index.html'))
const $ = cheerio.load(html)
expect($('link[rel=amphtml]').attr('href')).toBe('/index.amp')
})
})
module.exports = {
target: 'serverless',
exportPathMap() {
return {
'/regression/jeff-is-cool': { page: '/regression/[slug]' },
}
},
}
import { useRouter } from 'next/router'
function Regression() {
const { asPath } = useRouter()
if (typeof window !== 'undefined') {
window.__AS_PATHS = [...new Set([...(window.__AS_PATHS || []), asPath])]
}
return <div id="asPath">{asPath}</div>
}
Regression.getInitialProps = () => ({})
export default Regression
/* eslint-env jest */
import { join } from 'path'
import cheerio from 'cheerio'
import webdriver from 'next-webdriver'
import {
nextBuild,
nextExport,
startCleanStaticServer,
stopApp,
renderViaHTTP,
} from 'next-test-utils'
const appDir = join(__dirname, '../')
const outdir = join(appDir, 'out')
describe('Export Dynamic Pages', () => {
let server
let port
beforeAll(async () => {
await nextBuild(appDir)
await nextExport(appDir, { outdir })
server = await startCleanStaticServer(outdir)
port = server.address().port
})
afterAll(async () => {
await stopApp(server)
})
it('should of exported with correct asPath', async () => {
const html = await renderViaHTTP(port, '/regression/jeff-is-cool')
const $ = cheerio.load(html)
expect($('#asPath').text()).toBe('/regression/jeff-is-cool')
})
it('should hydrate with correct asPath', async () => {
expect.assertions(1)
const browser = await webdriver(port, '/regression/jeff-is-cool')
try {
expect(await browser.eval(`window.__AS_PATHS`)).toEqual([
'/regression/jeff-is-cool',
])
} finally {
await browser.close()
}
})
})
export default () => <p>Welcome to dynamic imports.</p>
const { PHASE_DEVELOPMENT_SERVER } = require('next/constants')
module.exports = (phase) => {
return {
target: 'serverless',
distDir: phase === PHASE_DEVELOPMENT_SERVER ? '.next-dev' : '.next',
exportTrailingSlash: true,
exportPathMap: function () {
return {
'/': { page: '/' },
'/about': { page: '/about' },
'/button-link': { page: '/button-link' },
'/get-initial-props-with-no-query': {
page: '/get-initial-props-with-no-query',
},
'/counter': { page: '/counter' },
'/dynamic-imports': { page: '/dynamic-imports' },
'/dynamic': { page: '/dynamic', query: { text: 'cool dynamic text' } },
'/dynamic/one': {
page: '/dynamic',
query: { text: 'next export is nice' },
},
'/dynamic/two': {
page: '/dynamic',
query: { text: 'Vercel is awesome' },
},
'/file-name.md': {
page: '/dynamic',
query: { text: 'this file has an extension' },
},
'/query': { page: '/query', query: { a: 'blue' } },
'/query-update': { page: '/query-update', query: { a: 'blue' } },
// API route
'/blog/nextjs/comment/test': { page: '/blog/[post]/comment/[id]' },
}
}, // end exportPathMap
}
}
import Link from 'next/link'
const About = ({ bar }) => (
<div id="about-page">
<div>
<Link href="/">Go Back</Link>
</div>
<p>{`This is the About page foo${bar || ''}`}</p>
</div>
)
About.getInitialProps = async () => {
return { bar: typeof window === 'undefined' ? 'bar' : '' }
}
export default About
export default (req, res) => {
res.send('Hello World')
}
import { useRouter } from 'next/router'
const Page = () => {
const router = useRouter()
const { post, id } = router.query
return (
<>
<p>{`Blog post ${post} comment ${id || '(all)'}`}</p>
</>
)
}
Page.getInitialProps = () => ({})
export default Page
import Link from 'next/link'
export default () => (
<div id="button-link-page">
<div>
<Link href="/" passHref legacyBehavior>
<button>Go Back</button>
</Link>
</div>
<p>This is the About page</p>
</div>
)
import React from 'react'
import Link from 'next/link'
let counter = 0
export default class Counter extends React.Component {
increaseCounter() {
counter++
this.forceUpdate()
}
render() {
return (
<div id="counter-page">
<div>
<Link href="/" id="go-back">
Go Back
</Link>
</div>
<p>Counter: {counter}</p>
<button id="counter-increase" onClick={() => this.increaseCounter()}>
Increase
</button>
</div>
)
}
}
import Link from 'next/link'
import dynamic from 'next/dynamic'
const DynamicComponent = dynamic(() => import('../components/hello'))
export default () => (
<div id="dynamic-imports-page">
<div>
<Link href="/">Go Back</Link>
</div>
<DynamicComponent />
</div>
)
/* global location */
import React from 'react'
import Link from 'next/link'
export default class DynamicPage extends React.Component {
static getInitialProps({ query }) {
return { text: query.text }
}
state = {}
componentDidMount() {
const [, hash] = location.href.split('#')
this.setState({ hash })
}
render() {
const { text } = this.props
const { hash } = this.state
return (
<div id="dynamic-page">
<div>
<Link href="/">Go Back</Link>
</div>
<p>{text}</p>
<div id="hash">Hash: {hash}</div>
</div>
)
}
}
const Page = ({ query }) => <div>{`Query is: ${query}`}</div>
Page.getInitialProps = ({ query }) => {
return { query: JSON.stringify(query) }
}
export default Page
import Link from 'next/link'
import Router from 'next/router'
function routeToAbout(e) {
e.preventDefault()
Router.push('/about')
}
export default () => (
<div id="home-page">
<div>
<Link href="/about" legacyBehavior>
<a id="about-via-link">About via Link</a>
</Link>
<a href="#" onClick={routeToAbout} id="about-via-router">
About via Router
</a>
<Link href="/counter" legacyBehavior>
<a id="counter">Counter</a>
</Link>
<Link href="/dynamic?text=cool+dynamic+text" legacyBehavior>
<a id="get-initial-props">getInitialProps</a>
</Link>
<Link
href="/dynamic?text=next+export+is+nice"
as="/dynamic/one"
legacyBehavior
>
<a id="dynamic-1">Dynamic 1</a>
</Link>
<Link
href="/dynamic?text=Vercel+is+awesome"
as="/dynamic/two"
legacyBehavior
>
<a id="dynamic-2">Dynamic 2</a>
</Link>
<Link href="/dynamic?text=Vercel+is+awesome#cool" legacyBehavior>
<a id="with-hash">With Hash</a>
</Link>
<Link
href="/dynamic?text=this+file+has+an+extension"
as="/file-name.md"
legacyBehavior
>
<a id="path-with-extension">Path with extension</a>
</Link>
<Link href="/level1" legacyBehavior>
<a id="level1-home-page">Level1 home page</a>
</Link>
<Link href="/level1/about" legacyBehavior>
<a id="level1-about-page">Level1 about page</a>
</Link>
<Link href="/dynamic-imports" legacyBehavior>
<a id="dynamic-imports-link">Dynamic imports page</a>
</Link>
</div>
<p>This is the home page</p>
<style jsx>{`
a {
margin: 0 10px 0 0;
}
`}</style>
</div>
)
import Link from 'next/link'
export default () => (
<div id="level1-about-page">
<div>
<Link href="/">Go Back</Link>
</div>
<p>This is the Level1 about page</p>
</div>
)
import Link from 'next/link'
export default () => (
<div id="level1-home-page">
<div>
<Link href="/">Go Back</Link>
</div>
<p>This is the Level1 home page</p>
</div>
)
import { useRouter } from 'next/router'
const Page = () => <div id="query">{JSON.stringify(useRouter().query)}</div>
Page.getInitialProps = () => ({ hello: 'world' })
export default Page
import { Component } from 'react'
class Page extends Component {
static getInitialProps({ query }) {
return { query }
}
render() {
return JSON.stringify(this.props.query, null, 2)
}
}
export default Page
/* eslint-env jest */
import { join } from 'path'
import { File, runNextCommand } from 'next-test-utils'
export default function (context) {
describe('API routes export', () => {
const nextConfig = new File(join(context.appDir, 'next.config.js'))
beforeEach(() => {
nextConfig.replace('// API route', `'/data': { page: '/api/data' },`)
})
afterEach(() => {
nextConfig.restore()
})
it('Should throw if a route is matched', async () => {
const outdir = join(context.appDir, 'outApi')
const { stderr } = await runNextCommand(
['export', context.appDir, '--outdir', outdir],
{ stderr: true }
)
expect(stderr).toContain(
'https://nextjs.org/docs/messages/api-routes-static-export'
)
})
})
}
/* eslint-env jest */
import webdriver from 'next-webdriver'
import { check, getBrowserBodyText } from 'next-test-utils'
export default function (context) {
describe('Render via browser', () => {
it('should render the home page', async () => {
const browser = await webdriver(context.port, '/')
const text = await browser.elementByCss('#home-page p').text()
expect(text).toBe('This is the home page')
await browser.close()
})
it('should add trailing slash on Link', async () => {
const browser = await webdriver(context.port, '/')
const link = await browser
.elementByCss('#about-via-link')
.getAttribute('href')
expect(link.slice(-1)).toBe('/')
})
it('should not add trailing slash on Link when disabled', async () => {
const browser = await webdriver(context.portNoTrailSlash, '/')
const link = await browser
.elementByCss('#about-via-link')
.getAttribute('href')
expect(link.slice(-1)).not.toBe('/')
})
it('should do navigations via Link', async () => {
const browser = await webdriver(context.port, '/')
const text = await browser
.elementByCss('#about-via-link')
.click()
.waitForElementByCss('#about-page')
.elementByCss('#about-page p')
.text()
expect(text).toBe('This is the About page foo')
await browser.close()
})
it('should do navigations via Router', async () => {
const browser = await webdriver(context.port, '/')
const text = await browser
.elementByCss('#about-via-router')
.click()
.waitForElementByCss('#about-page')
.elementByCss('#about-page p')
.text()
expect(text).toBe('This is the About page foo')
await browser.close()
})
it('should do run client side javascript', async () => {
const browser = await webdriver(context.port, '/')
const text = await browser
.elementByCss('#counter')
.click()
.waitForElementByCss('#counter-page')
.elementByCss('#counter-increase')
.click()
.elementByCss('#counter-increase')
.click()
.elementByCss('#counter-page p')
.text()
expect(text).toBe('Counter: 2')
await browser.close()
})
it('should render pages using getInitialProps', async () => {
const browser = await webdriver(context.port, '/')
const text = await browser
.elementByCss('#get-initial-props')
.click()
.waitForElementByCss('#dynamic-page')
.elementByCss('#dynamic-page p')
.text()
expect(text).toBe('cool dynamic text')
await browser.close()
})
it('should render dynamic pages with custom urls', async () => {
const browser = await webdriver(context.port, '/')
const text = await browser
.elementByCss('#dynamic-1')
.click()
.waitForElementByCss('#dynamic-page')
.elementByCss('#dynamic-page p')
.text()
expect(text).toBe('next export is nice')
await browser.close()
})
it('should support client side navigation', async () => {
const browser = await webdriver(context.port, '/')
const text = await browser
.elementByCss('#counter')
.click()
.waitForElementByCss('#counter-page')
.elementByCss('#counter-increase')
.click()
.elementByCss('#counter-increase')
.click()
.elementByCss('#counter-page p')
.text()
expect(text).toBe('Counter: 2')
// let's go back and come again to this page:
const textNow = await browser
.elementByCss('#go-back')
.click()
.waitForElementByCss('#home-page')
.elementByCss('#counter')
.click()
.waitForElementByCss('#counter-page')
.elementByCss('#counter-page p')
.text()
expect(textNow).toBe('Counter: 2')
await browser.close()
})
it('should render dynamic import components in the client', async () => {
const browser = await webdriver(context.port, '/')
await browser
.elementByCss('#dynamic-imports-link')
.click()
.waitForElementByCss('#dynamic-imports-page')
await check(
() => getBrowserBodyText(browser),
/Welcome to dynamic imports/
)
await browser.close()
})
it('should render pages with url hash correctly', async () => {
let browser
try {
browser = await webdriver(context.port, '/')
// Check for the query string content
const text = await browser
.elementByCss('#with-hash')
.click()
.waitForElementByCss('#dynamic-page')
.elementByCss('#dynamic-page p')
.text()
expect(text).toBe('Vercel is awesome')
await check(() => browser.elementByCss('#hash').text(), /cool/)
} finally {
if (browser) {
await browser.close()
}
}
})
it('should navigate even if used a button inside <Link />', async () => {
const browser = await webdriver(context.port, '/button-link')
const text = await browser
.elementByCss('button')
.click()
.waitForElementByCss('#home-page')
.elementByCss('#home-page p')
.text()
expect(text).toBe('This is the home page')
await browser.close()
})
it('should update query after mount', async () => {
const browser = await webdriver(context.port, '/query-update?hello=world')
const query = await browser.elementByCss('#query').text()
expect(JSON.parse(query)).toEqual({ hello: 'world', a: 'blue' })
await browser.close()
})
describe('pages in the nested level: level1', () => {
it('should render the home page', async () => {
const browser = await webdriver(context.port, '/')
await browser.eval(
'document.getElementById("level1-home-page").click()'
)
await check(
() => getBrowserBodyText(browser),
/This is the Level1 home page/
)
await browser.close()
})
it('should render the about page', async () => {
const browser = await webdriver(context.port, '/')
await browser.eval(
'document.getElementById("level1-about-page").click()'
)
await check(
() => getBrowserBodyText(browser),
/This is the Level1 about page/
)
await browser.close()
})
})
})
}
/* eslint-env jest */
import webdriver from 'next-webdriver'
import { renderViaHTTP, getBrowserBodyText, check } from 'next-test-utils'
import cheerio from 'cheerio'
const loadJSONInPage = (pageContent) => {
const page = cheerio.load(pageContent)
return JSON.parse(page('#__next').text())
}
export default function (context) {
describe('Render in development mode', () => {
it('should render the home page', async () => {
const browser = await webdriver(context.port, '/')
await check(() => getBrowserBodyText(browser), /This is the home page/)
await browser.close()
})
it('should render pages only existent in exportPathMap page', async () => {
const browser = await webdriver(context.port, '/dynamic/one')
const text = await browser.elementByCss('#dynamic-page p').text()
expect(text).toBe('next export is nice')
await browser.close()
})
})
describe(`ExportPathMap's query in development mode`, () => {
it('should be present in ctx.query', async () => {
const pageContent = await renderViaHTTP(context.port, '/query')
const json = loadJSONInPage(pageContent)
expect(json).toEqual({ a: 'blue' })
})
it('should replace url query params in ctx.query when conflicting', async () => {
const pageContent = await renderViaHTTP(context.port, '/query?a=red')
const json = loadJSONInPage(pageContent)
expect(json).toEqual({ a: 'blue' })
})
it('should be merged with url query params in ctx.query', async () => {
const pageContent = await renderViaHTTP(context.port, '/query?b=green')
const json = loadJSONInPage(pageContent)
expect(json).toEqual({ a: 'blue', b: 'green' })
})
})
}
/* eslint-env jest */
import { join } from 'path'
import { File, runNextCommand } from 'next-test-utils'
export default function (context) {
describe('Dynamic routes export', () => {
const nextConfig = new File(join(context.appDir, 'next.config.js'))
beforeEach(() => {
nextConfig.replace('/blog/nextjs/comment/test', '/bad/path')
})
afterEach(() => {
nextConfig.restore()
})
it('Should throw error not matched route', async () => {
const outdir = join(context.appDir, 'outDynamic')
const { stderr } = await runNextCommand(
['export', context.appDir, '--outdir', outdir],
{ stderr: true }
).catch((err) => err)
expect(stderr).toContain(
'https://nextjs.org/docs/messages/export-path-mismatch'
)
})
})
}
/* eslint-env jest */
import { join } from 'path'
import { promises } from 'fs'
import {
nextBuild,
nextExport,
startStaticServer,
launchApp,
stopApp,
killApp,
findPort,
renderViaHTTP,
File,
} from 'next-test-utils'
import ssr from './ssr'
import browser from './browser'
import dev from './dev'
import dynamic from './dynamic'
import apiRoutes from './api-routes'
const { access, mkdir, writeFile } = promises
const appDir = join(__dirname, '../')
const context = {}
context.appDir = appDir
const devContext = {}
const nextConfig = new File(join(appDir, 'next.config.js'))
describe('Static Export', () => {
it('should delete existing exported files', async () => {
const outdir = join(appDir, 'out')
const tempfile = join(outdir, 'temp.txt')
await mkdir(outdir).catch((e) => {
if (e.code !== 'EEXIST') throw e
})
await writeFile(tempfile, 'Hello there')
await nextBuild(appDir)
await nextExport(appDir, { outdir })
let doesNotExist = false
await access(tempfile).catch((e) => {
if (e.code === 'ENOENT') doesNotExist = true
})
expect(doesNotExist).toBe(true)
})
beforeAll(async () => {
const outdir = join(appDir, 'out')
const outNoTrailSlash = join(appDir, 'outNoTrailSlash')
await nextBuild(appDir)
await nextExport(appDir, { outdir })
nextConfig.replace(
`exportTrailingSlash: true`,
`exportTrailingSlash: false`
)
await nextBuild(appDir)
await nextExport(appDir, { outdir: outNoTrailSlash })
nextConfig.restore()
context.server = await startStaticServer(outdir)
context.port = context.server.address().port
context.serverNoTrailSlash = await startStaticServer(outNoTrailSlash)
context.portNoTrailSlash = context.serverNoTrailSlash.address().port
devContext.port = await findPort()
devContext.server = await launchApp(
join(__dirname, '../'),
devContext.port,
true
)
// pre-build all pages at the start
await Promise.all([
renderViaHTTP(devContext.port, '/'),
renderViaHTTP(devContext.port, '/dynamic/one'),
])
})
afterAll(async () => {
await Promise.all([
stopApp(context.server),
killApp(devContext.server),
stopApp(context.serverNoTrailSlash),
])
})
ssr(context)
browser(context)
dev(devContext)
dynamic(context)
apiRoutes(context)
})
/* eslint-env jest */
import { renderViaHTTP } from 'next-test-utils'
import cheerio from 'cheerio'
export default function (context) {
describe('Render via SSR', () => {
it('should render the home page', async () => {
const html = await renderViaHTTP(context.port, '/')
expect(html).toMatch(/This is the home page/)
})
it('should render the about page', async () => {
const html = await renderViaHTTP(context.port, '/about')
expect(html).toMatch(/This is the About page foobar/)
})
it('should render links correctly', async () => {
const html = await renderViaHTTP(context.port, '/')
const $ = cheerio.load(html)
const dynamicLink = $('#dynamic-1').prop('href')
const filePathLink = $('#path-with-extension').prop('href')
expect(dynamicLink).toEqual('/dynamic/one/')
expect(filePathLink).toEqual('/file-name.md')
})
it('should render a page with getInitialProps', async () => {
const html = await renderViaHTTP(context.port, '/dynamic')
expect(html).toMatch(/cool dynamic text/)
})
it('should render a dynamically rendered custom url page', async () => {
const html = await renderViaHTTP(context.port, '/dynamic/one')
expect(html).toMatch(/next export is nice/)
})
it('should render pages with dynamic imports', async () => {
const html = await renderViaHTTP(context.port, '/dynamic-imports')
expect(html).toMatch(/Welcome to dynamic imports/)
})
it('should render paths with extensions', async () => {
const html = await renderViaHTTP(context.port, '/file-name.md')
expect(html).toMatch(/this file has an extension/)
})
it('should give empty object for query if there is no query', async () => {
const html = await renderViaHTTP(
context.port,
'/get-initial-props-with-no-query'
)
expect(html).toMatch(/Query is: {}/)
})
it('should render _error on 404.html even if not provided in exportPathMap', async () => {
const html = await renderViaHTTP(context.port, '/404.html')
// The default error page from the test server
// contains "404", so need to be specific here
expect(html).toMatch(/404.*page.*not.*found/i)
})
it('should render _error on /404/index.html', async () => {
const html = await renderViaHTTP(context.port, '/404/index.html')
// The default error page from the test server
// contains "404", so need to be specific here
expect(html).toMatch(/404.*page.*not.*found/i)
})
it('Should serve static files', async () => {
const data = await renderViaHTTP(context.port, '/static/data/item.txt')
expect(data).toBe('item')
})
it('Should serve public files', async () => {
const html = await renderViaHTTP(context.port, '/about')
const data = await renderViaHTTP(context.port, '/about/data.txt')
expect(html).toMatch(/This is the About page foobar/)
expect(data).toBe('data')
})
it('Should render dynamic files with query', async () => {
const html = await renderViaHTTP(
context.port,
'/blog/nextjs/comment/test'
)
expect(html).toMatch(/Blog post nextjs comment test/)
})
})
}
/* eslint-env jest */
import { promises } from 'fs'
import { join } from 'path'
import cheerio from 'cheerio'
import { nextBuild, nextExport } from 'next-test-utils'
const { access, readFile } = promises
const appDir = join(__dirname, '../')
const outdir = join(appDir, 'out')
describe('Export config#exportTrailingSlash set to false', () => {
beforeAll(async () => {
await nextBuild(appDir)
await nextExport(appDir, { outdir })
})
it('should export pages as [filename].html instead of [filename]/index.html', async () => {
expect.assertions(6)
await expect(access(join(outdir, 'index.html'))).resolves.toBe(undefined)
await expect(access(join(outdir, 'about.html'))).resolves.toBe(undefined)
await expect(access(join(outdir, 'posts.html'))).resolves.toBe(undefined)
await expect(access(join(outdir, 'posts', 'single.html'))).resolves.toBe(
undefined
)
const html = await readFile(join(outdir, 'index.html'))
const $ = cheerio.load(html)
expect($('p').text()).toBe('I am a home page')
const htmlSingle = await readFile(join(outdir, 'posts', 'single.html'))
const $single = cheerio.load(htmlSingle)
expect($single('p').text()).toBe('I am a single post')
})
})
module.exports = {
target: 'experimental-serverless-trace',
onDemandEntries: {
maxInactiveAge: 1000 * 60 * 60,
},
}
import firebase from 'firebase/app'
import 'firebase/firestore'
if (!firebase.apps.length) {
firebase.initializeApp({ projectId: 'noop' })
}
const store = firebase.firestore()
const Comp = ({ results }) => {
return <div>Hello Firebase: {results}</div>
}
Comp.getInitialProps = async () => {
const query = await store.collection('users').get()
return { results: query.size }
}
export default Comp
/* eslint-env jest */
import { join } from 'path'
import {
killApp,
findPort,
nextBuild,
nextStart,
renderViaHTTP,
} from 'next-test-utils'
const appDir = join(__dirname, '../')
let appPort
let app
describe('External Assets', () => {
beforeAll(async () => {
await nextBuild(appDir, [])
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))
it('should support Firebase', async () => {
const html = await renderViaHTTP(appPort, '/about/history')
expect(html).toMatch(/Hello Firebase: <!-- -->0/)
})
})
......@@ -11,11 +11,9 @@ import {
nextStart,
renderViaHTTP,
launchApp,
File,
} from 'next-test-utils'
const appDir = join(__dirname, '../')
const nextConfig = new File(join(appDir, 'next.config.js'))
let appPort
let app
......@@ -64,24 +62,4 @@ describe('Fallback Dynamic Route Params', () => {
runTests()
})
describe.skip('serverless mode', () => {
beforeAll(async () => {
nextConfig.write(`
module.exports = {
target: 'experimental-serverless-trace'
}
`)
await fs.remove(join(appDir, '.next'))
await nextBuild(appDir, [], { stdout: true })
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
nextConfig.delete()
})
runTests()
})
})
// This is not the correct way to implement the Next.js serverless target for production traffic
// It is only used for testing cases of rendering specific pages in the integration test suite
const path = require('path')
const http = require('http')
const send = require('send')
const server = http.createServer((req, res) => {
if (req.url === '/ssr') {
return require('./.next/serverless/pages/ssr.js').render(req, res)
}
if (req.url === '/getinitialprops') {
return require('./.next/serverless/pages/getinitialprops.js').render(
req,
res
)
}
if (req.url === '/api/api-route') {
return require('./.next/serverless/pages/api/api-route.js').default(
req,
res
)
}
if (req.url === '/static') {
return send(
req,
path.join(__dirname, '.next/serverless/pages/static.html')
).pipe(res)
}
if (req.url.startsWith('/_next')) {
send(
req,
path.join(__dirname, '.next', req.url.split('/_next').pop())
).pipe(res)
}
})
server.listen(process.env.PORT, () => {
console.log('ready on', process.env.PORT)
})
/* eslint-env jest */
import fs from 'fs-extra'
import { join } from 'path'
import {
killApp,
......@@ -13,7 +12,6 @@ import {
} from 'next-test-utils'
const appDir = join(__dirname, '../')
const nextConfig = join(appDir, 'next.config.js')
let appPort
let app
let apiServerPort
......@@ -37,20 +35,6 @@ const startApiServer = async (optEnv = {}, opts) => {
)
}
const startServerlessServer = async (optEnv = {}, opts) => {
const scriptPath = join(appDir, 'serverless-server.js')
appPort = await findPort()
const env = Object.assign({ ...process.env }, { PORT: `${appPort}` }, optEnv)
return await initNextServerScript(
scriptPath,
/ready on/i,
env,
/ReferenceError: options is not defined/,
opts
)
}
function runTests() {
it('includes polyfilled fetch when using getStaticProps', async () => {
const html = await renderViaHTTP(appPort, '/static')
......@@ -103,28 +87,4 @@ describe('Fetch polyfill with ky-universal', () => {
runTests()
})
describe.skip('Serverless support', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfig,
`module.exports = { target: 'serverless' }`
)
await startApiServer()
await nextBuild(appDir, [], {
env: {
NEXT_PUBLIC_API_PORT: apiServerPort,
},
})
appPort = await findPort()
app = await startServerlessServer()
})
afterAll(async () => {
await killApp(app)
await fs.remove(nextConfig)
await killApp(apiServer)
})
runTests()
})
})
// This is not the correct way to implement the Next.js serverless target for production traffic
// It is only used for testing cases of rendering specific pages in the integration test suite
const path = require('path')
const http = require('http')
const send = require('send')
const server = http.createServer((req, res) => {
if (req.url === '/ssr') {
return require('./.next/serverless/pages/ssr.js').render(req, res)
}
if (req.url === '/getinitialprops') {
return require('./.next/serverless/pages/getinitialprops.js').render(
req,
res
)
}
if (req.url === '/api/api-route') {
return require('./.next/serverless/pages/api/api-route.js').default(
req,
res
)
}
if (req.url === '/user/a') {
return send(
req,
path.join(__dirname, '.next/serverless/pages/user/a.html')
).pipe(res)
}
if (req.url === '/user/b') {
return send(
req,
path.join(__dirname, '.next/serverless/pages/user/b.html')
).pipe(res)
}
if (req.url === '/static') {
return send(
req,
path.join(__dirname, '.next/serverless/pages/static.html')
).pipe(res)
}
if (req.url.startsWith('/_next')) {
send(
req,
path.join(__dirname, '.next', req.url.split('/_next').pop())
).pipe(res)
}
})
server.listen(process.env.PORT, () => {
console.log('ready on', process.env.PORT)
})
/* eslint-env jest */
import fs from 'fs-extra'
import { join } from 'path'
import {
killApp,
......@@ -15,7 +14,6 @@ import {
import cheerio from 'cheerio'
const appDir = join(__dirname, '../')
const nextConfig = join(appDir, 'next.config.js')
let appPort
let app
let apiServerPort
......@@ -39,20 +37,6 @@ const startApiServer = async (optEnv = {}, opts) => {
)
}
const startServerlessServer = async (optEnv = {}, opts) => {
const scriptPath = join(appDir, 'serverless-server.js')
appPort = await findPort()
const env = Object.assign({ ...process.env }, { PORT: `${appPort}` }, optEnv)
return await initNextServerScript(
scriptPath,
/ready on/i,
env,
/ReferenceError: options is not defined/,
opts
)
}
function runTests() {
it('includes polyfilled fetch when using getStaticProps', async () => {
const html = await renderViaHTTP(appPort, '/static')
......@@ -122,28 +106,4 @@ describe('Fetch polyfill', () => {
runTests()
})
describe.skip('Serverless support', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfig,
`module.exports = { target: 'serverless' }`
)
await startApiServer()
await nextBuild(appDir, [], {
env: {
NEXT_PUBLIC_API_PORT: apiServerPort,
},
})
appPort = await findPort()
app = await startServerlessServer()
})
afterAll(async () => {
await killApp(app)
await fs.remove(nextConfig)
await killApp(apiServer)
})
runTests()
})
})
......@@ -9,7 +9,6 @@ import {
nextStart,
nextBuild,
renderViaHTTP,
initNextServerScript,
waitFor,
} from 'next-test-utils'
import webdriver from 'next-webdriver'
......@@ -22,20 +21,6 @@ const fsExists = (file) =>
.then(() => true)
.catch(() => false)
async function getBuildId(appDir) {
return fs.readFile(join(appDir, '.next', 'BUILD_ID'), 'utf8')
}
const startServerlessEmulator = async (dir, port, opts = {}) => {
const scriptPath = join(dir, 'server.js')
const env = Object.assign(
{},
{ ...process.env },
{ PORT: port, BUILD_ID: await getBuildId(dir) }
)
return initNextServerScript(scriptPath, /ready on/i, env, false, opts)
}
describe('Font Optimization', () => {
describe.each([
[
......@@ -79,7 +64,6 @@ describe('Font Optimization', () => {
preconnectUrl
) => {
const appDir = join(fixturesDir, `with-${property}`)
const nextConfig = join(appDir, 'next.config.js')
let builtServerPagesDir
let builtPage
let appPort
......@@ -250,50 +234,6 @@ describe('Font Optimization', () => {
runTests()
})
describe.skip('Font optimization for serverless apps', () => {
const origNextConfig = fs.readFileSync(nextConfig)
beforeAll(async () => {
await fs.writeFile(
nextConfig,
`module.exports = ({ target: 'serverless', cleanDistDir: false })`,
'utf8'
)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
builtServerPagesDir = join(appDir, '.next', 'serverless')
builtPage = (file) => join(builtServerPagesDir, file)
})
afterAll(async () => {
await fs.writeFile(nextConfig, origNextConfig)
await killApp(app)
})
runTests()
})
describe.skip('Font optimization for emulated serverless apps', () => {
const origNextConfig = fs.readFileSync(nextConfig)
beforeAll(async () => {
await fs.writeFile(
nextConfig,
`module.exports = ({ target: 'experimental-serverless-trace', cleanDistDir: false })`,
'utf8'
)
await nextBuild(appDir)
appPort = await findPort()
app = await startServerlessEmulator(appDir, appPort)
builtServerPagesDir = join(appDir, '.next', 'serverless')
builtPage = (file) => join(builtServerPagesDir, file)
})
afterAll(async () => {
await fs.writeFile(nextConfig, origNextConfig)
await killApp(app)
})
runTests()
})
describe('Font optimization for unreachable font definitions.', () => {
beforeAll(async () => {
await nextBuild(appDir)
......
module.exports = {
// replace me
async rewrites() {
return [
{
......
......@@ -7,13 +7,11 @@ import {
nextStart,
nextBuild,
renderViaHTTP,
File,
} from 'next-test-utils'
let app
let appPort
const appDir = join(__dirname, '..')
const nextConfig = new File(join(appDir, 'next.config.js'))
const runTests = () => {
it('should have gip in __NEXT_DATA__', async () => {
......@@ -52,21 +50,6 @@ describe('getInitialProps', () => {
runTests()
})
describe.skip('serverless mode', () => {
beforeAll(async () => {
await nextConfig.replace('// replace me', `target: 'serverless', `)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
nextConfig.restore()
})
runTests()
})
describe('production mode', () => {
beforeAll(async () => {
await nextBuild(appDir)
......
......@@ -29,19 +29,6 @@ const runTests = () => {
}
describe('getServerSideProps', () => {
describe.skip('serverless mode', () => {
beforeAll(async () => {
await fs.remove(join(appDir, '.next'))
await fs.writeFile(
nextConfig,
`module.exports = { target: 'experimental-serverless-trace' }`,
'utf8'
)
})
runTests()
})
describe('production mode', () => {
beforeAll(async () => {
await fs.remove(nextConfig)
......
......@@ -6,7 +6,6 @@ import fs from 'fs-extra'
import {
fetchViaHTTP,
findPort,
initNextServerScript,
killApp,
launchApp,
nextBuild,
......@@ -14,12 +13,10 @@ import {
renderViaHTTP,
} from 'next-test-utils'
import webdriver from 'next-webdriver'
import os from 'os'
import { join } from 'path'
import qs from 'querystring'
const appDir = join(__dirname, '..')
const nextConfigPath = join(appDir, 'next.config.js')
async function getBuildId() {
return fs.readFile(join(appDir, '.next', 'BUILD_ID'), 'utf8')
......@@ -176,22 +173,8 @@ function runTests(startServer = nextStart) {
})
}
const startServerlessEmulator = async (dir, port) => {
const scriptPath = join(dir, 'server.js')
const env = Object.assign(
{},
{ ...process.env },
{ PORT: port, BUILD_ID: await getBuildId() }
)
return initNextServerScript(scriptPath, /ready on/i, env)
}
describe('ServerSide Props Preview Mode', () => {
describe('Development Mode', () => {
beforeAll(async () => {
await fs.remove(nextConfigPath)
})
let appPort, app
it('should start development application', async () => {
appPort = await findPort()
......@@ -287,38 +270,6 @@ describe('ServerSide Props Preview Mode', () => {
})
describe('Server Mode', () => {
beforeAll(async () => {
await fs.remove(nextConfigPath)
})
runTests()
})
describe.skip('Serverless Mode', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfigPath,
`module.exports = { target: 'experimental-serverless-trace' }` + os.EOL
)
})
afterAll(async () => {
await fs.remove(nextConfigPath)
})
runTests()
})
describe.skip('Emulated Serverless Mode', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfigPath,
`module.exports = { target: 'experimental-serverless-trace' }` + os.EOL
)
})
afterAll(async () => {
await fs.remove(nextConfigPath)
})
runTests(startServerlessEmulator)
})
})
/* eslint-env jest */
import fs from 'fs-extra'
import { join } from 'path'
import cheerio from 'cheerio'
import {
......@@ -13,7 +12,6 @@ import {
} from 'next-test-utils'
const appDir = join(__dirname, '..')
const nextConfig = join(appDir, 'next.config.js')
let appPort
let app
......@@ -55,26 +53,4 @@ describe('pageProps GSSP conflict', () => {
runTests()
})
describe.skip('serverless mode', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfig,
`module.exports = {
target: 'experimental-serverless-trace'
}`
)
const { code } = await nextBuild(appDir)
if (code !== 0) throw new Error(`build failed with code ${code}`)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await fs.remove(nextConfig)
await killApp(app)
})
runTests()
})
})
/* eslint-env jest */
import http from 'http'
import url from 'url'
import fs from 'fs-extra'
import webdriver from 'next-webdriver'
......@@ -12,11 +11,9 @@ import {
nextStart,
fetchViaHTTP,
check,
File,
} from 'next-test-utils'
const appDir = join(__dirname, '..')
const nextConfig = new File(join(appDir, 'next.config.js'))
let app
let appPort
......@@ -505,132 +502,6 @@ describe('GS(S)P Redirect Support', () => {
runTests()
})
describe.skip('serverless mode', () => {
let server
beforeAll(async () => {
nextConfig.write(
`module.exports = {
basePath: "${basePath}",
target: 'experimental-serverless-trace'
}`
)
await fs.remove(join(appDir, '.next'))
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
nextConfig.restore()
await killApp(app)
try {
server.close()
} catch (err) {
console.error('failed to close server', err)
}
})
it('should handle redirect in raw serverless mode correctly', async () => {
server = http.createServer(async (req, res) => {
try {
console.log(req.url)
if (req.url.includes('/gsp-blog')) {
await require(join(
appDir,
'.next/serverless/pages/gsp-blog/[post].js'
)).render(req, res)
} else {
await require(join(
appDir,
'./.next/serverless/pages/gssp-blog/[post].js'
)).render(req, res)
}
} catch (err) {
console.error('failed to render', err)
res.statusCode = 500
res.end('error')
}
})
const port = await findPort()
await new Promise((resolve, reject) => {
server.listen(port, (err) => (err ? reject(err) : resolve()))
})
console.log(`Raw serverless server listening at port ${port}`)
const res1 = await fetchViaHTTP(
port,
'/gsp-blog/redirect-dest-_gsp-blog_first',
undefined,
{
redirect: 'manual',
}
)
expect(res1.status).toBe(307)
const text1 = await res1.text()
expect(text1).toEqual(`${basePath}/gsp-blog/first`)
const parsed = url.parse(res1.headers.get('location'), true)
expect(parsed.pathname).toBe(`${basePath}/gsp-blog/first`)
expect(parsed.query).toEqual({})
expect(res1.headers.get('refresh')).toBe(null)
const res2 = await fetchViaHTTP(
port,
'/gsp-blog/redirect-permanent-dest-_gsp-blog_first',
undefined,
{
redirect: 'manual',
}
)
expect(res2.status).toBe(308)
const text2 = await res2.text()
expect(text2).toEqual(`${basePath}/gsp-blog/first`)
expect(res2.headers.get('refresh')).toContain(
`url=${basePath}/gsp-blog/first`
)
const parsed2 = url.parse(res2.headers.get('location'), true)
expect(parsed2.pathname).toBe(`${basePath}/gsp-blog/first`)
expect(parsed2.query).toEqual({})
const res3 = await fetchViaHTTP(
port,
'/gssp-blog/redirect-dest-_gssp-blog_first',
undefined,
{
redirect: 'manual',
}
)
expect(res3.status).toBe(307)
const text3 = await res3.text()
expect(text3).toEqual(`${basePath}/gssp-blog/first`)
expect(res3.headers.get('refresh')).toBe(null)
const parsed3 = url.parse(res3.headers.get('location'), true)
expect(parsed3.pathname).toBe(`${basePath}/gssp-blog/first`)
expect(parsed3.query).toEqual({})
const res4 = await fetchViaHTTP(
port,
'/gssp-blog/redirect-permanent-dest-_gssp-blog_first',
undefined,
{
redirect: 'manual',
}
)
expect(res4.status).toBe(308)
const text4 = await res4.text()
expect(text4).toEqual(`${basePath}/gssp-blog/first`)
expect(res4.headers.get('refresh')).toContain(
`url=${basePath}/gssp-blog/first`
)
const parsed4 = url.parse(res4.headers.get('location'), true)
expect(parsed4.pathname).toBe(`${basePath}/gssp-blog/first`)
expect(parsed4.query).toEqual({})
})
runTests()
})
it('should error for redirect during prerendering', async () => {
await fs.mkdirp(join(appDir, 'pages/invalid'))
await fs.writeFile(
......
/* eslint-env jest */
import http from 'http'
import url from 'url'
import fs from 'fs-extra'
import webdriver from 'next-webdriver'
......@@ -15,7 +14,6 @@ import {
} from 'next-test-utils'
const appDir = join(__dirname, '..')
const nextConfig = join(appDir, 'next.config.js')
let app
let appPort
......@@ -542,120 +540,6 @@ describe('GS(S)P Redirect Support', () => {
})
})
describe.skip('serverless mode', () => {
let server
beforeAll(async () => {
await fs.writeFile(
nextConfig,
`module.exports = {
target: 'experimental-serverless-trace'
}`
)
await fs.remove(join(appDir, '.next'))
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await fs.remove(nextConfig)
await killApp(app)
try {
server.close()
} catch (err) {
console.error('failed to close server', err)
}
})
it('should handle redirect in raw serverless mode correctly', async () => {
server = http.createServer(async (req, res) => {
try {
console.log(req.url)
if (req.url.includes('/gsp-blog')) {
await require(join(
appDir,
'.next/serverless/pages/gsp-blog/[post].js'
)).render(req, res)
} else {
await require(join(
appDir,
'./.next/serverless/pages/gssp-blog/[post].js'
)).render(req, res)
}
} catch (err) {
console.error('failed to render', err)
res.statusCode = 500
res.end('error')
}
})
const port = await findPort()
await new Promise((resolve, reject) => {
server.listen(port, (err) => (err ? reject(err) : resolve()))
})
console.log(`Raw serverless server listening at port ${port}`)
const res1 = await fetchViaHTTP(
port,
'/gsp-blog/redirect-dest-_gsp-blog_first',
undefined,
{
redirect: 'manual',
}
)
expect(res1.status).toBe(307)
const parsed = url.parse(res1.headers.get('location'), true)
expect(parsed.pathname).toBe('/gsp-blog/first')
expect(parsed.query).toEqual({})
expect(res1.headers.get('refresh')).toBe(null)
const res2 = await fetchViaHTTP(
port,
'/gsp-blog/redirect-permanent-dest-_gsp-blog_first',
undefined,
{
redirect: 'manual',
}
)
expect(res2.status).toBe(308)
expect(res2.headers.get('refresh')).toMatch(/url=\/gsp-blog\/first/)
const parsed2 = url.parse(res2.headers.get('location'), true)
expect(parsed2.pathname).toBe('/gsp-blog/first')
expect(parsed2.query).toEqual({})
const res3 = await fetchViaHTTP(
port,
'/gssp-blog/redirect-dest-_gssp-blog_first',
undefined,
{
redirect: 'manual',
}
)
expect(res3.status).toBe(307)
expect(res3.headers.get('refresh')).toBe(null)
const parsed3 = url.parse(res3.headers.get('location'), true)
expect(parsed3.pathname).toBe('/gssp-blog/first')
expect(parsed3.query).toEqual({})
const res4 = await fetchViaHTTP(
port,
'/gssp-blog/redirect-permanent-dest-_gssp-blog_first',
undefined,
{
redirect: 'manual',
}
)
expect(res4.status).toBe(308)
expect(res4.headers.get('refresh')).toMatch(/url=\/gssp-blog\/first/)
const parsed4 = url.parse(res4.headers.get('location'), true)
expect(parsed4.pathname).toBe('/gssp-blog/first')
expect(parsed4.query).toEqual({})
})
runTests()
})
it('should error for redirect during prerendering', async () => {
await fs.mkdirp(join(appDir, 'pages/invalid'))
await fs.writeFile(
......
const Invalid = (): JSX.Element => <p>Hello world</p>
export { Invalid }
export default (): JSX.Element => <p>Hello world</p>
/* eslint-env jest */
import path from 'path'
import { nextBuild } from 'next-test-utils'
const appDir = path.join(__dirname, '..')
describe.skip('Handle non-page in pages when target: serverless', () => {
it('Fails softly with descriptive error', async () => {
const { stderr } = await nextBuild(appDir, [], { stderr: true })
expect(stderr).toMatch(
/found page without a React Component as default export in/
)
expect(stderr).toMatch(/pages\/invalid/)
})
})
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"exclude": ["node_modules"],
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"]
}
module.exports = {
// target: 'experimental-serverless-trace',
basePath: '/docs',
i18n: {
// localeDetection: false,
......
......@@ -8,7 +8,6 @@ import {
nextStart,
findPort,
killApp,
getPageFileFromPagesManifest,
fetchViaHTTP,
File,
launchApp,
......@@ -72,122 +71,6 @@ describe('i18n Support basePath', () => {
runTests(ctx)
})
describe.skip('serverless mode', () => {
beforeAll(async () => {
await fs.remove(join(appDir, '.next'))
nextConfig.replace('// target', 'target')
nextConfig.replace(/__EXTERNAL_PORT__/g, ctx.externalPort)
await nextBuild(appDir)
ctx.appPort = await findPort()
ctx.app = await nextStart(appDir, ctx.appPort)
ctx.buildPagesDir = join(appDir, '.next/serverless/pages')
ctx.buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8')
})
afterAll(async () => {
nextConfig.restore()
await killApp(ctx.app)
})
it('should have correct props for blocking notFound', async () => {
const serverFile = getPageFileFromPagesManifest(
appDir,
'/not-found/blocking-fallback/[slug]'
)
const appPort = await findPort()
const mod = require(join(appDir, '.next/serverless', serverFile))
const server = http.createServer(async (req, res) => {
try {
await mod.render(req, res)
} catch (err) {
res.statusCode = 500
res.end('internal err')
}
})
await new Promise((resolve, reject) => {
server.listen(appPort, (err) => (err ? reject(err) : resolve()))
})
console.log('listening on', appPort)
const res = await fetchViaHTTP(
appPort,
`${ctx.basePath}/nl/not-found/blocking-fallback/first`
)
server.close()
expect(res.status).toBe(404)
const $ = cheerio.load(await res.text())
const props = JSON.parse($('#props').text())
expect($('#not-found').text().length > 0).toBe(true)
expect(props).toEqual({
is404: true,
locale: 'nl',
locales,
defaultLocale: 'en-US',
})
})
it('should resolve rewrites correctly', async () => {
const serverFile = getPageFileFromPagesManifest(appDir, '/another')
const appPort = await findPort()
const mod = require(join(appDir, '.next/serverless', serverFile))
const server = http.createServer(async (req, res) => {
try {
await mod.render(req, res)
} catch (err) {
res.statusCode = 500
res.end('internal err')
}
})
await new Promise((resolve, reject) => {
server.listen(appPort, (err) => (err ? reject(err) : resolve()))
})
console.log('listening on', appPort)
const requests = await Promise.all(
[
'/en-US/rewrite-1',
'/nl/rewrite-2',
'/fr/rewrite-3',
'/en-US/rewrite-4',
'/fr/rewrite-4',
].map((path) =>
fetchViaHTTP(appPort, `${ctx.basePath}${path}`, undefined, {
redirect: 'manual',
})
)
)
server.close()
const checks = [
['en-US', '/rewrite-1'],
['nl', '/rewrite-2'],
['nl', '/rewrite-3'],
['en-US', '/rewrite-4'],
['fr', '/rewrite-4'],
]
for (let i = 0; i < requests.length; i++) {
const res = requests[i]
const [locale, asPath] = checks[i]
const $ = cheerio.load(await res.text())
expect($('html').attr('lang')).toBe(locale)
expect($('#router-locale').text()).toBe(locale)
expect($('#router-pathname').text()).toBe('/another')
expect($('#router-as-path').text()).toBe(asPath)
}
})
runTests(ctx)
})
describe('with localeDetection disabled', () => {
beforeAll(async () => {
await fs.remove(join(appDir, '.next'))
......
module.exports = {
// target: 'experimental-serverless-trace',
i18n: {
locales: ['nl-NL', 'nl-BE', 'nl', 'fr-BE', 'fr', 'en-US', 'en'],
defaultLocale: 'en-US',
......
/* eslint-env jest */
import assert from 'assert'
import http from 'http'
import qs from 'querystring'
import fs from 'fs-extra'
import cheerio from 'cheerio'
import { join } from 'path'
......@@ -13,13 +11,10 @@ import {
launchApp,
nextBuild,
nextStart,
File,
check,
getPageFileFromPagesManifest,
} from 'next-test-utils'
const appDir = join(__dirname, '../')
const nextConfig = new File(join(appDir, 'next.config.js'))
let app
let appPort
let buildPagesDir
......@@ -249,129 +244,4 @@ describe('i18n Support Root Catch-all', () => {
runTests()
})
describe.skip('serverless mode', () => {
beforeAll(async () => {
await fs.remove(join(appDir, '.next'))
nextConfig.replace('// target', 'target')
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
buildPagesDir = join(appDir, '.next/serverless')
})
afterAll(async () => {
nextConfig.restore()
await killApp(app)
})
it('should normalize locale items in route-matches', async () => {
const server = http.createServer(async (req, res) => {
try {
await require(join(
appDir,
'.next/serverless',
getPageFileFromPagesManifest(appDir, '/[[...slug]]')
)).render(req, res)
} catch (err) {
console.error(err)
res.statusCode = 500
res.end('internal server error')
}
})
const port = await findPort()
await new Promise((resolve, reject) => {
server.listen(port, (err) => (err ? reject(err) : resolve()))
})
console.log(`Listening at ::${port}`)
const res = await fetchViaHTTP(port, '/[[...slug]]', undefined, {
headers: {
'x-vercel-id': 'hi',
'x-now-route-matches': qs.stringify({
1: 'nl-NL',
}),
},
redirect: 'manual',
})
const res2 = await fetchViaHTTP(port, '/[[...slug]]', undefined, {
headers: {
'x-vercel-id': 'hi',
'x-now-route-matches': qs.stringify({
slug: 'eN',
}),
},
redirect: 'manual',
})
const res3 = await fetchViaHTTP(port, '/fr/[[...slug]]', undefined, {
headers: {
'x-vercel-id': 'hi',
'x-now-route-matches': qs.stringify({
slug: 'hello',
}),
},
redirect: 'manual',
})
const res4 = await fetchViaHTTP(port, '/fr', undefined, {
redirect: 'manual',
})
server.close()
expect(res.status).toBe(200)
expect(res2.status).toBe(200)
expect(res3.status).toBe(200)
expect(res4.status).toBe(200)
const $ = cheerio.load(await res.text())
const $2 = cheerio.load(await res2.text())
const $3 = cheerio.load(await res3.text())
expect($('#router-locale').text()).toBe('nl-NL')
expect($('#router-pathname').text()).toBe('/[[...slug]]')
expect($('#router-as-path').text()).toBe('/')
expect($('#router-default-locale').text()).toBe('en-US')
expect(JSON.parse($('#router-query').text())).toEqual({})
expect(JSON.parse($('#router-locales').text())).toEqual(locales)
expect(JSON.parse($('#props').text())).toEqual({
locale: 'nl-NL',
defaultLocale: 'en-US',
locales,
params: {},
})
expect($2('#router-locale').text()).toBe('en')
expect($2('#router-pathname').text()).toBe('/[[...slug]]')
expect($2('#router-as-path').text()).toBe('/')
expect($2('#router-default-locale').text()).toBe('en-US')
expect(JSON.parse($2('#router-query').text())).toEqual({})
expect(JSON.parse($2('#router-locales').text())).toEqual(locales)
expect(JSON.parse($2('#props').text())).toEqual({
locale: 'en',
defaultLocale: 'en-US',
locales,
params: {},
})
expect($3('#router-locale').text()).toBe('fr')
expect($3('#router-pathname').text()).toBe('/[[...slug]]')
expect($3('#router-as-path').text()).toBe('/hello')
expect($3('#router-default-locale').text()).toBe('en-US')
expect(JSON.parse($3('#router-query').text())).toEqual({
slug: ['hello'],
})
expect(JSON.parse($3('#router-locales').text())).toEqual(locales)
expect(JSON.parse($3('#props').text())).toEqual({
locale: 'fr',
defaultLocale: 'en-US',
locales,
params: {
slug: ['hello'],
},
})
})
runTests()
})
})
module.exports = {
// target: 'experimental-serverless-trace',
// basePath: '/docs',
// trailingSlash: true,
i18n: {
......
const http = require('http')
const mod = require('./.next/serverless/pages/another')
const server = http.createServer(async (req, res) => {
try {
await mod.render(req, res)
} catch (err) {
console.error(err)
res.statusCode = 500
res.end('internal error')
}
})
server.listen(3000, () => console.log('listening'))
......@@ -10,7 +10,6 @@ import {
nextStart,
findPort,
killApp,
getPageFileFromPagesManifest,
fetchViaHTTP,
File,
launchApp,
......@@ -89,122 +88,6 @@ describe('i18n Support', () => {
})
})
describe.skip('serverless mode', () => {
beforeAll(async () => {
await fs.remove(join(appDir, '.next'))
nextConfig.replace('// target', 'target')
nextConfig.replace(/__EXTERNAL_PORT__/g, ctx.externalPort)
await nextBuild(appDir)
ctx.appPort = await findPort()
ctx.app = await nextStart(appDir, ctx.appPort)
ctx.buildPagesDir = join(appDir, '.next/serverless/pages')
ctx.buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8')
})
afterAll(async () => {
nextConfig.restore()
await killApp(ctx.app)
})
it('should have correct props for blocking notFound', async () => {
const serverFile = getPageFileFromPagesManifest(
appDir,
'/not-found/blocking-fallback/[slug]'
)
const appPort = await findPort()
const mod = require(join(appDir, '.next/serverless', serverFile))
const server = http.createServer(async (req, res) => {
try {
await mod.render(req, res)
} catch (err) {
res.statusCode = 500
res.end('internal err')
}
})
await new Promise((resolve, reject) => {
server.listen(appPort, (err) => (err ? reject(err) : resolve()))
})
console.log('listening on', appPort)
const res = await fetchViaHTTP(
appPort,
'/nl/not-found/blocking-fallback/first'
)
server.close()
expect(res.status).toBe(404)
const $ = cheerio.load(await res.text())
const props = JSON.parse($('#props').text())
expect($('#not-found').text().length > 0).toBe(true)
expect(props).toEqual({
is404: true,
locale: 'nl',
locales,
defaultLocale: 'en-US',
})
})
it('should resolve rewrites correctly', async () => {
const serverFile = getPageFileFromPagesManifest(appDir, '/another')
const appPort = await findPort()
const mod = require(join(appDir, '.next/serverless', serverFile))
const server = http.createServer(async (req, res) => {
try {
await mod.render(req, res)
} catch (err) {
res.statusCode = 500
res.end('internal err')
}
})
await new Promise((resolve, reject) => {
server.listen(appPort, (err) => (err ? reject(err) : resolve()))
})
console.log('listening on', appPort)
const requests = await Promise.all(
[
'/en-US/rewrite-1',
'/nl/rewrite-2',
'/fr/rewrite-3',
'/en-US/rewrite-4',
'/fr/rewrite-4',
].map((path) =>
fetchViaHTTP(appPort, `${ctx.basePath}${path}`, undefined, {
redirect: 'manual',
})
)
)
server.close()
const checks = [
['en-US', '/rewrite-1'],
['nl', '/rewrite-2'],
['nl', '/rewrite-3'],
['en-US', '/rewrite-4'],
['fr', '/rewrite-4'],
]
for (let i = 0; i < requests.length; i++) {
const res = requests[i]
const [locale, asPath] = checks[i]
const $ = cheerio.load(await res.text())
expect($('html').attr('lang')).toBe(locale)
expect($('#router-locale').text()).toBe(locale)
expect($('#router-pathname').text()).toBe('/another')
expect($('#router-as-path').text()).toBe(asPath)
}
})
runTests(ctx)
})
describe('with localeDetection disabled', () => {
beforeAll(async () => {
await fs.remove(join(appDir, '.next'))
......
......@@ -208,26 +208,4 @@ describe('nested index.js', () => {
runTests()
})
describe.skip('serverless mode', () => {
let origNextConfig
beforeAll(async () => {
origNextConfig = await fs.readFile(nextConfig, 'utf8')
await fs.writeFile(
nextConfig,
`module.exports = { target: 'serverless' }`
)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await fs.writeFile(nextConfig, origNextConfig)
await killApp(app)
})
runTests()
})
})
module.exports = { target: 'serverless' }
import { Client } from 'faunadb'
export default (_req, res) => {
const client = new Client({ secret: 'foobar' })
console.log(client)
res.send({ message: 'hello world' })
}
/* eslint-env jest */
import { join } from 'path'
import {
renderViaHTTP,
runNextCommand,
nextServer,
startApp,
stopApp,
} from 'next-test-utils'
let app
let appPort
let server
const appDir = join(__dirname, '../')
describe('Legacy Packages', () => {
beforeAll(async () => {
await runNextCommand(['build', appDir])
app = nextServer({
dir: appDir,
dev: false,
quiet: true,
})
server = await startApp(app)
appPort = server.address().port
})
it('should support `node-gently` packages', async () => {
const res = await renderViaHTTP(appPort, '/api/hello')
expect(res).toMatch(/hello world/i)
})
afterAll(() => stopApp(server))
})
......@@ -8,24 +8,7 @@ const appDir = join(__dirname, '..')
const indexPage = join(appDir, 'pages/index.js')
let origIndexPage = ''
const runTests = (serverless = false) => {
if (serverless) {
const nextConfig = join(appDir, 'next.config.js')
beforeEach(() =>
fs.writeFile(
nextConfig,
`
module.exports = {
target: 'experimental-serverless-trace'
}
`
)
)
afterAll(() => fs.remove(nextConfig))
}
const runTests = () => {
it('should error when legacy unstable_getStaticProps', async () => {
const { stderr, code } = await nextBuild(appDir, [], { stderr: true })
expect(code).toBe(1)
......@@ -72,8 +55,4 @@ describe('Mixed getStaticProps and getServerSideProps error', () => {
describe('server mode', () => {
runTests(false)
})
describe.skip('serverless mode', () => {
runTests(true)
})
})
/* eslint-env jest */
import fs from 'fs-extra'
import {
check,
findPort,
......@@ -16,7 +15,6 @@ import webdriver from 'next-webdriver'
import { join } from 'path'
const appDir = join(__dirname, '../')
const nextConfig = join(appDir, 'next.config.js')
let appPort
let app
......@@ -504,30 +502,4 @@ describe('Image Component basePath Tests', () => {
runTests('server')
})
describe.skip('serverless mode', () => {
let origConfig
beforeAll(async () => {
origConfig = await fs.readFile(nextConfig, 'utf8')
await fs.writeFile(
nextConfig,
`
module.exports = {
basePath: '/docs',
target: 'serverless'
}
`
)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await fs.writeFile(nextConfig, origConfig)
await killApp(app)
})
runTests('serverless')
})
})
......@@ -1446,17 +1446,4 @@ describe('Image Component Tests', () => {
runTests('server')
})
describe.skip('serverless mode', () => {
beforeAll(async () => {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
})
runTests('serverless')
})
})
/* eslint-env jest */
import fs from 'fs-extra'
import { join } from 'path'
import {
renderViaHTTP,
......@@ -12,7 +11,6 @@ import {
} from 'next-test-utils'
const appDir = join(__dirname, '..')
const nextConfig = join(appDir, 'next.config.js')
let appPort
let app
......@@ -43,7 +41,6 @@ describe('Optional chaining and nullish coalescing support', () => {
describe('server mode', () => {
beforeAll(async () => {
await fs.remove(nextConfig)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
......@@ -52,22 +49,4 @@ describe('Optional chaining and nullish coalescing support', () => {
runTests()
})
describe.skip('serverless mode', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfig,
`module.exports = { target: 'serverless' }`
)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
await fs.remove(nextConfig)
})
runTests()
})
})
module.exports = {
target: 'experimental-serverless-trace',
}
import { useRouter } from 'next/router'
export default function Comment({ params }) {
const router = useRouter()
if (router.isFallback) return 'Loading...'
return (
<>
<p id="as-path">{router.asPath}</p>
<p id="query">{JSON.stringify(router.query)}</p>
<p id="params">{JSON.stringify(params)}</p>
</>
)
}
export const getStaticProps = ({ params }) => {
return {
props: {
params,
},
}
}
export const getStaticPaths = () => {
return {
paths: [],
fallback: true,
}
}
import { useRouter } from 'next/router'
export default function Post({ params }) {
const router = useRouter()
if (router.isFallback) return 'Loading...'
return (
<>
<p id="as-path">{router.asPath}</p>
<p id="query">{JSON.stringify(router.query)}</p>
<p id="params">{JSON.stringify(params)}</p>
</>
)
}
export const getStaticProps = ({ params }) => {
return {
props: {
params,
},
}
}
export const getStaticPaths = () => {
return {
paths: [],
fallback: true,
}
}
const http = require('http')
const url = require('url')
const server = http.createServer(async (req, res) => {
try {
const { pathname } = url.parse(req.url)
switch (pathname) {
case '/blog/[post]':
case '/web-app/blog/[post]':
return require('./.next/serverless/pages/blog/[post].js').render(
req,
res
)
case '/blog/[post]/[comment]':
case '/web-app/blog/[post]/[comment]':
return require('./.next/serverless/pages/blog/[post]/[comment].js').render(
req,
res
)
default:
return res.end('404')
}
} catch (err) {
console.error('error rendering', err)
}
})
server.listen(process.env.PORT, () => {
console.log('ready on', process.env.PORT)
})
/* eslint-env jest */
import fs from 'fs-extra'
import { join } from 'path'
import cheerio from 'cheerio'
import {
initNextServerScript,
killApp,
findPort,
nextBuild,
fetchViaHTTP,
} from 'next-test-utils'
const appDir = join(__dirname, '..')
let app
let appPort
describe('Fallback asPath normalizing', () => {
beforeAll(async () => {
const startServerlessEmulator = async (dir, port, buildId) => {
const scriptPath = join(dir, 'server.js')
const env = Object.assign({}, { ...process.env }, { PORT: port })
return initNextServerScript(scriptPath, /ready on/i, env, false)
}
await fs.remove(join(appDir, '.next'))
await nextBuild(appDir)
appPort = await findPort()
app = await startServerlessEmulator(appDir, appPort)
})
afterAll(() => killApp(app))
it('should have normalized asPath for fallback page', async () => {
const html = await fetchViaHTTP(appPort, '/blog/[post]', undefined, {
headers: {
'x-now-route-matches': '1=post-1',
'x-vercel-id': 'hi',
},
}).then((res) => res.text())
const $ = cheerio.load(html)
const asPath = $('#as-path').text()
const query = JSON.parse($('#query').text())
const params = JSON.parse($('#params').text())
expect(asPath).toBe('/blog/post-1')
expect(query).toEqual({ post: 'post-1' })
expect(params).toEqual({ post: 'post-1' })
})
it('should have normalized asPath for fallback page with entry directory', async () => {
const html = await fetchViaHTTP(
appPort,
'/web-app/blog/[post]',
undefined,
{
headers: {
'x-now-route-matches': '1=post-abc',
'x-vercel-id': 'hi',
},
}
).then((res) => res.text())
const $ = cheerio.load(html)
const asPath = $('#as-path').text()
const query = JSON.parse($('#query').text())
const params = JSON.parse($('#params').text())
expect(asPath).toBe('/web-app/blog/post-abc')
expect(query).toEqual({ post: 'post-abc' })
expect(params).toEqual({ post: 'post-abc' })
})
it('should have normalized asPath for fallback page multi-params', async () => {
const html = await fetchViaHTTP(
appPort,
'/blog/[post]/[comment]',
undefined,
{
headers: {
'x-now-route-matches': '1=post-1&2=comment-2',
'x-vercel-id': 'hi',
},
}
).then((res) => res.text())
const $ = cheerio.load(html)
const asPath = $('#as-path').text()
const query = JSON.parse($('#query').text())
const params = JSON.parse($('#params').text())
expect(asPath).toBe('/blog/post-1/comment-2')
expect(query).toEqual({ post: 'post-1', comment: 'comment-2' })
expect(params).toEqual({ post: 'post-1', comment: 'comment-2' })
})
it('should have normalized asPath for fallback page with entry directory multi-params', async () => {
const html = await fetchViaHTTP(
appPort,
'/web-app/blog/[post]/[comment]',
undefined,
{
headers: {
'x-now-route-matches': '1=post-abc&2=comment-cba',
'x-vercel-id': 'hi',
},
}
).then((res) => res.text())
const $ = cheerio.load(html)
const asPath = $('#as-path').text()
const query = JSON.parse($('#query').text())
const params = JSON.parse($('#params').text())
expect(asPath).toBe('/web-app/blog/post-abc/comment-cba')
expect(query).toEqual({ post: 'post-abc', comment: 'comment-cba' })
expect(params).toEqual({ post: 'post-abc', comment: 'comment-cba' })
})
})
/* eslint-env jest */
import fs from 'fs-extra'
import { join } from 'path'
import { nextBuild } from 'next-test-utils'
const appDir = join(__dirname, '..')
const nextConfig = join(appDir, 'next.config.js')
describe('Legacy Prerender', () => {
describe('handles old getStaticParams', () => {
......@@ -16,18 +14,5 @@ describe('Legacy Prerender', () => {
'unstable_getStaticParams was replaced with getStaticPaths. Please update your code.'
)
})
it('should fail the build in serverless mode', async () => {
await fs.writeFile(
nextConfig,
`module.exports = { target: 'serverless' }`
)
const out = await nextBuild(appDir, [], { stderr: true })
await fs.remove(nextConfig)
expect(out.stderr).toMatch(`Build error occurred`)
expect(out.stderr).toMatch(
'unstable_getStaticParams was replaced with getStaticPaths. Please update your code.'
)
})
})
})
......@@ -13,18 +13,17 @@ import {
import { join } from 'path'
const appDir = join(__dirname, '..')
const nextConfig = join(appDir, 'next.config.js')
let app
let appPort
let buildId
let stderr
function runTests(route, routePath, serverless) {
function runTests(route, routePath) {
it(`[${route}] should not revalidate when set to false`, async () => {
const fileName = join(
appDir,
'.next',
serverless ? 'serverless' : 'server',
'server',
getPageFileFromPagesManifest(appDir, routePath)
)
const initialHtml = await renderViaHTTP(appPort, route)
......@@ -53,7 +52,7 @@ function runTests(route, routePath, serverless) {
const fileName = join(
appDir,
'.next',
serverless ? 'serverless' : 'server',
'server',
getPageFileFromPagesManifest(appDir, routePath)
)
const route = join(`/_next/data/${buildId}`, `${routePath}.json`)
......@@ -77,37 +76,8 @@ function runTests(route, routePath, serverless) {
}
describe('SSG Prerender No Revalidate', () => {
afterAll(() => fs.remove(nextConfig))
describe.skip('serverless mode', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfig,
`module.exports = { target: 'experimental-serverless-trace' }`,
'utf8'
)
await fs.remove(join(appDir, '.next'))
await nextBuild(appDir)
appPort = await findPort()
stderr = ''
app = await nextStart(appDir, appPort, {
onStderr: (msg) => {
stderr += msg
},
})
buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8')
})
afterAll(() => killApp(app))
runTests('/', '/', true)
runTests('/named', '/named', true)
runTests('/nested', '/nested', true)
runTests('/nested/named', '/nested/named', true)
})
describe('production mode', () => {
beforeAll(async () => {
await fs.remove(nextConfig)
await fs.remove(join(appDir, '.next'))
await nextBuild(appDir, [])
appPort = await findPort()
......
......@@ -6,7 +6,6 @@ import fs from 'fs-extra'
import {
fetchViaHTTP,
findPort,
initNextServerScript,
killApp,
launchApp,
nextBuild,
......@@ -14,12 +13,10 @@ import {
renderViaHTTP,
} from 'next-test-utils'
import webdriver from 'next-webdriver'
import os from 'os'
import { join } from 'path'
import qs from 'querystring'
const appDir = join(__dirname, '..')
const nextConfigPath = join(appDir, 'next.config.js')
async function getBuildId() {
return fs.readFile(join(appDir, '.next', 'BUILD_ID'), 'utf8')
......@@ -269,22 +266,8 @@ function runTests(startServer = nextStart) {
})
}
const startServerlessEmulator = async (dir, port) => {
const scriptPath = join(dir, 'server.js')
const env = Object.assign(
{},
{ ...process.env },
{ PORT: port, BUILD_ID: await getBuildId() }
)
return initNextServerScript(scriptPath, /ready on/i, env)
}
describe('Prerender Preview Mode', () => {
describe('Development Mode', () => {
beforeAll(async () => {
await fs.remove(nextConfigPath)
})
let appPort, app
it('should start development application', async () => {
appPort = await findPort()
......@@ -398,38 +381,6 @@ describe('Prerender Preview Mode', () => {
})
describe('Server Mode', () => {
beforeAll(async () => {
await fs.remove(nextConfigPath)
})
runTests()
})
describe.skip('Serverless Mode', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfigPath,
`module.exports = { target: 'experimental-serverless-trace' }` + os.EOL
)
})
afterAll(async () => {
await fs.remove(nextConfigPath)
})
runTests()
})
describe.skip('Emulated Serverless Mode', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfigPath,
`module.exports = { target: 'experimental-serverless-trace' }` + os.EOL
)
})
afterAll(async () => {
await fs.remove(nextConfigPath)
})
runTests(startServerlessEmulator)
})
})
......@@ -17,7 +17,7 @@ let app
let appPort
let buildId
function runTests(route, routePath, serverless) {
function runTests(route, routePath) {
it(`[${route}] should regenerate page when revalidate time exceeded`, async () => {
const fileName = join(
appDir,
......
......@@ -12,29 +12,21 @@ import {
launchApp,
nextBuild,
nextStart,
File,
renderViaHTTP,
check,
} from 'next-test-utils'
const appDir = join(__dirname, '..')
const nextConfig = new File(join(appDir, 'next.config.js'))
let app
let appPort
let previewCookie
const getCacheFile = (path = '', serverless) => {
return join(
appDir,
'.next',
serverless ? 'serverless' : 'server',
'pages',
path
)
const getCacheFile = (path = '') => {
return join(appDir, '.next', 'server', 'pages', path)
}
function runTests(isDev, serverless) {
function runTests(isDev) {
it('should get preview cookie correctly', async () => {
const res = await fetchViaHTTP(appPort, '/api/enable')
previewCookie = ''
......@@ -83,7 +75,7 @@ function runTests(isDev, serverless) {
})
if (!isDev) {
const fsHtml = await fs.readFile(getCacheFile('index.html', serverless))
const fsHtml = await fs.readFile(getCacheFile('index.html'))
const fsProps = JSON.parse(cheerio.load(fsHtml)('#props').text())
expect(fsProps).toEqual({
......@@ -125,9 +117,7 @@ function runTests(isDev, serverless) {
})
if (!isDev) {
const fsHtml = await fs.readFile(
getCacheFile('no-fallback/first.html', serverless)
)
const fsHtml = await fs.readFile(getCacheFile('no-fallback/first.html'))
const fsProps = JSON.parse(cheerio.load(fsHtml)('#props').text())
expect(fsProps).toEqual({
......@@ -165,9 +155,9 @@ function runTests(isDev, serverless) {
})
if (!isDev) {
expect(
await fs.exists(getCacheFile('no-fallback/second.html', serverless))
).toBe(false)
expect(await fs.exists(getCacheFile('no-fallback/second.html'))).toBe(
false
)
}
const res2 = await fetchViaHTTP(appPort, '/no-fallback/second')
......@@ -199,9 +189,7 @@ function runTests(isDev, serverless) {
})
if (!isDev) {
const fsHtml = await fs.readFile(
getCacheFile('fallback/first.html', serverless)
)
const fsHtml = await fs.readFile(getCacheFile('fallback/first.html'))
const fsProps = JSON.parse(cheerio.load(fsHtml)('#props').text())
expect(fsProps).toEqual({
......@@ -251,9 +239,7 @@ function runTests(isDev, serverless) {
})
if (!isDev) {
const fsHtml = await fs.readFile(
getCacheFile('fallback/second.html', serverless)
)
const fsHtml = await fs.readFile(getCacheFile('fallback/second.html'))
const fsProps = JSON.parse(cheerio.load(fsHtml)('#props').text())
expect(fsProps).toEqual({
......@@ -302,24 +288,4 @@ describe('Preview mode with fallback pages', () => {
runTests()
})
describe.skip('serverless mode', () => {
beforeAll(async () => {
nextConfig.write(`
module.exports = {
target: 'experimental-serverless-trace'
}
`)
await fs.remove(join(appDir, '.next'))
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
nextConfig.delete()
await killApp(app)
})
runTests(true, true)
})
})
// This is not the correct way to implement the Next.js serverless target for production traffic
// It is only used for testing cases of rendering specific pages in the integration test suite
const path = require('path')
const http = require('http')
const send = require('send')
const server = http.createServer((req, res) => {
if (req.url === '/ssr') {
return require('./.next/serverless/pages/ssr.js').render(req, res)
}
if (req.url === '/getinitialprops') {
return require('./.next/serverless/pages/getinitialprops.js').render(
req,
res
)
}
if (req.url === '/api/api-route') {
return require('./.next/serverless/pages/api/api-route.js').default(
req,
res
)
}
if (req.url === '/user/a') {
return send(
req,
path.join(__dirname, '.next/serverless/pages/user/a.html')
).pipe(res)
}
if (req.url === '/user/b') {
return send(
req,
path.join(__dirname, '.next/serverless/pages/user/b.html')
).pipe(res)
}
if (req.url === '/static') {
return send(
req,
path.join(__dirname, '.next/serverless/pages/static.html')
).pipe(res)
}
if (req.url.startsWith('/_next')) {
send(
req,
path.join(__dirname, '.next', req.url.split('/_next').pop())
).pipe(res)
}
})
server.listen(process.env.PORT, () => {
console.log('ready on', process.env.PORT)
})
......@@ -5,8 +5,6 @@ import { nextBuild, getPageFileFromBuildManifest } from 'next-test-utils'
import { recursiveReadDir } from 'next/dist/lib/recursive-readdir'
const appDir = join(__dirname, '../')
const nextConfig = join(appDir, 'next.config.js')
let nextConfigContent
function runTests() {
it('includes sourcemaps for all browser files', async () => {
......@@ -50,25 +48,4 @@ describe('Production browser sourcemaps', () => {
runTests()
})
describe.skip('Serverless support', () => {
beforeAll(async () => {
nextConfigContent = await fs.readFile(nextConfig, 'utf8')
await fs.writeFile(
nextConfig,
`
module.exports = {
target: 'serverless',
productionBrowserSourceMaps: true
}
`
)
await nextBuild(appDir, [], {})
})
afterAll(async () => {
await fs.writeFile(nextConfig, nextConfigContent)
})
runTests()
})
})
module.exports = {
target: 'serverless',
experimental: {
profiling: true,
},
onDemandEntries: {
// Make sure entries are not getting disposed.
maxInactiveAge: 1000 * 60 * 60,
},
}
import Link from 'next/link'
export default () => (
<div>
<Link href="/about">About Page</Link>
<p className="index-page">Hello World</p>
</div>
)
/* eslint-env jest */
import { join } from 'path'
import { nextBuild } from 'next-test-utils'
import fs from 'fs'
const appDir = join(__dirname, '../')
const profileEventsPath = join(appDir, '.next', 'profile-events.json')
// TODO: Make profiling experimental flag work with webpack 5
describe.skip('Profiling Usage', () => {
beforeAll(async () => {
// Delete file if it already exists
if (fs.existsSync(profileEventsPath))
fs.unlink(profileEventsPath, () => {
console.log('Deleted Existing profile-events.json file')
})
await nextBuild(appDir)
})
describe('Profiling the build', () => {
it('should emit files', async () => {
expect(fs.existsSync(profileEventsPath)).toBe(true)
})
})
})
module.exports = {
// ensure incorrect target is overridden by env
target: 'serverless',
rewrites() {
return [
{
......
......@@ -7,7 +7,6 @@ import {
killApp,
findPort,
nextBuild,
initNextServerScript,
renderViaHTTP,
nextStart,
waitFor,
......@@ -15,7 +14,6 @@ import {
} from 'next-test-utils'
const appDir = join(__dirname, '../')
const nextConfigPath = join(appDir, 'next.config.js')
let appPort
let buildId
let app
......@@ -29,13 +27,7 @@ const checkAsPath = async (urlPath, expectedAsPath) => {
expect(asPath).toBe(expectedAsPath)
}
const runTests = (isServerless) => {
if (isServerless) {
it('should render with correct asPath with /index requested', async () => {
await checkAsPath('/index', '/')
})
}
const runTests = () => {
it('should render with correct asPath with /_next/data /index requested', async () => {
stdout = ''
const path = `/_next/data/${buildId}/index.json`
......@@ -78,45 +70,6 @@ const runTests = (isServerless) => {
}
describe('Revalidate asPath Normalizing', () => {
describe.skip('raw serverless mode', () => {
beforeAll(async () => {
await fs.remove(join(appDir, '.next'))
await fs.writeFile(
nextConfigPath,
`
module.exports = {
target: 'experimental-serverless-trace'
}
`
)
appPort = await findPort()
await nextBuild(appDir)
buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8')
app = await initNextServerScript(
join(appDir, 'server.js'),
/ready on/,
{
...process.env,
PORT: appPort,
BUILD_ID: buildId,
},
/error/,
{
onStdout(msg) {
stdout += msg || ''
},
}
)
})
afterAll(async () => {
await killApp(app)
await fs.remove(nextConfigPath)
})
runTests()
})
describe('server mode', () => {
beforeAll(async () => {
await fs.remove(join(appDir, '.next'))
......
......@@ -5,16 +5,13 @@ import cheerio from 'cheerio'
import {
killApp,
findPort,
File,
nextBuild,
nextStart,
initNextServerScript,
renderViaHTTP,
waitFor,
} from 'next-test-utils'
const appDir = join(__dirname, '../')
const nextConfig = new File(join(appDir, 'next.config.js'))
let app
let appPort
......@@ -24,7 +21,7 @@ const getProps = async (path) => {
return JSON.parse($('#props').text())
}
const runTests = (rawServerless = false) => {
const runTests = () => {
it('should render / correctly', async () => {
const props = await getProps('/')
expect(props.params).toEqual({})
......@@ -37,20 +34,6 @@ const runTests = (rawServerless = false) => {
expect(props.random).not.toBe(newProps.random)
})
if (rawServerless) {
it('should render /index correctly', async () => {
const props = await getProps('/index')
expect(props.params).toEqual({})
await waitFor(1000)
await getProps('/index')
const newProps = await getProps('/index')
expect(newProps.params).toEqual({})
expect(props.random).not.toBe(newProps.random)
})
}
it('should render /a correctly', async () => {
const props = await getProps('/a')
expect(props.params).toEqual({ slug: ['a'] })
......@@ -87,27 +70,4 @@ describe('Root Optional Catch-all Revalidate', () => {
runTests()
})
describe.skip('raw serverless mode', () => {
beforeAll(async () => {
nextConfig.write(`
module.exports = {
target: 'experimental-serverless-trace'
}
`)
await nextBuild(appDir)
appPort = await findPort()
app = await initNextServerScript(join(appDir, 'server.js'), /ready on/i, {
...process.env,
PORT: appPort,
})
})
afterAll(async () => {
nextConfig.delete()
await killApp(app)
})
runTests(true)
})
})
module.exports = { target: 'experimental-serverless-trace' }
import styles from './page1.module.css'
function Page1() {
function Page1(props) {
return (
<p id="page-text" className={styles.abc}>
1
......@@ -8,4 +8,9 @@ function Page1() {
)
}
export async function getServerSideProps() {
await new Promise((resolve) => setTimeout(resolve, 5000))
return { props: {} }
}
export default Page1
const http = require('http')
const url = require('url')
const fs = require('fs')
const path = require('path')
const server = http.createServer(async (req, res) => {
let { pathname } = url.parse(req.url)
pathname = pathname.replace(/\/$/, '')
let isDataReq = false
if (pathname.startsWith('/_next/data')) {
isDataReq = true
pathname = pathname
.replace(`/_next/data/${process.env.BUILD_ID}/`, '/')
.replace(/\.json$/, '')
}
console.log('serving', pathname)
if (pathname === '/favicon.ico') {
res.statusCode = 404
return res.end()
}
if (pathname.startsWith('/_next/static/')) {
let prom = Promise.resolve()
if (pathname.endsWith('.css')) {
prom = new Promise((resolve) => setTimeout(resolve, 20000))
}
prom.then(() => {
res.write(
fs.readFileSync(
path.join(
__dirname,
'./.next/static/',
decodeURI(pathname.slice('/_next/static/'.length))
),
'utf8'
)
)
return res.end()
})
} else {
if (pathname === '') {
pathname = '/index'
}
const ext = isDataReq ? 'json' : 'html'
if (
fs.existsSync(
path.join(__dirname, `./.next/serverless/pages${pathname}.${ext}`)
)
) {
res.write(
fs.readFileSync(
path.join(__dirname, `./.next/serverless/pages${pathname}.${ext}`),
'utf8'
)
)
return res.end()
}
let re
try {
re = require(`./.next/serverless/pages${pathname}`)
} catch {
const d = decodeURI(pathname)
if (
fs.existsSync(
path.join(__dirname, `./.next/serverless/pages${d}.${ext}`)
)
) {
res.write(
fs.readFileSync(
path.join(__dirname, `./.next/serverless/pages${d}.${ext}`),
'utf8'
)
)
return res.end()
}
const routesManifest = require('./.next/routes-manifest.json')
const { dynamicRoutes } = routesManifest
dynamicRoutes.some(({ page, regex }) => {
if (new RegExp(regex).test(pathname)) {
if (
fs.existsSync(
path.join(__dirname, `./.next/serverless/pages${page}.${ext}`)
)
) {
res.write(
fs.readFileSync(
path.join(__dirname, `./.next/serverless/pages${page}.${ext}`),
'utf8'
)
)
res.end()
return true
}
re = require(`./.next/serverless/pages${page}`)
return true
}
return false
})
}
if (!res.finished) {
try {
return await (typeof re.render === 'function'
? re.render(req, res)
: re.default(req, res))
} catch (e) {
console.log('FAIL_FUNCTION', e)
res.statusCode = 500
res.write('FAIL_FUNCTION')
res.end()
}
}
}
})
server.listen(process.env.PORT, () => {
console.log('ready on', process.env.PORT)
})
/* eslint-env jest */
import fs from 'fs-extra'
import {
findPort,
initNextServerScript,
killApp,
nextBuild,
nextServer,
runNextCommand,
startApp,
stopApp,
waitFor,
} from 'next-test-utils'
import webdriver from 'next-webdriver'
......@@ -13,6 +12,7 @@ import { join } from 'path'
let app
let appPort
let server
const appDir = join(__dirname, '../')
function runTests() {
......@@ -34,27 +34,19 @@ function runTests() {
describe('route cancel via CSS', () => {
beforeAll(async () => {
const startServerlessEmulator = async (dir, port, buildId) => {
const scriptPath = join(dir, 'server.js')
const env = Object.assign(
{},
{ ...process.env },
{ PORT: port, BUILD_ID: buildId }
)
return initNextServerScript(scriptPath, /ready on/i, env, false, {})
}
await fs.remove(join(appDir, '.next'))
await nextBuild(appDir)
const buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8')
appPort = await findPort()
app = await startServerlessEmulator(appDir, appPort, buildId)
})
await runNextCommand(['build', appDir])
app = nextServer({
dir: appDir,
dev: false,
quiet: true,
})
afterAll(async () => {
await killApp(app)
server = await startApp(app)
appPort = server.address().port
})
afterAll(() => stopApp(server))
runTests()
})
/* eslint-env jest */
import fs from 'fs-extra'
import {
fetchViaHTTP,
findPort,
......@@ -26,8 +25,6 @@ function runTests() {
})
}
const nextConfig = join(appDir, 'next.config.js')
describe('serverside asset modules', () => {
describe('dev mode', () => {
beforeAll(async () => {
......@@ -41,11 +38,6 @@ describe('serverside asset modules', () => {
describe('production mode', () => {
beforeAll(async () => {
const curConfig = await fs.readFile(nextConfig, 'utf8')
if (curConfig.includes('target')) {
await fs.writeFile(nextConfig, `module.exports = {}`)
}
await nextBuild(appDir)
appPort = await findPort()
......@@ -55,26 +47,4 @@ describe('serverside asset modules', () => {
runTests()
})
describe.skip('serverless mode', () => {
let origNextConfig
beforeAll(async () => {
origNextConfig = await fs.readFile(nextConfig, 'utf8')
await fs.writeFile(
nextConfig,
`module.exports = { target: 'serverless' }`
)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await fs.writeFile(nextConfig, origNextConfig)
await killApp(app)
})
runTests()
})
})
import getConfig from 'next/config'
const config = getConfig()
export default function App({ Component, pageProps }) {
return (
<>
<p id="app-config">{JSON.stringify(config)}</p>
<Component {...pageProps} />
</>
)
}
import Document, { Html, Head, Main, NextScript } from 'next/document'
import getConfig from 'next/config'
const config = getConfig()
class MyDocument extends Document {
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx)
return { ...initialProps }
}
render() {
return (
<Html>
<Head />
<body>
<div id="doc-config">{JSON.stringify(config)}</div>
<Main />
<NextScript />
</body>
</Html>
)
}
}
export default MyDocument
import getConfig from 'next/config'
const config = getConfig()
export default (req, res) => {
res.json(config)
}
import getConfig from 'next/config'
const config = getConfig()
const page = () => <p id="config">{JSON.stringify(config)}</p>
page.getInitialProps = () => ({ a: 'b' })
export default page
const path = require('path')
const http = require('http')
const send = require('send')
const server = http.createServer((req, res) => {
if (req.url === '/config') {
return require('./.next/serverless/pages/config.js').render(req, res)
}
if (req.url === '/') {
return send(
req,
path.join(__dirname, '.next/serverless/pages/index.html')
).pipe(res)
}
if (req.url === '/api/config') {
return require('./.next/serverless/pages/api/config.js').default(req, res)
}
if (req.url.startsWith('/_next')) {
send(
req,
path.join(__dirname, '.next', req.url.split('/_next').pop())
).pipe(res)
}
})
server.listen(process.env.PORT, () => {
console.log('ready on', process.env.PORT)
})
/* eslint-env jest */
import fs from 'fs-extra'
import { join } from 'path'
import {
nextBuild,
findPort,
killApp,
launchApp,
renderViaHTTP,
initNextServerScript,
} from 'next-test-utils'
import cheerio from 'cheerio'
import webdriver from 'next-webdriver'
const appDir = join(__dirname, '../')
const nextConfigPath = join(appDir, 'next.config.js')
const cleanUp = () => fs.remove(nextConfigPath)
const nextStart = async (appDir, appPort) => {
const scriptPath = join(appDir, 'server.js')
const env = Object.assign({ ...process.env }, { PORT: `${appPort}` })
return initNextServerScript(
scriptPath,
/ready on/i,
env,
/ReferenceError: options is not defined/
)
}
const runTests = (oldServerless = false) => {
const serverlessMode = oldServerless
? 'serverless'
: 'experimental-serverless-trace'
it('should not error on usage of publicRuntimeConfig', async () => {
await fs.writeFile(
nextConfigPath,
`module.exports = {
target: '${serverlessMode}',
publicRuntimeConfig: {
hello: 'world'
}
}`
)
const { stderr, code } = await nextBuild(appDir, undefined, {
stderr: true,
})
expect(code).toBe(0)
expect(stderr).not.toMatch(
/Cannot use publicRuntimeConfig or serverRuntimeConfig/
)
})
it('should not error on usage of serverRuntimeConfig', async () => {
await fs.writeFile(
nextConfigPath,
`module.exports = {
target: '${serverlessMode}',
serverRuntimeConfig: {
hello: 'world'
}
}`
)
const { stderr, code } = await nextBuild(appDir, undefined, {
stderr: true,
})
expect(code).toBe(0)
expect(stderr).not.toMatch(
/Cannot use publicRuntimeConfig or serverRuntimeConfig/
)
})
const testRuntimeConfig = async (app, appPort) => {
const browser = await webdriver(appPort, '/config')
const clientHTML = await browser.eval(`document.documentElement.innerHTML`)
const ssrHTML = await renderViaHTTP(appPort, '/config')
const apiJson = await renderViaHTTP(appPort, '/api/config')
await killApp(app)
await fs.remove(nextConfigPath)
const ssr$ = cheerio.load(ssrHTML)
const client$ = cheerio.load(clientHTML)
const ssrConfig = ssr$('#config').text()
const clientConfig = client$('#config').text()
const expectedSsrConfig = {
publicRuntimeConfig: {
another: 'thing',
},
serverRuntimeConfig: {
hello: 'world',
},
}
const expectedClientConfig = {
publicRuntimeConfig: {
another: 'thing',
},
serverRuntimeConfig: {},
}
expect(JSON.parse(ssrConfig)).toEqual(expectedSsrConfig)
expect(JSON.parse(clientConfig)).toEqual(expectedClientConfig)
const appSsrConfig = ssr$('#app-config').text()
const appClientConfig = client$('#app-config').text()
expect(JSON.parse(appSsrConfig)).toEqual(expectedSsrConfig)
expect(JSON.parse(appClientConfig)).toEqual(expectedClientConfig)
const docSsrConfig = ssr$('#doc-config').text()
const docClientConfig = client$('#doc-config').text()
// _document doesn't update on client so should be the same
expect(JSON.parse(docSsrConfig)).toEqual(expectedSsrConfig)
expect(JSON.parse(docClientConfig)).toEqual(expectedSsrConfig)
expect(JSON.parse(apiJson)).toEqual(expectedSsrConfig)
}
it('should support runtime configs in serverless mode (production)', async () => {
await fs.writeFile(
nextConfigPath,
`module.exports = {
target: '${serverlessMode}',
serverRuntimeConfig: {
hello: 'world'
},
publicRuntimeConfig: {
another: 'thing'
}
}`
)
await nextBuild(appDir, [], { stderr: true, stdout: true })
const appPort = await findPort()
const app = await nextStart(appDir, appPort)
await testRuntimeConfig(app, appPort)
})
it('should support runtime configs in serverless mode (dev)', async () => {
await fs.writeFile(
nextConfigPath,
`module.exports = {
target: '${serverlessMode}',
serverRuntimeConfig: {
hello: 'world'
},
publicRuntimeConfig: {
another: 'thing'
}
}`
)
const appPort = await findPort()
const app = await launchApp(appDir, appPort)
await testRuntimeConfig(app, appPort)
})
}
describe.skip('Serverless runtime configs', () => {
beforeAll(() => cleanUp())
afterAll(() => cleanUp())
describe.skip('legacy serverless mode', () => {
runTests(true)
})
describe.skip('experimental-serverless-trace mode', () => {
runTests()
})
})
module.exports = {
target: 'experimental-serverless-trace',
// make sure error isn't thrown from empty publicRuntimeConfig
publicRuntimeConfig: {},
}
export const getStaticProps = () => {
return {
props: {
hello: 'hello world',
random: Math.random(),
},
revalidate: 10,
}
}
export default ({ hello, random }) => (
<>
<p id="hello">{hello}</p>
<p id="random">{random}</p>
</>
)
const path = require('path')
const http = require('http')
const server = http.createServer((req, res) => {
const pagePath = (page) => path.join('.next/serverless/pages/', page)
const render = (page) => {
require(`./${pagePath(page)}`).render(req, res)
}
switch (req.url) {
case '/revalidate': {
return render('/revalidate')
}
default: {
return res.end('404')
}
}
})
server.listen(process.env.PORT, () => {
console.log('ready on', process.env.PORT)
})
/* eslint-env jest */
import fs from 'fs-extra'
import { join } from 'path'
import {
nextBuild,
findPort,
killApp,
initNextServerScript,
} from 'next-test-utils'
const appDir = join(__dirname, '../')
let appPort
let app
let buildId
const nextStart = async (appDir, appPort) => {
const scriptPath = join(appDir, 'server.js')
const env = Object.assign({ ...process.env }, { PORT: `${appPort}` })
return initNextServerScript(
scriptPath,
/ready on/i,
env,
/ReferenceError: options is not defined/
)
}
describe.skip('Serverless Trace', () => {
beforeAll(async () => {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8')
})
afterAll(() => killApp(app))
it('should have revalidate page in prerender-manifest with correct interval', async () => {
const data = await fs.readJSON(
join(appDir, '.next/prerender-manifest.json')
)
expect(data.routes['/revalidate']).toEqual({
initialRevalidateSeconds: 10,
dataRoute: `/_next/data/${buildId}/revalidate.json`,
srcRoute: null,
})
})
it('should set correct Cache-Control header', async () => {
const url = `http://localhost:${appPort}/revalidate`
const res = await fetch(url)
expect(res.headers.get('Cache-Control')).toMatch(
's-maxage=10, stale-while-revalidate'
)
})
})
module.exports = {
target: 'experimental-serverless-trace',
onDemandEntries: {
// Make sure entries are not getting disposed.
maxInactiveAge: 1000 * 60 * 60,
},
// make sure error isn't thrown from empty publicRuntimeConfig
publicRuntimeConfig: {},
}
export default ({ query }, res) => {
res.json(query)
}
export default (req, res) => {
res.send('hello world')
}
export default ({ query }, res) => {
res.json(query)
}
import dynamic from 'next/dynamic'
const Hello = dynamic(() => import('../components/hello'))
export default () => (
<div>
<Hello />
</div>
)
import dynamic from 'next/dynamic'
const Hello = dynamic(() => import('../components/hello'))
export default () => (
<div>
<Hello />
</div>
)
import fetch from 'isomorphic-unfetch'
import React from 'react'
export default class extends React.Component {
static async getInitialProps() {
try {
const res = await fetch('')
const text = await res.text()
console.log(text)
return { text }
} catch (err) {
if (err.message.includes('is not a function')) {
return { failed: true, error: err.toString() }
}
return { error: err.toString() }
}
}
render() {
const { failed, error, text } = this.props
return (
<div className="fetch-page">
{failed ? 'failed' : ''}
{error}
<div id="text">{text}</div>
</div>
)
}
}
import Link from 'next/link'
export default () => {
return (
<div>
Hello World
<Link href="/fetch" id="fetchlink">
fetch page
</Link>
</div>
)
}
export default () => `Hi Im an AMP page!`
export const config = { amp: 'hybrid' }
/* eslint-env jest */
import webdriver from 'next-webdriver'
import { join } from 'path'
import { existsSync, readdirSync } from 'fs'
import {
killApp,
findPort,
nextBuild,
nextStart,
renderViaHTTP,
fetchViaHTTP,
readNextBuildClientPageFile,
getPageFileFromPagesManifest,
} from 'next-test-utils'
import fetch from 'node-fetch'
const appDir = join(__dirname, '../')
const serverlessDir = join(appDir, '.next/serverless/pages')
const chunksDir = join(appDir, '.next/static/chunks')
let appPort
let app
describe.skip('Serverless Trace', () => {
beforeAll(async () => {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))
it('should render the page', async () => {
const html = await renderViaHTTP(appPort, '/')
expect(html).toMatch(/Hello World/)
})
it('should serve file from public folder', async () => {
const content = await renderViaHTTP(appPort, '/hello.txt')
expect(content.trim()).toBe('hello world')
})
it('should render the page with dynamic import', async () => {
const html = await renderViaHTTP(appPort, '/dynamic')
expect(html).toMatch(/Hello!/)
})
it('should render the page with same dynamic import', async () => {
const html = await renderViaHTTP(appPort, '/dynamic-two')
expect(html).toMatch(/Hello!/)
})
it('should render 404', async () => {
const html = await renderViaHTTP(appPort, '/404')
expect(html).toMatch(/This page could not be found/)
})
it('should render 404 for /_next/static', async () => {
const html = await renderViaHTTP(appPort, '/_next/static')
expect(html).toMatch(/This page could not be found/)
})
it('should render an AMP page', async () => {
const html = await renderViaHTTP(appPort, '/some-amp?amp=1')
expect(html).toMatch(/Hi Im an AMP page/)
expect(html).toMatch(/ampproject\.org/)
})
it('should have correct amphtml rel link', async () => {
const html = await renderViaHTTP(appPort, '/some-amp')
expect(html).toMatch(/Hi Im an AMP page/)
expect(html).toMatch(/rel="amphtml" href="\/some-amp\.amp"/)
})
it('should have correct canonical link', async () => {
const html = await renderViaHTTP(appPort, '/some-amp?amp=1')
expect(html).toMatch(/rel="canonical" href="\/some-amp"/)
})
it('should have correct canonical link (auto-export link)', async () => {
const html = await renderViaHTTP(appPort, '/some-amp.amp')
expect(html).toMatch(/rel="canonical" href="\/some-amp"/)
})
it('should render correctly when importing isomorphic-unfetch', async () => {
const url = `http://localhost:${appPort}/fetch`
const res = await fetch(url)
expect(res.status).toBe(200)
const text = await res.text()
expect(text.includes('failed')).toBe(false)
})
it('should render correctly when importing isomorphic-unfetch on the client side', async () => {
const browser = await webdriver(appPort, '/')
try {
const text = await browser
.elementByCss('a')
.click()
.waitForElementByCss('.fetch-page')
.elementByCss('#text')
.text()
expect(text).toMatch(/fetch page/)
} finally {
await browser.close()
}
})
it('should not have combined client-side chunks', () => {
expect(readdirSync(chunksDir).length).toBeGreaterThanOrEqual(2)
const contents = readNextBuildClientPageFile(appDir, '/dynamic')
expect(contents).not.toContain('Hello!')
})
it('should not output _app.js and _document.js to serverless build', () => {
expect(existsSync(join(serverlessDir, '_app.js'))).toBeFalsy()
expect(existsSync(join(serverlessDir, '_document.js'))).toBeFalsy()
})
it('should replace static pages with HTML files', async () => {
const pages = ['/abc', '/dynamic', '/dynamic-two', '/some-amp']
for (const page of pages) {
const file = getPageFileFromPagesManifest(appDir, page)
expect(file.endsWith('.html')).toBe(true)
}
})
it('should not replace non-static pages with HTML files', async () => {
const pages = ['/fetch', '/_error']
for (const page of pages) {
const file = getPageFileFromPagesManifest(appDir, page)
expect(file.endsWith('.js')).toBe(true)
}
})
it('should reply on API request successfully', async () => {
const content = await renderViaHTTP(appPort, '/api/hello')
expect(content).toMatch(/hello world/)
})
it('should reply on dynamic API request successfully', async () => {
const result = await renderViaHTTP(appPort, '/api/posts/post-1')
const { id } = JSON.parse(result)
expect(id).toBe('post-1')
})
it('should reply on dynamic API request successfully with query parameters', async () => {
const result = await renderViaHTTP(appPort, '/api/posts/post-1?param=val')
const { id, param } = JSON.parse(result)
expect(id).toBe('post-1')
expect(param).toBe('val')
})
it('should reply on dynamic API index request successfully', async () => {
const result = await renderViaHTTP(appPort, '/api/dynamic/post-1')
const { path } = JSON.parse(result)
expect(path).toBe('post-1')
})
it('should reply on dynamic API index request successfully with query parameters', async () => {
const result = await renderViaHTTP(appPort, '/api/dynamic/post-1?param=val')
const { path, param } = JSON.parse(result)
expect(path).toBe('post-1')
expect(param).toBe('val')
})
it('should reply with redirect on API request with trailing slash', async () => {
const res = await fetchViaHTTP(
appPort,
'/api/hello/',
{},
{ redirect: 'manual' }
)
expect(res.status).toBe(308)
expect(res.headers.get('location')).toBe(
`http://localhost:${appPort}/api/hello`
)
})
it('should reply on API request with trailing slassh successfully', async () => {
const content = await renderViaHTTP(appPort, '/api/hello/')
expect(content).toMatch(/hello world/)
})
describe('With basic usage', () => {
it('should allow etag header support', async () => {
const url = `http://localhost:${appPort}/`
const etag = (await fetch(url)).headers.get('ETag')
const headers = { 'If-None-Match': etag }
const res2 = await fetch(url, { headers })
expect(res2.status).toBe(304)
})
it('should set Content-Length header', async () => {
const url = `http://localhost:${appPort}`
const res = await fetch(url)
expect(res.headers.get('Content-Length')).toBeDefined()
})
})
})
module.exports = {
target: 'serverless',
onDemandEntries: {
// Make sure entries are not getting disposed.
maxInactiveAge: 1000 * 60 * 60,
},
// make sure error isn't thrown from empty publicRuntimeConfig
publicRuntimeConfig: {},
}
export default () => <div>test</div>
export default ({ query }, res) => {
res.json(query)
}
export default (req, res) => {
res.send('hello world')
}
export default ({ query }, res) => {
res.json(query)
}
throw new Error('top-level-oops')
// eslint-disable-next-line
export default (req, res) => {
res.end('hi')
}
const SlugPage = ({ query }) => <div>{JSON.stringify(query)}</div>
SlugPage.getInitialProps = ({ query }) => ({ query })
export default SlugPage
const SlugPage = ({ query }) => <div>{JSON.stringify(query)}</div>
SlugPage.getInitialProps = ({ query }) => ({ query })
export default SlugPage
import dynamic from 'next/dynamic'
const Hello = dynamic(() => import('../components/hello'))
export default () => (
<div>
<Hello />
</div>
)
import dynamic from 'next/dynamic'
const Hello = dynamic(() => import('../components/hello'))
export default () => (
<div>
<Hello />
</div>
)
const fetch = require('isomorphic-unfetch')
const React = require('react')
export default class extends React.Component {
static async getInitialProps() {
try {
const res = await fetch('')
const text = await res.text()
console.log(text)
return { text }
} catch (err) {
if (err.message.includes('is not a function')) {
return { failed: true, error: err.toString() }
}
return { error: err.toString() }
}
}
render() {
const { failed, error, text } = this.props
return (
<div className="fetch-cjs-page">
{failed ? 'failed' : ''}
{error}
<div id="text">{text}</div>
</div>
)
}
}
import fetch from 'isomorphic-unfetch'
import React from 'react'
export default class extends React.Component {
static async getInitialProps() {
try {
const res = await fetch('')
const text = await res.text()
console.log(text)
return { text }
} catch (err) {
if (err.message.includes('is not a function')) {
return { failed: true, error: err.toString() }
}
return { error: err.toString() }
}
}
render() {
const { failed, error, text } = this.props
return (
<div className="fetch-page">
{failed ? 'failed' : ''}
{error}
<div id="text">{text}</div>
</div>
)
}
}
import Link from 'next/link'
export default () => {
return (
<div>
Hello World
<Link href="/fetch" id="fetchlink">
fetch page
</Link>
<Link href="/fetch-cjs" id="fetchcjslink">
fetch cjs page
</Link>
</div>
)
}
export default () => `Hi Im an AMP page!`
export const config = { amp: 'hybrid' }
/* eslint-env jest */
import webdriver from 'next-webdriver'
import { join } from 'path'
import cheerio from 'cheerio'
import { existsSync, readdirSync, readFileSync } from 'fs'
import {
killApp,
waitFor,
findPort,
nextBuild,
nextStart,
fetchViaHTTP,
renderViaHTTP,
getPageFileFromBuildManifest,
getPageFileFromPagesManifest,
} from 'next-test-utils'
import qs from 'querystring'
import fetch from 'node-fetch'
const appDir = join(__dirname, '../')
const serverlessDir = join(appDir, '.next/serverless/pages')
const chunksDir = join(appDir, '.next/static/chunks')
let stderr = ''
let appPort
let app
describe.skip('Serverless', () => {
let output
beforeAll(async () => {
const result = await nextBuild(appDir, undefined, {
stderr: true,
stdout: true,
})
output = result.stdout + result.stderr
appPort = await findPort()
app = await nextStart(appDir, appPort, {
onStderr: (msg) => {
stderr += msg || ''
},
})
})
afterAll(() => killApp(app))
it('should show target config deprecation warning', () => {
expect(output).toContain(
'The `target` config is deprecated and will be removed in a future version'
)
})
it('should render the page', async () => {
const html = await renderViaHTTP(appPort, '/')
expect(html).toMatch(/Hello World/)
})
it('should add autoExport for auto pre-rendered pages', async () => {
for (const page of ['/', '/abc']) {
const html = await renderViaHTTP(appPort, page)
const $ = cheerio.load(html)
const data = JSON.parse($('#__NEXT_DATA__').html())
expect(data.autoExport).toBe(true)
}
})
it('should not add autoExport for non pre-rendered pages', async () => {
for (const page of ['/fetch']) {
const html = await renderViaHTTP(appPort, page)
const $ = cheerio.load(html)
const data = JSON.parse($('#__NEXT_DATA__').html())
expect(!!data.autoExport).toBe(false)
}
})
it('should serve file from public folder', async () => {
const content = await renderViaHTTP(appPort, '/hello.txt')
expect(content.trim()).toBe('hello world')
const legacy = await renderViaHTTP(appPort, '/static/legacy.txt')
expect(legacy).toMatch(`new static folder`)
})
it('should not infinity loop on a 404 static file', async () => {
expect.assertions(2)
// ensure top-level static does not exist (important for test)
// we expect /public/static, though.
expect(existsSync(join(appDir, 'static'))).toBe(false)
const res = await fetchViaHTTP(appPort, '/static/404')
expect(res.status).toBe(404)
})
it('should render the page with dynamic import', async () => {
const html = await renderViaHTTP(appPort, '/dynamic')
expect(html).toMatch(/Hello!/)
})
it('should render the page with same dynamic import', async () => {
const html = await renderViaHTTP(appPort, '/dynamic-two')
expect(html).toMatch(/Hello!/)
})
it('should render 404', async () => {
const html = await renderViaHTTP(appPort, '/404')
expect(html).toMatch(/This page could not be found/)
})
it('should render 404 for /_next/static', async () => {
const html = await renderViaHTTP(appPort, '/_next/static')
expect(html).toMatch(/This page could not be found/)
})
it('should render an AMP page', async () => {
const html = await renderViaHTTP(appPort, '/some-amp?amp=1')
expect(html).toMatch(/Hi Im an AMP page/)
expect(html).toMatch(/ampproject\.org/)
})
it('should have correct amphtml rel link', async () => {
const html = await renderViaHTTP(appPort, '/some-amp')
expect(html).toMatch(/Hi Im an AMP page/)
expect(html).toMatch(/rel="amphtml" href="\/some-amp\.amp"/)
})
it('should have correct canonical link', async () => {
const html = await renderViaHTTP(appPort, '/some-amp?amp=1')
expect(html).toMatch(/rel="canonical" href="\/some-amp"/)
})
it('should have correct canonical link (auto-export link)', async () => {
const html = await renderViaHTTP(appPort, '/some-amp.amp')
expect(html).toMatch(/rel="canonical" href="\/some-amp"/)
})
it('should render correctly when importing isomorphic-unfetch', async () => {
const url = `http://localhost:${appPort}/fetch`
const res = await fetch(url)
expect(res.status).toBe(200)
const text = await res.text()
expect(text.includes('failed')).toBe(false)
})
it('should render correctly when importing isomorphic-unfetch on the client side', async () => {
const browser = await webdriver(appPort, '/')
try {
const text = await browser
.elementByCss('#fetchlink')
.click()
.waitForElementByCss('.fetch-page')
.elementByCss('#text')
.text()
expect(text).toMatch(/fetch page/)
} finally {
await browser.close()
}
})
it('should render correctly when importing isomorphic-unfetch CJS', async () => {
const url = `http://localhost:${appPort}/fetch-cjs`
const res = await fetch(url)
expect(res.status).toBe(200)
const text = await res.text()
expect(text.includes('failed')).toBe(false)
})
it('should render correctly when importing isomorphic-unfetch CJS on the client side', async () => {
const browser = await webdriver(appPort, '/')
try {
const text = await browser
.elementByCss('#fetchcjslink')
.click()
.waitForElementByCss('.fetch-cjs-page')
.elementByCss('#text')
.text()
expect(text).toMatch(/fetch page/)
} finally {
await browser.close()
}
})
it('should not have combined client-side chunks', () => {
expect(readdirSync(chunksDir).length).toBeGreaterThanOrEqual(2)
const pageFile = getPageFileFromBuildManifest(appDir, '/')
expect(
readFileSync(join(__dirname, '..', '.next', pageFile), 'utf8')
).not.toContain('Hello!')
})
it('should not output _app.js and _document.js to serverless build', () => {
expect(existsSync(join(serverlessDir, '_app.js'))).toBeFalsy()
expect(existsSync(join(serverlessDir, '_document.js'))).toBeFalsy()
})
it('should replace static pages with HTML files', async () => {
const pages = ['/abc', '/dynamic', '/dynamic-two', '/some-amp']
for (const page of pages) {
const file = getPageFileFromPagesManifest(appDir, page)
expect(file.endsWith('.html')).toBe(true)
}
})
it('should not replace non-static pages with HTML files', async () => {
const pages = ['/fetch', '/_error']
for (const page of pages) {
const file = getPageFileFromPagesManifest(appDir, page)
expect(file.endsWith('.js')).toBe(true)
}
})
it('should reply on API request successfully', async () => {
const content = await renderViaHTTP(appPort, '/api/hello')
expect(content).toMatch(/hello world/)
})
it('should reply on dynamic API request successfully', async () => {
const result = await renderViaHTTP(appPort, '/api/posts/post-1')
const { id } = JSON.parse(result)
expect(id).toBe('post-1')
})
it('should reply on dynamic API request successfully with query parameters', async () => {
const result = await renderViaHTTP(appPort, '/api/posts/post-1?param=val')
const { id, param } = JSON.parse(result)
expect(id).toBe('post-1')
expect(param).toBe('val')
})
it('should reply on dynamic API index request successfully', async () => {
const result = await renderViaHTTP(appPort, '/api/dynamic/post-1')
const { path } = JSON.parse(result)
expect(path).toBe('post-1')
})
it('should reply on dynamic API index request successfully with query parameters', async () => {
const result = await renderViaHTTP(appPort, '/api/dynamic/post-1?param=val')
const { path, param } = JSON.parse(result)
expect(path).toBe('post-1')
expect(param).toBe('val')
})
it('should reply with redirect on API request with trailing slash', async () => {
const res = await fetchViaHTTP(
appPort,
'/api/hello/',
{},
{ redirect: 'manual' }
)
expect(res.status).toBe(308)
expect(res.headers.get('location')).toBe(
`http://localhost:${appPort}/api/hello`
)
})
it('should reply on API request with trailing slash successfully', async () => {
const content = await renderViaHTTP(appPort, '/api/hello/')
expect(content).toMatch(/hello world/)
})
it('should have the correct query string for a dynamic route', async () => {
const paramRaw = 'test % 123'
const param = encodeURIComponent(paramRaw)
const html = await renderViaHTTP(appPort, `/dr/${param}`)
const $ = cheerio.load(html)
const data = JSON.parse($('#__NEXT_DATA__').html())
expect(data.query).toEqual({ slug: paramRaw })
})
it('should have the correct query string for a now route', async () => {
const paramRaw = 'test % 123'
const html = await fetchViaHTTP(appPort, `/dr/[slug]`, '', {
headers: {
'x-now-route-matches': qs.stringify({
1: encodeURIComponent(paramRaw),
}),
},
}).then((res) => res.text())
const $ = cheerio.load(html)
const data = JSON.parse($('#__NEXT_DATA__').html())
expect(data.query).toEqual({ slug: paramRaw })
})
it('should have the correct query string for a now route with invalid matches but correct path', async () => {
const paramRaw = 'dr/first'
const html = await fetchViaHTTP(appPort, `/dr/first`, '', {
headers: {
'x-now-route-matches': qs.stringify({
1: encodeURIComponent(paramRaw),
}),
},
}).then((res) => res.text())
const $ = cheerio.load(html)
const data = JSON.parse($('#__NEXT_DATA__').html())
expect(data.query).toEqual({ slug: 'first' })
})
it('should have the correct query string for a catch all now route', async () => {
const paramRaw = ['nested % 1', 'nested/2']
const html = await fetchViaHTTP(appPort, `/catchall/[...slug]`, '', {
headers: {
'x-now-route-matches': qs.stringify({
1: paramRaw.map((e) => encodeURIComponent(e)).join('/'),
}),
},
}).then((res) => res.text())
const $ = cheerio.load(html)
const data = JSON.parse($('#__NEXT_DATA__').html())
expect(data.query).toEqual({ slug: paramRaw })
})
it('should log error in API route correctly', async () => {
await renderViaHTTP(appPort, '/api/top-level-error')
await waitFor(1000)
expect(stderr).toContain('top-level-oops')
})
describe('With basic usage', () => {
it('should allow etag header support', async () => {
const url = `http://localhost:${appPort}/`
const etag = (await fetch(url)).headers.get('ETag')
const headers = { 'If-None-Match': etag }
const res2 = await fetch(url, { headers })
expect(res2.status).toBe(304)
})
it('should set Content-Length header', async () => {
const url = `http://localhost:${appPort}`
const res = await fetch(url)
expect(res.headers.get('Content-Length')).toBeDefined()
})
})
})
/* eslint-env jest */
import { join } from 'path'
import fs from 'fs-extra'
import {
renderViaHTTP,
findPort,
......@@ -27,8 +26,6 @@ function runTests(dev) {
})
}
const nextConfig = join(appDir, 'next.config.js')
describe('Dynamic Routing', () => {
describe('dev mode', () => {
beforeAll(async () => {
......@@ -42,11 +39,6 @@ describe('Dynamic Routing', () => {
describe('production mode', () => {
beforeAll(async () => {
const curConfig = await fs.readFile(nextConfig, 'utf8')
if (curConfig.includes('target')) {
await fs.remove(nextConfig)
}
await nextBuild(appDir)
appPort = await findPort()
......@@ -56,20 +48,4 @@ describe('Dynamic Routing', () => {
runTests()
})
describe.skip('serverless production mode', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfig,
`module.exports = { target: 'serverless' }`
)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))
runTests()
})
})
......@@ -2,7 +2,6 @@
import webdriver from 'next-webdriver'
import { join } from 'path'
import fs from 'fs-extra'
import {
renderViaHTTP,
findPort,
......@@ -76,8 +75,6 @@ function runTests() {
})
}
const nextConfig = join(appDir, 'next.config.js')
describe('Dynamic Routing', () => {
describe('dev mode', () => {
beforeAll(async () => {
......@@ -91,7 +88,6 @@ describe('Dynamic Routing', () => {
describe('production mode', () => {
beforeAll(async () => {
await fs.remove(nextConfig)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
......@@ -100,23 +96,4 @@ describe('Dynamic Routing', () => {
runTests()
})
describe('SSR production mode', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfig,
`
module.exports = {
target: 'serverless'
}
`
)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))
runTests()
})
})
/* eslint-env jest */
import fs from 'fs-extra'
import { join } from 'path'
import {
killApp,
......@@ -12,7 +11,6 @@ import {
} from 'next-test-utils'
const appDir = join(__dirname, '../')
const nextConfig = join(appDir, 'next.config.js')
let appPort
let app
......@@ -45,33 +43,6 @@ describe('Custom 404 Page for static site generation with dynamic routes', () =>
runTests('server')
})
describe.skip('serverless mode', () => {
afterAll(async () => {
await fs.remove(nextConfig)
await killApp(app)
})
it('should build successfully', async () => {
await fs.writeFile(
nextConfig,
`
module.exports = { target: 'experimental-serverless-trace' }
`
)
const { code } = await nextBuild(appDir, [], {
stderr: true,
stdout: true,
})
expect(code).toBe(0)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
runTests('serverless')
})
describe('dev mode', () => {
beforeAll(async () => {
appPort = await findPort()
......
module.exports = {
target: 'serverless',
}
import ssrPrepass from 'react-ssr-prepass'
export default () => <p>hello {ssrPrepass && 'world'}</p>
/* eslint-env jest */
import { join } from 'path'
import {
killApp,
findPort,
nextStart,
nextBuild,
renderViaHTTP,
} from 'next-test-utils'
const appDir = join(__dirname, '../')
let appPort
let app
describe('SSR Prepass', () => {
beforeAll(async () => {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))
it('should not externalize when used outside Next.js', async () => {
const html = await renderViaHTTP(appPort, '/')
expect(html).toMatch(/hello.*?world/)
})
})
......@@ -35,23 +35,6 @@ describe('Static 404 page', () => {
expect(html).toContain('This page could not be found')
})
it('should export 404 page without custom _error (serverless)', async () => {
await fs.writeFile(
nextConfig,
`
module.exports = {
target: 'experimental-serverless-trace'
}
`
)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
const html = await renderViaHTTP(appPort, '/non-existent')
await killApp(app)
expect(html).toContain('This page could not be found')
})
it('should not export 404 page with custom _error GIP', async () => {
await fs.writeFile(
errorPage,
......
/* eslint-env jest */
import path from 'path'
import fs from 'fs-extra'
import webdriver from 'next-webdriver'
import {
launchApp,
......@@ -13,7 +12,6 @@ import {
} from 'next-test-utils'
const appDir = path.join(__dirname, '..')
const nextConfigPath = path.join(appDir, 'next.config.js')
let app
let appPort
......@@ -51,21 +49,4 @@ describe('Static Page Name', () => {
afterAll(() => killApp(app))
runTests()
})
describe.skip('serverless mode', () => {
beforeAll(async () => {
appPort = await findPort()
await fs.writeFile(
nextConfigPath,
'module.exports = { target: "serverless" }'
)
await nextBuild(appDir)
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
await fs.remove(nextConfigPath)
})
runTests()
})
})
const { PHASE_PRODUCTION_BUILD } = require('next/constants')
module.exports = phase => {
if (phase === PHASE_PRODUCTION_BUILD) {
return {
target: 'experimental-serverless-trace',
}
}
return {}
}
......@@ -284,32 +284,6 @@ describe('Telemetry CLI', () => {
expect(event).toMatch(/"hasBabelConfig": true/)
})
it('cli session: next config with target', async () => {
await fs.rename(
path.join(appDir, 'next.config.target'),
path.join(appDir, 'next.config.js')
)
const { stderr } = await runNextCommand(['build', appDir], {
stderr: true,
env: {
NEXT_TELEMETRY_DEBUG: 1,
},
})
await fs.rename(
path.join(appDir, 'next.config.js'),
path.join(appDir, 'next.config.target')
)
const event = /NEXT_CLI_SESSION_STARTED[\s\S]+?{([\s\S]+?)}/
.exec(stderr)
.pop()
expect(event).toMatch(/"hasNextConfig": true/)
expect(event).toMatch(/"buildTarget": "experimental-serverless-trace"/)
expect(event).toMatch(/"hasWebpackConfig": false/)
expect(event).toMatch(/"hasBabelConfig": false/)
})
it('cli session: next config with webpack', async () => {
await fs.rename(
path.join(appDir, 'next.config.webpack'),
......
import { nextBuild } from 'next-test-utils'
import path from 'path'
describe.skip('Middleware is not allowed when using serverless target', () => {
it('fails to build', async () => {
const { code, stderr } = await nextBuild(
path.resolve(__dirname, './app'),
undefined,
{ ignoreFail: true, stderr: true }
)
expect(code).toEqual(1)
expect(stderr).toContain('MiddlewareInServerlessTargetError')
})
})
......@@ -74,15 +74,7 @@ describe('config', () => {
PHASE_DEVELOPMENT_SERVER,
join(__dirname, '_resolvedata', 'invalid-target')
)
}).rejects.toThrow(/Specified target is invalid/)
})
it('Should pass when a valid target is provided', async () => {
const config = await loadConfig(
PHASE_DEVELOPMENT_SERVER,
join(__dirname, '_resolvedata', 'valid-target')
)
expect(config.target).toBe('serverless')
}).rejects.toThrow(/The "target" property is no longer supported/)
})
it('Should throw an error when next.config.js is not present', async () => {
......
......@@ -52,18 +52,18 @@ describe('normalizePagePath', () => {
describe('getPagePath', () => {
it('Should not append /index to the / page', () => {
expect(() => getPagePath('/', distDir, false)).toThrow(
expect(() => getPagePath('/', distDir)).toThrow(
'Cannot find module for page: /'
)
})
it('Should prepend / when a page does not have it', () => {
const pagePath = getPagePath('_error', distDir, false)
const pagePath = getPagePath('_error', distDir)
expect(pagePath).toBe(join(pathToBundles, `${sep}_error.js`))
})
it('Should throw with paths containing ../', () => {
expect(() => getPagePath('/../../package.json', distDir, false)).toThrow()
expect(() => getPagePath('/../../package.json', distDir)).toThrow()
})
})
......
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