Unverified Commit 89cd2058 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

[turbopack] Move generation of `cacheLife` types out of the webpack plugin and…

[turbopack] Move generation of `cacheLife` types out of the webpack plugin and into the dev bundler directly (#85539)

Ensure that users configuring custom `cacheLife` configs with Typescript get support with turbopack

This follows the same pattern as typed-routes, so generation can happen during `next type-gen` and also via the dev bundler.

Also this fixes a subtle bug in the timestamp formatting where we believed months had 30 or 31 _weeks_ instead of 30 or 31 _days_s.

Beyond the unit tests, i also tried out adding a `cacheLife` config to the `with-turbopack` example and observed that the types were generated and vscode was able to autocomplete my custom config type.

Fixes NAR-458
parent 920b2c00
......@@ -2155,7 +2155,6 @@ export default async function getBaseWebpackConfig(
dev,
isEdgeServer,
pageExtensions: config.pageExtensions,
cacheLifeConfig: config.cacheLife,
originalRewrites,
originalRedirects,
}),
......
......@@ -11,7 +11,6 @@ describe('next-types-plugin', () => {
dev: false,
isEdgeServer: false,
pageExtensions: ['tsx', 'ts', 'jsx', 'js'],
cacheLifeConfig: undefined,
originalRewrites: undefined,
originalRedirects: undefined,
})
......@@ -39,7 +38,6 @@ describe('next-types-plugin', () => {
dev: false,
isEdgeServer: false,
pageExtensions: ['tsx', 'ts', 'jsx', 'js'],
cacheLifeConfig: undefined,
originalRewrites: undefined,
originalRedirects: undefined,
})
......@@ -59,7 +57,7 @@ describe('next-types-plugin', () => {
dev: false,
isEdgeServer: false,
pageExtensions: ['tsx', 'ts', 'jsx', 'js'],
cacheLifeConfig: undefined,
originalRewrites: undefined,
originalRedirects: undefined,
})
......
// DO NOT ADD NEW FEATURES TO THIS PLUGIN
// DOING SO PREVENTS THEM FROM WORKING FOR TURBOPACK USERS.
// FOLLOW THE PATTERN OF TYPED-ROUTES AND CACHE-LIFE GENERATION
import type { Rewrite, Redirect } from '../../../../lib/load-custom-routes'
import fs from 'fs/promises'
......@@ -14,7 +18,6 @@ import { normalizeAppPath } from '../../../../shared/lib/router/utils/app-paths'
import { getPageFromPath } from '../../../entries'
import type { PageExtensions } from '../../../page-extensions-type'
import { getProxiedPluginState } from '../../../build-context'
import type { CacheLife } from '../../../../server/use-cache/cache-life'
const PLUGIN_NAME = 'NextTypesPlugin'
......@@ -31,7 +34,6 @@ interface Options {
dev: boolean
isEdgeServer: boolean
pageExtensions: PageExtensions
cacheLifeConfig: undefined | { [profile: string]: CacheLife }
originalRewrites: Rewrites | undefined
originalRedirects: Redirect[] | undefined
}
......@@ -283,63 +285,6 @@ function formatRouteToRouteType(route: string) {
}
}
function formatTimespan(seconds: number): string {
if (seconds > 0) {
if (seconds === 18748800) {
return '1 month'
}
if (seconds === 18144000) {
return '1 month'
}
if (seconds === 604800) {
return '1 week'
}
if (seconds === 86400) {
return '1 day'
}
if (seconds === 3600) {
return '1 hour'
}
if (seconds === 60) {
return '1 minute'
}
if (seconds % 18748800 === 0) {
return seconds / 18748800 + ' months'
}
if (seconds % 18144000 === 0) {
return seconds / 18144000 + ' months'
}
if (seconds % 604800 === 0) {
return seconds / 604800 + ' weeks'
}
if (seconds % 86400 === 0) {
return seconds / 86400 + ' days'
}
if (seconds % 3600 === 0) {
return seconds / 3600 + ' hours'
}
if (seconds % 60 === 0) {
return seconds / 60 + ' minutes'
}
}
return seconds + ' seconds'
}
function formatTimespanWithSeconds(seconds: undefined | number): string {
if (seconds === undefined) {
return 'default'
}
if (seconds >= 0xfffffffe) {
return 'never'
}
const text = seconds + ' seconds'
const descriptive = formatTimespan(seconds)
if (descriptive === text) {
return text
}
return text + ' (' + descriptive + ')'
}
function getRootParamsFromLayouts(layouts: Record<string, string[]>) {
// Sort layouts by depth (descending)
const sortedLayouts = Object.entries(layouts).sort(
......@@ -426,127 +371,6 @@ function createServerDefinitions() {
`
}
function createCustomCacheLifeDefinitions(cacheLife: {
[profile: string]: CacheLife
}) {
let overloads = ''
const profileNames = Object.keys(cacheLife)
for (let i = 0; i < profileNames.length; i++) {
const profileName = profileNames[i]
const profile = cacheLife[profileName]
if (typeof profile !== 'object' || profile === null) {
continue
}
let description = ''
if (profile.stale === undefined) {
description += `
* This cache may be stale on clients for the default stale time of the scope before checking with the server.`
} else if (profile.stale >= 0xfffffffe) {
description += `
* This cache may be stale on clients indefinitely before checking with the server.`
} else {
description += `
* This cache may be stale on clients for ${formatTimespan(profile.stale)} before checking with the server.`
}
if (
profile.revalidate !== undefined &&
profile.expire !== undefined &&
profile.revalidate >= profile.expire
) {
description += `
* This cache will expire after ${formatTimespan(profile.expire)}. The next request will recompute it.`
} else {
if (profile.revalidate === undefined) {
description += `
* It will inherit the default revalidate time of its scope since it does not define its own.`
} else if (profile.revalidate >= 0xfffffffe) {
// Nothing to mention.
} else {
description += `
* If the server receives a new request after ${formatTimespan(profile.revalidate)}, start revalidating new values in the background.`
}
if (profile.expire === undefined) {
description += `
* It will inherit the default expiration time of its scope since it does not define its own.`
} else if (profile.expire >= 0xfffffffe) {
description += `
* It lives for the maximum age of the server cache. If this entry has no traffic for a while, it may serve an old value the next request.`
} else {
description += `
* If this entry has no traffic for ${formatTimespan(profile.expire)} it will expire. The next request will recompute it.`
}
}
overloads += `
/**
* Cache this \`"use cache"\` for a timespan defined by the \`${JSON.stringify(profileName)}\` profile.
* \`\`\`
* stale: ${formatTimespanWithSeconds(profile.stale)}
* revalidate: ${formatTimespanWithSeconds(profile.revalidate)}
* expire: ${formatTimespanWithSeconds(profile.expire)}
* \`\`\`
* ${description}
*/
export function cacheLife(profile: ${JSON.stringify(profileName)}): void
`
}
overloads += `
/**
* Cache this \`"use cache"\` using a custom timespan.
* \`\`\`
* stale: ... // seconds
* revalidate: ... // seconds
* expire: ... // seconds
* \`\`\`
*
* This is similar to Cache-Control: max-age=\`stale\`,s-max-age=\`revalidate\`,stale-while-revalidate=\`expire-revalidate\`
*
* If a value is left out, the lowest of other cacheLife() calls or the default, is used instead.
*/
export function cacheLife(profile: {
/**
* This cache may be stale on clients for ... seconds before checking with the server.
*/
stale?: number,
/**
* If the server receives a new request after ... seconds, start revalidating new values in the background.
*/
revalidate?: number,
/**
* If this entry has no traffic for ... seconds it will expire. The next request will recompute it.
*/
expire?: number
}): void
`
// Redefine the cacheLife() accepted arguments.
return `// Type definitions for Next.js cacheLife configs
declare module 'next/cache' {
export { unstable_cache } from 'next/dist/server/web/spec-extension/unstable-cache'
export {
updateTag,
revalidateTag,
revalidatePath,
refresh,
} from 'next/dist/server/web/spec-extension/revalidate'
export { unstable_noStore } from 'next/dist/server/web/spec-extension/unstable-no-store'
${overloads}
import { cacheTag } from 'next/dist/server/use-cache/cache-tag'
export { cacheTag }
export const unstable_cacheTag: typeof cacheTag
export const unstable_cacheLife: typeof cacheLife
}
`
}
const appTypesBasePath = path.join('types', 'app')
export class NextTypesPlugin {
......@@ -557,7 +381,6 @@ export class NextTypesPlugin {
isEdgeServer: boolean
pageExtensions: string[]
pagesDir: string
cacheLifeConfig: undefined | { [profile: string]: CacheLife }
distDirAbsolutePath: string
constructor(options: Options) {
......@@ -568,7 +391,6 @@ export class NextTypesPlugin {
this.isEdgeServer = options.isEdgeServer
this.pageExtensions = options.pageExtensions
this.pagesDir = path.join(this.appDir, '..', 'pages')
this.cacheLifeConfig = options.cacheLifeConfig
this.distDirAbsolutePath = path.join(this.dir, this.distDir)
}
......@@ -829,20 +651,6 @@ export class NextTypesPlugin {
) as unknown as webpack.sources.RawSource
)
if (this.cacheLifeConfig) {
const cacheLifeAssetPath = path.join(
assetDirRelative,
'types/cache-life.d.ts'
)
compilation.emitAsset(
cacheLifeAssetPath,
new sources.RawSource(
createCustomCacheLifeDefinitions(this.cacheLifeConfig)
) as unknown as webpack.sources.RawSource
)
}
callback()
}
)
......
......@@ -30,6 +30,7 @@ import {
writeRouteTypesManifest,
writeValidatorFile,
} from '../server/lib/router-utils/route-types-utils'
import { writeCacheLifeTypes } from '../server/lib/router-utils/cache-life-type-utils'
import { createValidFileMatcher } from '../server/lib/find-page-file'
export type NextTypegenOptions = {
......@@ -167,7 +168,11 @@ const nextTypegen = async (
await writeValidatorFile(routeTypesManifest, validatorFilePath)
console.log('✓ Route types generated successfully')
// Generate cache-life types if cacheLife config exists
const cacheLifeFilePath = join(distDir, 'types', 'cache-life.d.ts')
writeCacheLifeTypes(nextConfig.cacheLife, cacheLifeFilePath)
console.log('✓ Types generated successfully')
}
export { nextTypegen }
import type { CacheLife } from '../../use-cache/cache-life'
import fs from 'fs'
import path from 'path'
const MINUTE_IN_SECONDS = 60
const HOUR_IN_SECONDS = MINUTE_IN_SECONDS * 60 // nevermind leap seconds
const DAY_IN_SECONDS = HOUR_IN_SECONDS * 24
const WEEK_IN_SECONDS = DAY_IN_SECONDS * 7
// Nevermind leap years, or you know, July
const MONTH_30_DAYS_IN_SECONDS = DAY_IN_SECONDS * 30
function formatTimespan(seconds: number): string {
if (seconds > 0) {
if (seconds === MONTH_30_DAYS_IN_SECONDS) {
return '1 month'
}
if (seconds === WEEK_IN_SECONDS) {
return '1 week'
}
if (seconds === DAY_IN_SECONDS) {
return '1 day'
}
if (seconds === HOUR_IN_SECONDS) {
return '1 hour'
}
if (seconds === MINUTE_IN_SECONDS) {
return '1 minute'
}
if (seconds % MONTH_30_DAYS_IN_SECONDS === 0) {
return seconds / MONTH_30_DAYS_IN_SECONDS + ' months'
}
if (seconds % 18144000 === 0) {
return seconds / 18144000 + ' months'
}
if (seconds % WEEK_IN_SECONDS === 0) {
return seconds / WEEK_IN_SECONDS + ' weeks'
}
if (seconds % DAY_IN_SECONDS === 0) {
return seconds / DAY_IN_SECONDS + ' days'
}
if (seconds % HOUR_IN_SECONDS === 0) {
return seconds / HOUR_IN_SECONDS + ' hours'
}
if (seconds % MINUTE_IN_SECONDS === 0) {
return seconds / MINUTE_IN_SECONDS + ' minutes'
}
}
return seconds + ' seconds'
}
function formatTimespanWithSeconds(seconds: undefined | number): string {
if (seconds === undefined) {
return 'default'
}
if (seconds >= 0xfffffffe) {
return 'never'
}
const text = seconds + ' seconds'
const descriptive = formatTimespan(seconds)
if (descriptive === text) {
return text
}
return text + ' (' + descriptive + ')'
}
/**
* Generates TypeScript type definitions for custom cacheLife profiles.
* This creates overloaded function signatures for the cacheLife() function
* that provide autocomplete and documentation for each profile.
*/
export function generateCacheLifeTypes(cacheLife: {
[profile: string]: CacheLife
}): string {
let overloads = ''
const profileNames = Object.keys(cacheLife)
for (let i = 0; i < profileNames.length; i++) {
const profileName = profileNames[i]
const profile = cacheLife[profileName]
if (typeof profile !== 'object' || profile === null) {
continue
}
let description = ''
if (profile.stale === undefined) {
description += `
* This cache may be stale on clients for the default stale time of the scope before checking with the server.`
} else if (profile.stale >= 0xfffffffe) {
description += `
* This cache may be stale on clients indefinitely before checking with the server.`
} else {
description += `
* This cache may be stale on clients for ${formatTimespan(profile.stale)} before checking with the server.`
}
if (
profile.revalidate !== undefined &&
profile.expire !== undefined &&
profile.revalidate >= profile.expire
) {
description += `
* This cache will expire after ${formatTimespan(profile.expire)}. The next request will recompute it.`
} else {
if (profile.revalidate === undefined) {
description += `
* It will inherit the default revalidate time of its scope since it does not define its own.`
} else if (profile.revalidate >= 0xfffffffe) {
// Nothing to mention.
} else {
description += `
* If the server receives a new request after ${formatTimespan(profile.revalidate)}, start revalidating new values in the background.`
}
if (profile.expire === undefined) {
description += `
* It will inherit the default expiration time of its scope since it does not define its own.`
} else if (profile.expire >= 0xfffffffe) {
description += `
* It lives for the maximum age of the server cache. If this entry has no traffic for a while, it may serve an old value the next request.`
} else {
description += `
* If this entry has no traffic for ${formatTimespan(profile.expire)} it will expire. The next request will recompute it.`
}
}
overloads += `
/**
* Cache this \`"use cache"\` for a timespan defined by the \`${JSON.stringify(profileName)}\` profile.
* \`\`\`
* stale: ${formatTimespanWithSeconds(profile.stale)}
* revalidate: ${formatTimespanWithSeconds(profile.revalidate)}
* expire: ${formatTimespanWithSeconds(profile.expire)}
* \`\`\`
* ${description}
*/
export function cacheLife(profile: ${JSON.stringify(profileName)}): void
`
}
overloads += `
/**
* Cache this \`"use cache"\` using a custom timespan.
* \`\`\`
* stale: ... // seconds
* revalidate: ... // seconds
* expire: ... // seconds
* \`\`\`
*
* This is similar to Cache-Control: max-age=\`stale\`,s-max-age=\`revalidate\`,stale-while-revalidate=\`expire-revalidate\`
*
* If a value is left out, the lowest of other cacheLife() calls or the default, is used instead.
*/
export function cacheLife(profile: {
/**
* This cache may be stale on clients for ... seconds before checking with the server.
*/
stale?: number,
/**
* If the server receives a new request after ... seconds, start revalidating new values in the background.
*/
revalidate?: number,
/**
* If this entry has no traffic for ... seconds it will expire. The next request will recompute it.
*/
expire?: number
}): void
`
// Redefine the cacheLife() accepted arguments.
return `// Type definitions for Next.js cacheLife configs
declare module 'next/cache' {
export { unstable_cache } from 'next/dist/server/web/spec-extension/unstable-cache'
export {
updateTag,
revalidateTag,
revalidatePath,
refresh,
} from 'next/dist/server/web/spec-extension/revalidate'
export { unstable_noStore } from 'next/dist/server/web/spec-extension/unstable-no-store'
${overloads}
import { cacheTag } from 'next/dist/server/use-cache/cache-tag'
export { cacheTag }
export const unstable_cacheTag: typeof cacheTag
export const unstable_cacheLife: typeof cacheLife
}
`
}
/**
* Writes cache-life type definitions to a file if cacheLifeConfig exists.
* This is used by both the CLI (next type-gen) and dev server to generate
* cache-life.d.ts in the types directory.
*/
export function writeCacheLifeTypes(
cacheLifeConfig: undefined | { [profile: string]: CacheLife },
filePath: string
) {
if (!cacheLifeConfig || Object.keys(cacheLifeConfig).length === 0) {
return
}
const dirname = path.dirname(filePath)
if (!fs.existsSync(dirname)) {
fs.mkdirSync(dirname, { recursive: true })
}
const content = generateCacheLifeTypes(cacheLifeConfig)
fs.writeFileSync(filePath, content)
}
......@@ -85,6 +85,7 @@ import {
writeRouteTypesManifest,
writeValidatorFile,
} from './route-types-utils'
import { writeCacheLifeTypes } from './cache-life-type-utils'
import { isParallelRouteSegment } from '../../../shared/lib/segment'
import { ensureLeadingSlash } from '../../../shared/lib/page-path/ensure-leading-slash'
import { Lockfile } from '../../../build/lockfile'
......@@ -1165,6 +1166,10 @@ async function startWatcher(
opts.nextConfig
)
await writeValidatorFile(routeTypesManifest, validatorFilePath)
// Generate cache-life types if cacheLife config exists
const cacheLifeFilePath = path.join(distTypesDir, 'cache-life.d.ts')
writeCacheLifeTypes(opts.nextConfig.cacheLife, cacheLifeFilePath)
}
if (!resolved) {
......
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