Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update cache methods for app dir #49165

Merged
merged 4 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,7 @@
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'

Check warning on line 7 in packages/next/src/server/lib/incremental-cache/file-system-cache.ts

View workflow job for this annotation

GitHub Actions / lint

'CACHE_ONE_YEAR' is defined but never used

type FileSystemCacheContext = Omit<
CacheHandlerContext,
Expand Down Expand Up @@ -290,15 +290,17 @@
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() })
}