Unverified Commit e4beb85b authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update cache methods for app dir (#49165)

This ensures we match behavior local and deployed and also cleans up
naming of the methods.

---------
parent b7c213fc
export { unstable_cache } from 'next/dist/server/web/spec-extension/unstable-cache' 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 { revalidatePath } from 'next/dist/server/web/spec-extension/revalidate-path'
export { unstable_revalidateTag } from 'next/dist/server/web/spec-extension/unstable-revalidate-tag' export { revalidateTag } from 'next/dist/server/web/spec-extension/revalidate-tag'
const cacheExports = { const cacheExports = {
unstable_cache: require('next/dist/server/web/spec-extension/unstable-cache') unstable_cache: require('next/dist/server/web/spec-extension/unstable-cache')
.unstable_cache, .unstable_cache,
unstable_revalidateTag: revalidateTag: require('next/dist/server/web/spec-extension/revalidate-tag')
require('next/dist/server/web/spec-extension/unstable-revalidate-tag') .revalidateTag,
.unstable_revalidateTag, revalidatePath: require('next/dist/server/web/spec-extension/revalidate-path')
unstable_revalidatePath: .revalidatePath,
require('next/dist/server/web/spec-extension/unstable-revalidate-path')
.unstable_revalidatePath,
} }
// https://nodejs.org/api/esm.html#commonjs-namespaces // https://nodejs.org/api/esm.html#commonjs-namespaces
...@@ -15,5 +13,5 @@ module.exports = cacheExports ...@@ -15,5 +13,5 @@ module.exports = cacheExports
// make import { xxx } from 'next/server' work // make import { xxx } from 'next/server' work
exports.unstable_cache = cacheExports.unstable_cache exports.unstable_cache = cacheExports.unstable_cache
exports.unstable_revalidatePath = cacheExports.unstable_revalidatePath exports.revalidatePath = cacheExports.revalidatePath
exports.unstable_revalidateTag = cacheExports.unstable_revalidateTag exports.revalidateTag = cacheExports.revalidateTag
...@@ -4,7 +4,6 @@ import LRUCache from 'next/dist/compiled/lru-cache' ...@@ -4,7 +4,6 @@ import LRUCache from 'next/dist/compiled/lru-cache'
import { CacheFs } from '../../../shared/lib/utils' import { CacheFs } from '../../../shared/lib/utils'
import path from '../../../shared/lib/isomorphic/path' import path from '../../../shared/lib/isomorphic/path'
import { CachedFetchValue } from '../../response-cache' import { CachedFetchValue } from '../../response-cache'
import { CACHE_ONE_YEAR } from '../../../lib/constants'
type FileSystemCacheContext = Omit< type FileSystemCacheContext = Omit<
CacheHandlerContext, CacheHandlerContext,
...@@ -290,15 +289,17 @@ export default class FileSystemCache implements CacheHandler { ...@@ -290,15 +289,17 @@ export default class FileSystemCache implements CacheHandler {
const innerData = data.value.data const innerData = data.value.data
const derivedTags = getDerivedTags(innerData.tags || []) const derivedTags = getDerivedTags(innerData.tags || [])
const isStale = derivedTags.some((tag) => { const wasRevalidated = derivedTags.some((tag) => {
return ( return (
tagsManifest?.items[tag]?.revalidatedAt && tagsManifest?.items[tag]?.revalidatedAt &&
tagsManifest?.items[tag].revalidatedAt >= tagsManifest?.items[tag].revalidatedAt >=
(data?.lastModified || Date.now()) (data?.lastModified || Date.now())
) )
}) })
if (isStale) { // When revalidate tag is called we don't return
data.lastModified = Date.now() - CACHE_ONE_YEAR // stale data so it's updated right away
if (wasRevalidated) {
data = undefined
} }
} }
......
// This file is for modularized imports for next/server to get fully-treeshaking. // This file is for modularized imports for next/server to get fully-treeshaking.
export { unstable_revalidateTag as default } from '../spec-extension/unstable-revalidate-tag' export { revalidatePath as default } from '../spec-extension/revalidate-path'
// This file is for modularized imports for next/server to get fully-treeshaking. // This file is for modularized imports for next/server to get fully-treeshaking.
export { unstable_revalidatePath as default } from '../spec-extension/unstable-revalidate-path' export { revalidateTag as default } from '../spec-extension/revalidate-tag'
import { revalidateTag } from './revalidate-tag'
export function revalidatePath(path: string) {
return revalidateTag(path)
}
...@@ -3,7 +3,7 @@ import { ...@@ -3,7 +3,7 @@ import {
StaticGenerationStore, StaticGenerationStore,
} from '../../../client/components/static-generation-async-storage' } from '../../../client/components/static-generation-async-storage'
export function unstable_revalidateTag(tag: string) { export function revalidateTag(tag: string) {
const staticGenerationAsyncStorage = ( const staticGenerationAsyncStorage = (
fetch as any fetch as any
).__nextGetStaticStore?.() as undefined | StaticGenerationAsyncStorage ).__nextGetStaticStore?.() as undefined | StaticGenerationAsyncStorage
...@@ -13,7 +13,7 @@ export function unstable_revalidateTag(tag: string) { ...@@ -13,7 +13,7 @@ export function unstable_revalidateTag(tag: string) {
if (!store || !store.incrementalCache) { if (!store || !store.incrementalCache) {
throw new Error( throw new Error(
`Invariant: static generation store missing in unstable_revalidateTag ${tag}` `Invariant: static generation store missing in revalidateTag ${tag}`
) )
} }
......
import { unstable_revalidateTag } from './unstable-revalidate-tag'
export function unstable_revalidatePath(path: string) {
return unstable_revalidateTag(path)
}
import { NextRequest, NextResponse } from 'next/server' import { NextRequest, NextResponse } from 'next/server'
import { unstable_revalidatePath } from 'next/cache' import { revalidatePath } from 'next/cache'
export const runtime = 'edge' export const runtime = 'edge'
...@@ -7,7 +7,7 @@ export async function GET(req: NextRequest) { ...@@ -7,7 +7,7 @@ export async function GET(req: NextRequest) {
const path = req.nextUrl.searchParams.get('path') || '/' const path = req.nextUrl.searchParams.get('path') || '/'
try { try {
console.log('revalidating path', path) console.log('revalidating path', path)
unstable_revalidatePath(path) revalidatePath(path)
return NextResponse.json({ revalidated: true, now: Date.now() }) return NextResponse.json({ revalidated: true, now: Date.now() })
} catch (err) { } catch (err) {
console.error('Failed to revalidate', path, err) console.error('Failed to revalidate', path, err)
......
import { NextRequest, NextResponse } from 'next/server' import { NextRequest, NextResponse } from 'next/server'
import { unstable_revalidatePath } from 'next/cache' import { revalidatePath } from 'next/cache'
export async function GET(req: NextRequest) { export async function GET(req: NextRequest) {
const path = req.nextUrl.searchParams.get('path') || '/' const path = req.nextUrl.searchParams.get('path') || '/'
try { try {
console.log('revalidating path', path) console.log('revalidating path', path)
unstable_revalidatePath(path) revalidatePath(path)
return NextResponse.json({ revalidated: true, now: Date.now() }) return NextResponse.json({ revalidated: true, now: Date.now() })
} catch (err) { } catch (err) {
console.error('Failed to revalidate', path, err) console.error('Failed to revalidate', path, err)
......
import { NextResponse } from 'next/server' import { NextResponse } from 'next/server'
import { unstable_revalidateTag } from 'next/cache' import { revalidateTag } from 'next/cache'
export const revalidate = 0 export const revalidate = 0
export const runtime = 'edge' export const runtime = 'edge'
export async function GET(req) { export async function GET(req) {
const tag = req.nextUrl.searchParams.get('tag') const tag = req.nextUrl.searchParams.get('tag')
unstable_revalidateTag(tag) revalidateTag(tag)
return NextResponse.json({ revalidated: true, now: Date.now() }) return NextResponse.json({ revalidated: true, now: Date.now() })
} }
import { NextResponse } from 'next/server' import { NextResponse } from 'next/server'
import { unstable_revalidateTag } from 'next/cache' import { revalidateTag } from 'next/cache'
export const revalidate = 0 export const revalidate = 0
export async function GET(req) { export async function GET(req) {
const tag = req.nextUrl.searchParams.get('tag') const tag = req.nextUrl.searchParams.get('tag')
unstable_revalidateTag(tag) revalidateTag(tag)
return NextResponse.json({ revalidated: true, now: Date.now() }) return NextResponse.json({ revalidated: true, now: Date.now() })
} }
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