Skip to content

Commit

Permalink
Update cache methods for app dir (#49165)
Browse files Browse the repository at this point in the history
This ensures we match behavior local and deployed and also cleans up
naming of the methods.

---------
  • Loading branch information
ijjk committed May 3, 2023
1 parent b7c213f commit e4beb85
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 33 deletions.
4 changes: 2 additions & 2 deletions packages/next/cache.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +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'
export { revalidatePath } from 'next/dist/server/web/spec-extension/revalidate-path'
export { revalidateTag } from 'next/dist/server/web/spec-extension/revalidate-tag'
14 changes: 6 additions & 8 deletions packages/next/cache.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
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,
revalidateTag: require('next/dist/server/web/spec-extension/revalidate-tag')
.revalidateTag,
revalidatePath: require('next/dist/server/web/spec-extension/revalidate-path')
.revalidatePath,
}

// https://nodejs.org/api/esm.html#commonjs-namespaces
Expand All @@ -15,5 +13,5 @@ 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
exports.revalidatePath = cacheExports.revalidatePath
exports.revalidateTag = cacheExports.revalidateTag
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import LRUCache from 'next/dist/compiled/lru-cache'
import { CacheFs } from '../../../shared/lib/utils'
import path from '../../../shared/lib/isomorphic/path'
import { CachedFetchValue } from '../../response-cache'
import { CACHE_ONE_YEAR } from '../../../lib/constants'

type FileSystemCacheContext = Omit<
CacheHandlerContext,
Expand Down Expand Up @@ -290,15 +289,17 @@ export default class FileSystemCache implements CacheHandler {
const innerData = data.value.data
const derivedTags = getDerivedTags(innerData.tags || [])

const isStale = derivedTags.some((tag) => {
const wasRevalidated = derivedTags.some((tag) => {
return (
tagsManifest?.items[tag]?.revalidatedAt &&
tagsManifest?.items[tag].revalidatedAt >=
(data?.lastModified || Date.now())
)
})
if (isStale) {
data.lastModified = Date.now() - CACHE_ONE_YEAR
// When revalidate tag is called we don't return
// stale data so it's updated right away
if (wasRevalidated) {
data = undefined
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/next/src/server/web/exports/revalidate-path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// This file is for modularized imports for next/server to get fully-treeshaking.
export { revalidatePath as default } from '../spec-extension/revalidate-path'
2 changes: 2 additions & 0 deletions packages/next/src/server/web/exports/revalidate-tag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// This file is for modularized imports for next/server to get fully-treeshaking.
export { revalidateTag as default } from '../spec-extension/revalidate-tag'

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { revalidateTag } from './revalidate-tag'

export function revalidatePath(path: string) {
return revalidateTag(path)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
StaticGenerationStore,
} from '../../../client/components/static-generation-async-storage'

export function unstable_revalidateTag(tag: string) {
export function revalidateTag(tag: string) {
const staticGenerationAsyncStorage = (
fetch as any
).__nextGetStaticStore?.() as undefined | StaticGenerationAsyncStorage
Expand All @@ -13,7 +13,7 @@ export function unstable_revalidateTag(tag: string) {

if (!store || !store.incrementalCache) {
throw new Error(
`Invariant: static generation store missing in unstable_revalidateTag ${tag}`
`Invariant: static generation store missing in revalidateTag ${tag}`
)
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { NextRequest, NextResponse } from 'next/server'
import { unstable_revalidatePath } from 'next/cache'
import { revalidatePath } from 'next/cache'

export const runtime = 'edge'

export async function GET(req: NextRequest) {
const path = req.nextUrl.searchParams.get('path') || '/'
try {
console.log('revalidating path', path)
unstable_revalidatePath(path)
revalidatePath(path)
return NextResponse.json({ revalidated: true, now: Date.now() })
} catch (err) {
console.error('Failed to revalidate', path, err)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { NextRequest, NextResponse } from 'next/server'
import { unstable_revalidatePath } from 'next/cache'
import { revalidatePath } from 'next/cache'

export async function GET(req: NextRequest) {
const path = req.nextUrl.searchParams.get('path') || '/'
try {
console.log('revalidating path', path)
unstable_revalidatePath(path)
revalidatePath(path)
return NextResponse.json({ revalidated: true, now: Date.now() })
} catch (err) {
console.error('Failed to revalidate', path, err)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { NextResponse } from 'next/server'
import { unstable_revalidateTag } from 'next/cache'
import { revalidateTag } from 'next/cache'

export const revalidate = 0
export const runtime = 'edge'

export async function GET(req) {
const tag = req.nextUrl.searchParams.get('tag')
unstable_revalidateTag(tag)
revalidateTag(tag)
return NextResponse.json({ revalidated: true, now: Date.now() })
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { NextResponse } from 'next/server'
import { unstable_revalidateTag } from 'next/cache'
import { revalidateTag } from 'next/cache'

export const revalidate = 0

export async function GET(req) {
const tag = req.nextUrl.searchParams.get('tag')
unstable_revalidateTag(tag)
revalidateTag(tag)
return NextResponse.json({ revalidated: true, now: Date.now() })
}

0 comments on commit e4beb85

Please sign in to comment.