Skip to content

Commit

Permalink
Merge branch 'canary' of github.com:vercel/next.js into 05-02-Change_…
Browse files Browse the repository at this point in the history
…create-next-app_defaults
  • Loading branch information
timneutkens committed May 2, 2023
2 parents 9c5344b + f3068a5 commit 6641f20
Show file tree
Hide file tree
Showing 50 changed files with 749 additions and 130 deletions.
4 changes: 2 additions & 2 deletions docs/api-reference/create-next-app.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ Options:

Initialize with ESLint config.

--experimental-app
--app-dir

Initialize as a `app/` directory project.
Initialize as an `app/` directory project.

--src-dir

Expand Down
4 changes: 4 additions & 0 deletions packages/next-swc/crates/napi/src/app_structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ struct ComponentsForJs {
loading: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
template: Option<String>,
#[serde(skip_serializing_if = "Option::is_none", rename = "not-found")]
not_found: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
default: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -127,6 +129,7 @@ async fn prepare_components_for_js(
error,
loading,
template,
not_found,
default,
route,
metadata,
Expand All @@ -147,6 +150,7 @@ async fn prepare_components_for_js(
add(&mut result.error, project_path, error).await?;
add(&mut result.loading, project_path, loading).await?;
add(&mut result.template, project_path, template).await?;
add(&mut result.not_found, project_path, not_found).await?;
add(&mut result.default, project_path, default).await?;
add(&mut result.route, project_path, route).await?;
async fn add_meta<'a>(
Expand Down
4 changes: 3 additions & 1 deletion packages/next-swc/crates/next-core/src/app_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -856,15 +856,17 @@ import {}, {{ chunks as {} }} from "COMPONENT_{}";
layout,
loading,
template,
not_found,
metadata,
route: _,
} = &*components.await?;
write_component(state, "page", *page)?;
write_component(state, "default", *default)?;
write_component(state, "defaultPage", *default)?;
write_component(state, "error", *error)?;
write_component(state, "layout", *layout)?;
write_component(state, "loading", *loading)?;
write_component(state, "template", *template)?;
write_component(state, "not-found", *not_found)?;
write_metadata(state, metadata)?;
write!(state.loader_tree_code, "}}]")?;
Ok(())
Expand Down
5 changes: 5 additions & 0 deletions packages/next-swc/crates/next-core/src/app_structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ pub struct Components {
#[serde(skip_serializing_if = "Option::is_none")]
pub template: Option<FileSystemPathVc>,
#[serde(skip_serializing_if = "Option::is_none")]
pub not_found: Option<FileSystemPathVc>,
#[serde(skip_serializing_if = "Option::is_none")]
pub default: Option<FileSystemPathVc>,
#[serde(skip_serializing_if = "Option::is_none")]
pub route: Option<FileSystemPathVc>,
Expand All @@ -51,6 +53,7 @@ impl Components {
error: self.error,
loading: self.loading,
template: self.template,
not_found: self.not_found,
default: None,
route: None,
metadata: self.metadata.clone(),
Expand All @@ -64,6 +67,7 @@ impl Components {
error: a.error.or(b.error),
loading: a.loading.or(b.loading),
template: a.template.or(b.template),
not_found: a.not_found.or(b.not_found),
default: a.default.or(b.default),
route: a.default.or(b.route),
metadata: Metadata::merge(&a.metadata, &b.metadata),
Expand Down Expand Up @@ -321,6 +325,7 @@ async fn get_directory_tree(
"error" => components.error = Some(file),
"loading" => components.loading = Some(file),
"template" => components.template = Some(file),
"not-found" => components.not_found = Some(file),
"default" => components.default = Some(file),
"route" => components.route = Some(file),
"manifest" => {
Expand Down
12 changes: 11 additions & 1 deletion packages/next-swc/crates/next-core/src/next_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,17 @@ impl NextConfigVc {
.await?
.env
.iter()
.map(|(k, v)| (k.clone(), v.to_string()))
.map(|(k, v)| {
(
k.clone(),
if let JsonValue::String(s) = v {
// A string value is kept, calling `to_string` would wrap in to quotes.
s.clone()
} else {
v.to_string()
},
)
})
.collect();

Ok(EnvMapVc::cell(env))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
STRING_ENV_VAR_FROM_DOT_ENV="Hello World"
NUMBER_ENV_VAR_FROM_DOT_ENV=123
BOOLEAN_ENV_VAR_FROM_DOT_ENV=true

NEXT_PUBLIC_STRING_ENV_VAR_FROM_DOT_ENV="Hello World"
NEXT_PUBLIC_NUMBER_ENV_VAR_FROM_DOT_ENV=123
NEXT_PUBLIC_BOOLEAN_ENV_VAR_FROM_DOT_ENV=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
env: {
STRING_ENV_VAR_FROM_CONFIG: 'Hello World',
NUMBER_ENV_VAR_FROM_CONFIG: 123,
BOOLEAN_ENV_VAR_FROM_CONFIG: true,
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { useEffect } from 'react'

export default function Page() {
useEffect(() => {
// Only run on client
import('@turbo/pack-test-harness').then(runTests)
})
}

function runTests() {
it('should support env vars from config', () => {
expect(process.env.STRING_ENV_VAR_FROM_CONFIG).toBe('Hello World')
expect(process.env.BOOLEAN_ENV_VAR_FROM_CONFIG).toBe('true')
expect(process.env.NUMBER_ENV_VAR_FROM_CONFIG).toBe('123')
})

it('should support env vars from .env', () => {
expect(process.env.NEXT_PUBLIC_STRING_ENV_VAR_FROM_DOT_ENV).toBe(
'Hello World'
)
expect(process.env.NEXT_PUBLIC_BOOLEAN_ENV_VAR_FROM_DOT_ENV).toBe('true')
expect(process.env.NEXT_PUBLIC_NUMBER_ENV_VAR_FROM_DOT_ENV).toBe('123')
})

it('should not support env vars from .env without NEXT_PUBLIC prefix', () => {
expect(process.env.STRING_ENV_VAR_FROM_DOT_ENV).toBeUndefined()
expect(process.env.BOOLEAN_ENV_VAR_FROM_DOT_ENV).toBeUndefined()
expect(process.env.NUMBER_ENV_VAR_FROM_DOT_ENV).toBeUndefined()
})
}
3 changes: 3 additions & 0 deletions packages/next/cache.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { unstable_cache } from 'next/dist/server/web/spec-extension/unstable-cache'
export { unstable_revalidatePath } from 'next/dist/server/web/spec-extension/unstable-revalidate-path'
export { unstable_revalidateTag } from 'next/dist/server/web/spec-extension/unstable-revalidate-tag'
19 changes: 19 additions & 0 deletions packages/next/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const cacheExports = {
unstable_cache: require('next/dist/server/web/spec-extension/unstable-cache')
.unstable_cache,
unstable_revalidateTag:
require('next/dist/server/web/spec-extension/unstable-revalidate-tag')
.unstable_revalidateTag,
unstable_revalidatePath:
require('next/dist/server/web/spec-extension/unstable-revalidate-path')
.unstable_revalidatePath,
}

// https://nodejs.org/api/esm.html#commonjs-namespaces
// When importing CommonJS modules, the module.exports object is provided as the default export
module.exports = cacheExports

// make import { xxx } from 'next/server' work
exports.unstable_cache = cacheExports.unstable_cache
exports.unstable_revalidatePath = cacheExports.unstable_revalidatePath
exports.unstable_revalidateTag = cacheExports.unstable_revalidateTag
1 change: 1 addition & 0 deletions packages/next/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/// <reference path="./dist/styled-jsx/types/global.d.ts" />
/// <reference path="./amp.d.ts" />
/// <reference path="./app.d.ts" />
/// <reference path="./cache.d.ts" />
/// <reference path="./config.d.ts" />
/// <reference path="./document.d.ts" />
/// <reference path="./dynamic.d.ts" />
Expand Down
2 changes: 2 additions & 0 deletions packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"client.js",
"client.d.ts",
"compat",
"cache.js",
"cache.d.ts",
"config.js",
"config.d.ts",
"constants.js",
Expand Down
3 changes: 0 additions & 3 deletions packages/next/server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,3 @@ export { userAgent } from 'next/dist/server/web/spec-extension/user-agent'
export { URLPattern } from 'next/dist/compiled/@edge-runtime/primitives/url'
export { ImageResponse } from 'next/dist/server/web/spec-extension/image-response'
export type { ImageResponseOptions } from 'next/dist/compiled/@vercel/og/types'
export { unstable_cache } from 'next/dist/server/web/spec-extension/unstable-cache'
export { unstable_revalidatePath } from 'next/dist/server/web/spec-extension/unstable-revalidate-path'
export { unstable_revalidateTag } from 'next/dist/server/web/spec-extension/unstable-revalidate-tag'
11 changes: 0 additions & 11 deletions packages/next/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@ const serverExports = {
.NextResponse,
ImageResponse: require('next/dist/server/web/spec-extension/image-response')
.ImageResponse,
unstable_cache: require('next/dist/server/web/spec-extension/unstable-cache')
.unstable_cache,
unstable_revalidateTag:
require('next/dist/server/web/spec-extension/unstable-revalidate-tag')
.unstable_revalidateTag,
unstable_revalidatePath:
require('next/dist/server/web/spec-extension/unstable-revalidate-path')
.unstable_revalidatePath,
userAgentFromString: require('next/dist/server/web/spec-extension/user-agent')
.userAgentFromString,
userAgent: require('next/dist/server/web/spec-extension/user-agent')
Expand All @@ -32,9 +24,6 @@ module.exports = serverExports
exports.NextRequest = serverExports.NextRequest
exports.NextResponse = serverExports.NextResponse
exports.ImageResponse = serverExports.ImageResponse
exports.unstable_cache = serverExports.unstable_cache
exports.unstable_revalidatePath = serverExports.unstable_revalidatePath
exports.unstable_revalidateTag = serverExports.unstable_revalidateTag
exports.userAgentFromString = serverExports.userAgentFromString
exports.userAgent = serverExports.userAgent
exports.URLPattern = serverExports.URLPattern
18 changes: 9 additions & 9 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2377,16 +2377,16 @@ export default async function build(
initialHeaders?: SsgRoute['initialHeaders']
} = {}

if (isRouteHandler) {
const exportRouteMeta =
exportConfig.initialPageMetaMap[route] || {}
const exportRouteMeta: {
status?: number
headers?: Record<string, string>
} = exportConfig.initialPageMetaMap[route] || {}

if (exportRouteMeta.status !== 200) {
routeMeta.initialStatus = exportRouteMeta.status
}
if (Object.keys(exportRouteMeta.headers).length) {
routeMeta.initialHeaders = exportRouteMeta.headers
}
if (exportRouteMeta.status !== 200) {
routeMeta.initialStatus = exportRouteMeta.status
}
if (Object.keys(exportRouteMeta.headers || {}).length) {
routeMeta.initialHeaders = exportRouteMeta.headers
}

finalPrerenderRoutes[route] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface StaticGenerationStore {
readonly pathname: string
readonly incrementalCache?: IncrementalCache
readonly isRevalidate?: boolean
readonly isMinimalMode?: boolean
readonly isOnDemandRevalidate?: boolean
readonly isPrerendering?: boolean

Expand All @@ -17,6 +18,7 @@ export interface StaticGenerationStore {
| 'force-no-store'
| 'default-no-store'
| 'only-no-store'

revalidate?: false | number
forceStatic?: boolean
dynamicShouldError?: boolean
Expand All @@ -26,6 +28,8 @@ export interface StaticGenerationStore {
dynamicUsageStack?: string

nextFetchId?: number

tags?: string[]
}

export type StaticGenerationAsyncStorage =
Expand Down
38 changes: 38 additions & 0 deletions packages/next/src/export/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { isAppRouteRoute } from '../lib/is-app-route-route'
import { toNodeHeaders } from '../server/web/utils'
import { RouteModuleLoader } from '../server/future/helpers/module-loader/route-module-loader'
import { NextRequestAdapter } from '../server/web/spec-extension/adapters/next-request'
import * as ciEnvironment from '../telemetry/ci-info'

const envConfig = require('../shared/lib/runtime-config')

Expand Down Expand Up @@ -322,6 +323,12 @@ export default async function exportPage({
fontManifest: optimizeFonts ? requireFontManifest(distDir) : null,
locale: locale as string,
supportsDynamicHTML: false,
...(ciEnvironment.hasNextSupport
? {
isMinimalMode: true,
isRevalidate: true,
}
: {}),
}
}

Expand Down Expand Up @@ -400,6 +407,12 @@ export default async function exportPage({
nextExport: true,
supportsDynamicHTML: false,
incrementalCache: curRenderOpts.incrementalCache,
...(ciEnvironment.hasNextSupport
? {
isMinimalMode: true,
isRevalidate: true,
}
: {}),
},
}

Expand Down Expand Up @@ -429,6 +442,12 @@ export default async function exportPage({

results.fromBuildExportRevalidate = revalidate
const headers = toNodeHeaders(response.headers)
const cacheTags = (context.staticGenerationContext as any)
.fetchTags

if (cacheTags) {
headers['x-next-cache-tags'] = cacheTags
}

if (!headers['content-type'] && body.type) {
headers['content-type'] = body.type
Expand Down Expand Up @@ -481,7 +500,26 @@ export default async function exportPage({
results.fromBuildExportRevalidate = revalidate

if (revalidate !== 0) {
const cacheTags = (curRenderOpts as any).fetchTags
const headers = cacheTags
? {
'x-next-cache-tags': cacheTags,
}
: undefined

if (ciEnvironment.hasNextSupport) {
if (cacheTags) {
results.fromBuildExportMeta = {
headers,
}
}
}

await promises.writeFile(htmlFilepath, html ?? '', 'utf8')
await promises.writeFile(
htmlFilepath.replace(/\.html$/, '.meta'),
JSON.stringify({ headers })
)
await promises.writeFile(
htmlFilepath.replace(/\.html$/, '.rsc'),
flightData
Expand Down
Loading

0 comments on commit 6641f20

Please sign in to comment.