├── .github ├── FUNDING.yml └── workflows │ └── ci-cd.yml ├── res ├── tag.png ├── entry.png └── index.png ├── .gitignore ├── src ├── components │ ├── await.ts │ ├── typography.tsx │ ├── tag.tsx │ └── entry-list-item.tsx ├── paths.ts ├── index.ts ├── pages │ ├── disabled.tsx │ ├── tag.tsx │ ├── index.tsx │ └── entry.tsx ├── layout.tsx ├── router.tsx ├── cache.ts └── theme.ts ├── tsup.config.ts ├── tsconfig.json ├── LICENSE ├── package.json ├── README.md └── pnpm-lock.yaml /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: [franky47] 2 | liberapay: francoisbest 3 | -------------------------------------------------------------------------------- /res/tag.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/47ng/next-cache-explorer/HEAD/res/tag.png -------------------------------------------------------------------------------- /res/entry.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/47ng/next-cache-explorer/HEAD/res/entry.png -------------------------------------------------------------------------------- /res/index.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/47ng/next-cache-explorer/HEAD/res/index.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | dist/ 3 | .env 4 | .volumes/ 5 | coverage/ 6 | sceau.json 7 | -------------------------------------------------------------------------------- /.github/workflows/ci-cd.yml: -------------------------------------------------------------------------------- 1 | name: CI/CD 2 | 3 | on: 4 | push: 5 | 6 | jobs: 7 | ci-cd: 8 | name: CI/CD 9 | uses: 47ng/workflows/.github/workflows/pnpm-ci-cd.yml@main 10 | secrets: inherit 11 | -------------------------------------------------------------------------------- /src/components/await.ts: -------------------------------------------------------------------------------- 1 | export async function Await({ 2 | promise, 3 | children, 4 | }: { 5 | promise: Promise 6 | children: (result: T) => JSX.Element 7 | }) { 8 | let result = await promise 9 | 10 | return children(result) 11 | } 12 | -------------------------------------------------------------------------------- /tsup.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'tsup' 2 | 3 | export default defineConfig({ 4 | format: ['esm'], 5 | dts: true, 6 | entry: ['src/index.ts'], 7 | esbuildOptions: () => ({ 8 | jsx: 'preserve', 9 | jsxDev: true, 10 | }), 11 | }) 12 | -------------------------------------------------------------------------------- /src/components/typography.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import { font, spacing } from '../theme' 3 | 4 | export const H2: React.FC> = ({ 5 | style, 6 | ...props 7 | }) => ( 8 |

17 | ) 18 | -------------------------------------------------------------------------------- /src/paths.ts: -------------------------------------------------------------------------------- 1 | import path from 'node:path' 2 | import { fileURLToPath } from 'node:url' 3 | 4 | export const fetchCacheDir = path.resolve( 5 | process.cwd(), 6 | '.next', 7 | 'cache', 8 | 'fetch-cache', 9 | ) 10 | export const tagsManifestFilePath = path.resolve( 11 | fetchCacheDir, 12 | 'tags-manifest.json', 13 | ) 14 | 15 | export const packageJsonPath = path.resolve( 16 | path.dirname(fileURLToPath(import.meta.url)), 17 | '../package.json', 18 | ) 19 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import { notFound } from 'next/navigation' 2 | import DisabledPage from './pages/disabled' 3 | import type { CacheExplorerCatchAllPageProps } from './router' 4 | import CacheExplorerRouter from './router' 5 | 6 | type Options = { 7 | mountPath?: string 8 | enabled?: boolean 9 | notFoundWhenDisabled?: boolean 10 | } 11 | 12 | export function mountCacheExplorer({ 13 | enabled = process.env.NODE_ENV !== 'production' || 14 | process.env.CACHE_EXPLORER === 'true', 15 | mountPath = '/cache-explorer', 16 | notFoundWhenDisabled = false, 17 | }: Options = {}) { 18 | if (!enabled) { 19 | return notFoundWhenDisabled ? notFound : DisabledPage 20 | } 21 | return (props: Omit) => 22 | CacheExplorerRouter({ mountPath, ...props }) 23 | } 24 | -------------------------------------------------------------------------------- /src/pages/disabled.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import { colors, font, spacing } from '../theme' 3 | 4 | export default function DisabledPage() { 5 | return ( 6 |
17 |

24 | Cache Explorer is disabled 25 |

26 |

34 | Run with `CACHE_EXPLORER=true` to enable it. 35 |

36 |
37 | ) 38 | } 39 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://json.schemastore.org/tsconfig", 3 | "compilerOptions": { 4 | // Type checking 5 | "strict": true, 6 | "noUncheckedIndexedAccess": true, 7 | 8 | // Modules 9 | "module": "ESNext", 10 | "moduleResolution": "Bundler", 11 | "resolveJsonModule": true, 12 | // Language & Environment 13 | "target": "ESNext", 14 | "lib": ["DOM", "DOM.Iterable", "ESNext"], 15 | 16 | // Emit is disabled (typechecking only) 17 | "noEmit": true, 18 | "declaration": true, 19 | "declarationMap": true, 20 | "jsx": "react", 21 | 22 | // Interop 23 | "allowJs": true, 24 | "isolatedModules": true, 25 | "esModuleInterop": true, 26 | "forceConsistentCasingInFileNames": true, 27 | "verbatimModuleSyntax": true, 28 | "moduleDetection": "force", 29 | 30 | // Misc 31 | "skipLibCheck": true, 32 | "skipDefaultLibCheck": true 33 | }, 34 | "include": ["**/*.ts", "**/*.tsx"], 35 | "exclude": ["node_modules"] 36 | } 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 François Best 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/layout.tsx: -------------------------------------------------------------------------------- 1 | import Link from 'next/link' 2 | import fs from 'node:fs/promises' 3 | import React from 'react' 4 | import { packageJsonPath } from './paths' 5 | import { colors, font, spacing } from './theme' 6 | 7 | type LayoutProps = { 8 | mountPath: string 9 | children: React.ReactNode 10 | } 11 | 12 | export async function Layout({ children, mountPath }: LayoutProps) { 13 | const pkgJson = await fs.readFile(packageJsonPath, 'utf-8') 14 | const pkg = JSON.parse(pkgJson) 15 | return ( 16 |
23 |

30 | Next.js Cache Explorer{' '} 31 | 39 | v{pkg.version} 40 | 41 |

42 | {children} 43 |
44 | ) 45 | } 46 | -------------------------------------------------------------------------------- /src/router.tsx: -------------------------------------------------------------------------------- 1 | import { notFound } from 'next/navigation' 2 | import React from 'react' 3 | import { Layout } from './layout' 4 | import DisabledPage from './pages/disabled' 5 | import EntryPage from './pages/entry' 6 | import IndexPage from './pages/index' 7 | import TagPage from './pages/tag' 8 | 9 | export type CacheExplorerCatchAllPageProps = { 10 | mountPath: string 11 | params: { 12 | slug: string[] 13 | } 14 | } 15 | 16 | export default async function CacheExplorerRouter({ 17 | mountPath, 18 | params, 19 | }: CacheExplorerCatchAllPageProps) { 20 | const path = (params.slug ?? []).join('/') 21 | if (path === '') { 22 | return ( 23 | 24 | 25 | 26 | ) 27 | } 28 | if (path.startsWith('entry/')) { 29 | return ( 30 | 31 | 35 | 36 | ) 37 | } 38 | if (path.startsWith('tag/')) { 39 | return ( 40 | 41 | 45 | 46 | ) 47 | } 48 | if (path === 'disabled') { 49 | return 50 | } 51 | return notFound() 52 | } 53 | -------------------------------------------------------------------------------- /src/pages/tag.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import { readTagsManifest } from '../cache' 3 | import { EntryListItem } from '../components/entry-list-item' 4 | import { Tag } from '../components/tag' 5 | import { H2 } from '../components/typography' 6 | import { spacing } from '../theme' 7 | 8 | type TagPageProps = { 9 | mountPath: string 10 | tag: string 11 | } 12 | 13 | export default async function IndexPage({ mountPath, tag }: TagPageProps) { 14 | const manifest = await readTagsManifest() 15 | if (!manifest.success) { 16 | return
No entries
17 | } 18 | const { items } = manifest.data 19 | if (Object.keys(items).length === 0) { 20 | return
No entries
21 | } 22 | const entries = Array.from( 23 | new Set( 24 | Object.entries(items) 25 | .filter(([key]) => key === tag) 26 | .flatMap(([, v]) => v.keys), 27 | ), 28 | ) 29 | 30 | return ( 31 | <> 32 |

33 | Entries tagged {tag} 34 |

35 |
    36 | {entries.map(id => ( 37 | 43 | ))} 44 |
45 | 46 | ) 47 | } 48 | -------------------------------------------------------------------------------- /src/cache.ts: -------------------------------------------------------------------------------- 1 | import { base64toUTF8 } from '@47ng/codec' 2 | import fs from 'node:fs/promises' 3 | import path from 'node:path' 4 | import { setTimeout } from 'node:timers/promises' 5 | import { z } from 'zod' 6 | import { fetchCacheDir, tagsManifestFilePath } from './paths' 7 | 8 | const cacheEntrySchema = z.object({ 9 | kind: z.literal('FETCH'), 10 | data: z.object({ 11 | headers: z.record(z.string()), 12 | body: z.string().transform(base64toUTF8), 13 | status: z.number(), 14 | tags: z.array(z.string()), 15 | url: z.string(), 16 | }), 17 | revalidate: z.number().optional(), 18 | }) 19 | export type CacheEntry = z.infer 20 | 21 | export async function readCacheEntry(id: string) { 22 | await setTimeout(1000) 23 | const filePath = path.resolve(fetchCacheDir, id) 24 | const contents = await fs.readFile(filePath, 'utf-8') 25 | return cacheEntrySchema.parse(JSON.parse(contents)) 26 | } 27 | 28 | // -- 29 | 30 | const tagsManifestSchema = z.object({ 31 | version: z.literal(1), 32 | items: z.record( 33 | z.object({ 34 | keys: z.array(z.string()), 35 | revalidatedAt: z 36 | .number() 37 | .transform(v => new Date(v)) 38 | .optional(), 39 | }), 40 | ), 41 | }) 42 | export type TagsManifest = z.infer 43 | 44 | export async function readTagsManifest() { 45 | try { 46 | const contents = await fs.readFile(tagsManifestFilePath, 'utf-8') 47 | return tagsManifestSchema.safeParse(JSON.parse(contents)) 48 | } catch { 49 | return tagsManifestSchema.safeParse(null) 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/theme.ts: -------------------------------------------------------------------------------- 1 | export const colors = { 2 | bg: 'var(--nce-bg, var(--nce-gray-900, transparent))', 3 | text: 'var(--nce-text, var(--nce-gray-100, inherit))', 4 | gray: { 5 | 50: 'var(--nce-gray-50, #f8fafc)', 6 | 100: 'var(--nce-gray-100, #f1f5f9)', 7 | 200: 'var(--nce-gray-200, #e2e8f0)', 8 | 300: 'var(--nce-gray-300, #cbd5e1)', 9 | 400: 'var(--nce-gray-400, #94a3b8)', 10 | 500: 'var(--nce-gray-500, #64748b)', 11 | 600: 'var(--nce-gray-600, #475569)', 12 | 700: 'var(--nce-gray-700, #334155)', 13 | 800: 'var(--nce-gray-800, #1e293b)', 14 | 900: 'var(--nce-gray-900, #0f172a)', 15 | 950: 'var(--nce-gray-950, #020617)', 16 | }, 17 | border: 'var(--nce-border, #64748b40)', 18 | red: { 500: '#ef4444' }, 19 | orange: { 500: '#f97316' }, 20 | amber: { 500: '#f59e0b' }, 21 | yellow: { 500: '#eab308' }, 22 | lime: { 500: '#84cc16' }, 23 | green: { 500: '#22c55e' }, 24 | emerald: { 500: '#10b981' }, 25 | teal: { 500: '#14b8a6' }, 26 | cyan: { 500: '#06b6d4' }, 27 | sky: { 500: '#0ea5e9' }, 28 | blue: { 500: '#3b82f6' }, 29 | indigo: { 500: '#6366f1' }, 30 | violet: { 500: '#8b5cf6' }, 31 | purple: { 500: '#a855f7' }, 32 | fuchsia: { 500: '#d946ef' }, 33 | pink: { 500: '#ec4899' }, 34 | rose: { 500: '#f43f5e' }, 35 | } 36 | 37 | export const spacing = { 38 | 1: 'var(--nce-spacing-1, 0.25rem)', 39 | 4: 'var(--nce-spacing-4, 1rem)', 40 | } 41 | 42 | export const font = { 43 | mono: 'var(--nce-font-mono, monospace)', 44 | normal: 'var(--nce-font-normal, 400)', 45 | bold: 'var(--nce-font-bold, 700)', 46 | xs: 'var(--nce-text-xs, 0.75rem)', 47 | sm: 'var(--nce-text-sm, 0.875rem)', 48 | lg: 'var(--nce-text-lg, 1.125rem)', 49 | xl: 'var(--nce-text-xl, 1.25rem)', 50 | '2xl': 'var(--nce-text-2xl, 1.5rem)', 51 | '3xl': 'var(--nce-text-3xl, 1.875rem)', 52 | } 53 | -------------------------------------------------------------------------------- /src/components/tag.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import { colors } from '../theme' 3 | 4 | export type TagProps = React.ComponentProps<'span'> & { 5 | children: string 6 | fixedHash?: number 7 | } 8 | 9 | export const Tag: React.FC = ({ 10 | children, 11 | fixedHash, 12 | style, 13 | ...props 14 | }) => { 15 | const tag = String(children) 16 | const color = tag.startsWith('/') 17 | ? colors.gray[500] 18 | : getColors(tag, fixedHash) 19 | return ( 20 | 30 | 40 | {tag} 41 | 42 | ) 43 | } 44 | 45 | function getColors(input: string, fixedHash?: number) { 46 | // Digest the input string into a 4-bit integer. 47 | let hash = 0x2 48 | for (let i = 0; i < input.length; i++) { 49 | hash ^= input.charCodeAt(i) & 0xf 50 | hash ^= (input.charCodeAt(i) >> 4) & 0xf 51 | } 52 | // prettier-ignore 53 | switch (fixedHash ?? hash) { 54 | case 0x00: return colors.indigo[500] 55 | case 0x01: return colors.blue[500] 56 | case 0x02: return colors.cyan[500] 57 | case 0x03: return colors.green[500] 58 | case 0x04: return colors.purple[500] 59 | case 0x05: return colors.red[500] 60 | case 0x06: return colors.rose[500] 61 | case 0x07: return colors.teal[500] 62 | case 0x08: return colors.orange[500] 63 | case 0x09: return colors.amber[500] 64 | case 0x0a: return colors.emerald[500] 65 | case 0x0b: return colors.sky[500] 66 | case 0x0c: return colors.yellow[500] 67 | case 0x0d: return colors.violet[500] 68 | case 0x0e: return colors.fuchsia[500] 69 | case 0x0f: return colors.pink[500] 70 | default: return colors.gray[500] 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "next-cache-explorer", 3 | "version": "0.0.0-semantically-released", 4 | "description": "Navigate & debug the Next.js data cache", 5 | "license": "MIT", 6 | "author": { 7 | "name": "François Best", 8 | "email": "npm@francoisbest.com", 9 | "url": "https://francoisbest.com" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "git+https://github.com/47ng/next-cache-explorer.git" 14 | }, 15 | "keywords": [ 16 | "next", 17 | "nextjs", 18 | "cache", 19 | "plugin" 20 | ], 21 | "publishConfig": { 22 | "access": "public" 23 | }, 24 | "files": [ 25 | "dist/", 26 | "sceau.json" 27 | ], 28 | "type": "module", 29 | "sideEffects": false, 30 | "module": "dist/index.js", 31 | "types": "dist/index.d.ts", 32 | "exports": { 33 | ".": { 34 | "types": "./dist/index.d.ts", 35 | "import": "./dist/index.js" 36 | } 37 | }, 38 | "tsup": { 39 | "format": [ 40 | "esm" 41 | ], 42 | "dts": true, 43 | "sourcemap": true, 44 | "entryPoints": [ 45 | "src/index.ts" 46 | ], 47 | "treeshake": true 48 | }, 49 | "scripts": { 50 | "typecheck": "tsc", 51 | "build": "tsup --clean --external=react", 52 | "ci": "run-p typecheck build", 53 | "prepack": "sceau sign" 54 | }, 55 | "peerDependencies": { 56 | "next": ">=13.4" 57 | }, 58 | "dependencies": { 59 | "@47ng/codec": "^1.1.0", 60 | "pretty-bytes": "^6.1.1", 61 | "pretty-ms": "^8.0.0", 62 | "zod": "^3.22.4" 63 | }, 64 | "devDependencies": { 65 | "@types/node": "^20.8.3", 66 | "@types/react": "^18", 67 | "next": ">=13.4", 68 | "npm-run-all": "^4.1.5", 69 | "react": "^18", 70 | "sceau": "^1.3.0", 71 | "semantic-release": "^22.0.5", 72 | "tsup": "^7.2.0", 73 | "typescript": "^5.2.2" 74 | }, 75 | "prettier": { 76 | "arrowParens": "avoid", 77 | "semi": false, 78 | "singleQuote": true, 79 | "tabWidth": 2, 80 | "useTabs": false 81 | }, 82 | "release": { 83 | "branches": [ 84 | "main", 85 | { 86 | "name": "next", 87 | "channel": "next", 88 | "prerelease": true 89 | } 90 | ] 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/components/entry-list-item.tsx: -------------------------------------------------------------------------------- 1 | import Link from 'next/link' 2 | import prettyBytes from 'pretty-bytes' 3 | import React, { Suspense } from 'react' 4 | import { readCacheEntry } from '../cache' 5 | import { colors, font } from '../theme' 6 | import { Await } from './await' 7 | import { Tag } from './tag' 8 | 9 | type EntryListItemProps = React.ComponentProps<'li'> & { 10 | id: string 11 | mountPath: string 12 | } 13 | 14 | export const EntryListItem: React.FC = ({ 15 | id, 16 | mountPath, 17 | style = {}, 18 | ...props 19 | }) => { 20 | return ( 21 |
  • 31 | {id}} 33 | > 34 | 35 | {entry => ( 36 | <> 37 | 38 | 39 | {id.slice(0, 8)} 40 | {' '} 41 | 42 | 100_000 48 | ? colors.red[500] 49 | : entry.data.body.length > 10_000 50 | ? colors.yellow[500] 51 | : colors.green[500], 52 | opacity: 0.5, 53 | }} 54 | title={`${entry.data.body.length} bytes`} 55 | > 56 | {prettyBytes(entry.data.body.length)} 57 | 58 | 59 | {entry.data.url} 60 | 61 | {entry.data.tags.map(tag => ( 62 | 66 | {tag} 67 | 68 | ))} 69 | 70 | )} 71 | 72 | 73 |
  • 74 | ) 75 | } 76 | -------------------------------------------------------------------------------- /src/pages/index.tsx: -------------------------------------------------------------------------------- 1 | import Link from 'next/link' 2 | import React from 'react' 3 | import { readTagsManifest } from '../cache' 4 | import { EntryListItem } from '../components/entry-list-item' 5 | import { Tag } from '../components/tag' 6 | import { H2 } from '../components/typography' 7 | import { colors, font, spacing } from '../theme' 8 | 9 | type IndexPageProps = { 10 | mountPath: string 11 | } 12 | 13 | export default async function IndexPage({ mountPath }: IndexPageProps) { 14 | const manifest = await readTagsManifest() 15 | if (!manifest.success) { 16 | return
    Empty cache
    17 | } 18 | const { items } = manifest.data 19 | if (Object.keys(items).length === 0) { 20 | return
    Empty cache
    21 | } 22 | const tags = Object.entries(items).filter(([key]) => !key.startsWith('/')) 23 | const pages = Object.entries(items).filter(([key]) => key.startsWith('/')) 24 | const entries = Array.from( 25 | new Set(Object.values(items).flatMap(({ keys }) => keys)), 26 | ) 27 | 28 | return ( 29 |
    30 |

    Tags

    31 |
      32 | {tags.map(([key, { keys }]) => ( 33 |
    • 34 | 35 | {key} 36 | {' '} 37 | 44 | ({keys.length} {keys.length > 1 ? 'entries' : 'entry'}) 45 | 46 |
    • 47 | ))} 48 |
    49 |

    Pages

    50 |
      51 | {pages.map(([key, { keys }]) => ( 52 |
    • 53 | 54 | {key} 55 | {' '} 56 | 63 | ({keys.length} {keys.length > 1 ? 'entries' : 'entry'}) 64 | 65 |
    • 66 | ))} 67 |
    68 |

    All Entries

    69 |
      70 | {entries.map(id => ( 71 | 77 | ))} 78 |
    79 |
    80 | ) 81 | } 82 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

    next-cache-explorer

    2 | 3 |
    4 | 5 | [![NPM](https://img.shields.io/npm/v/next-cache-explorer?color=red)](https://www.npmjs.com/package/next-cache-explorer) 6 | [![MIT License](https://img.shields.io/github/license/47ng/next-cache-explorer.svg?color=blue)](https://github.com/47ng/next-cache-explorer/blob/next/LICENSE) 7 | [![CI/CD](https://github.com/47ng/next-cache-explorer/workflows/CI%2FCD/badge.svg?branch=next)](https://github.com/47ng/next-cache-explorer/actions) 8 | [![Coverage Status](https://coveralls.io/repos/github/47ng/next-cache-explorer/badge.svg?branch=next)](https://coveralls.io/github/47ng/next-cache-explorer?branch=next) 9 | 10 |
    11 | 12 |

    13 | Navigate & debug the Next.js data cache 14 |

    15 | 16 | ## Installation 17 | 18 | 1. Install the dependency: 19 | 20 | > Note: requires Next.js 13.4+ with the `app` router. 21 | 22 | ```shell 23 | pnpm add next-cache-explorer 24 | yarn add next-cache-explorer 25 | npm i next-cache-explorer 26 | ``` 27 | 28 | 2. Add the following file to your Next.js app: 29 | 30 | ```tsx 31 | // src/app/cache-explorer/[[...slug]]/page.tsx 32 | 33 | import { mountCacheExplorer } from 'next-cache-explorer' 34 | 35 | export const dynamic = 'force-dynamic' 36 | 37 | export default mountCacheExplorer() 38 | ``` 39 | 40 | 3. Start your app and navigate to the `/cache-explorer` page. 41 | 42 | ## Usage 43 | 44 | ### Index page 45 | 46 | Shows a list of all cache entries and tags. Click an entry to see its details, 47 | or a tag to only show entries with that tag. 48 | 49 | ![](https://raw.githubusercontent.com/47ng/next-cache-explorer/main/res/index.png) 50 | 51 | ### Tags page 52 | 53 | Shows a list of all entries for a given tag. 54 | 55 | ![](https://raw.githubusercontent.com/47ng/next-cache-explorer/main/res/tag.png) 56 | 57 | ### Entry page 58 | 59 | Shows the details of a given cache entry, including: 60 | 61 | - Size in bytes 62 | - Associated URL 63 | - Revalidation time / TTL 64 | - Tags to invalidate this entry 65 | - Response headers 66 | - Response body 67 | 68 | ![](https://raw.githubusercontent.com/47ng/next-cache-explorer/main/res/entry.png) 69 | 70 | ## Options 71 | 72 | ### Mount path 73 | 74 | You can choose to place the cache explorer at a different path. The main page 75 | will still need to be under `[[...slug]]/page.tsx`, but if you place it in 76 | another base path, you'll need to configure it: 77 | 78 | ```tsx 79 | // src/app/admin/cache-explorer/[[...slug]]/page.tsx 80 | 81 | export default mountCacheExplorer({ 82 | mountPath: '/admin/cache-explorer', 83 | }) 84 | ``` 85 | 86 | ### Running in production 87 | 88 | By default, the cache explorer will only be available in development mode. 89 | To enable it in production, you can set the `CACHE_EXPLORER` environment variable 90 | to `true`. 91 | 92 | You can also enable it programmatically: 93 | 94 | ```ts 95 | export default mountCacheExplorer({ 96 | enabled: true, 97 | }) 98 | ``` 99 | 100 | When disabled, the page (and sub-pages) will render a "Cache explorer is disabled" message. 101 | 102 | To redirect to the default NotFound page, use the `notFoundWhenDisabled` option: 103 | 104 | ```ts 105 | export default mountCacheExplorer({ 106 | notFoundWhenDisabled: true, 107 | }) 108 | ``` 109 | 110 | ## License 111 | 112 | [MIT](https://github.com/47ng/next-cache-explorer/blob/next/LICENSE) - Made with ❤️ by [François Best](https://francoisbest.com) 113 | 114 | Using this package at work ? [Sponsor me](https://github.com/sponsors/franky47) to help with support and maintenance. 115 | 116 | This package is signed with [`sceau`](https://github.com/47ng/sceau), under the following associated public key: 117 | 118 | ```shell 119 | sceau verify --publicKey 380db0ad0ccf92c3bcffc065b614515dd260cf291ed301a9f6ae550f6419f3c7 120 | ``` 121 | -------------------------------------------------------------------------------- /src/pages/entry.tsx: -------------------------------------------------------------------------------- 1 | import Link from 'next/link' 2 | import prettyBytes from 'pretty-bytes' 3 | import prettyMs from 'pretty-ms' 4 | import React from 'react' 5 | import { readCacheEntry } from '../cache' 6 | import { Tag } from '../components/tag' 7 | import { H2 } from '../components/typography' 8 | import { colors, font, spacing } from '../theme' 9 | 10 | type EntryPageProps = { 11 | mountPath: string 12 | params: { 13 | id: string 14 | } 15 | } 16 | 17 | export default async function EntryPage({ mountPath, params }: EntryPageProps) { 18 | const entry = await readCacheEntry(params.id) 19 | return ( 20 | <> 21 |

    Cache Entry

    22 |
    30 | Key 31 | 37 | {params.id} 38 | 39 | URL 40 | 41 | {' '} 42 | {entry.data.url} 43 | 44 | Size 45 | {prettyBytes(entry.data.body.length)} 46 | {Boolean(entry.data.tags.length) && ( 47 | <> 48 | Tags 49 |
    50 | {entry.data.tags.map(tag => ( 51 | 55 | {tag} 56 | 57 | ))} 58 |
    59 | 60 | )} 61 | {Boolean(entry.revalidate) && ( 62 | <> 63 | Revalidate 64 | 65 | {prettyMs(entry.revalidate! * 1000, { verbose: true })} 66 | 67 | 68 | )} 69 | {entry.data.headers.date && ( 70 | <> 71 | Age 72 | 73 | {prettyMs( 74 | new Date().valueOf() - 75 | new Date(entry.data.headers.date).valueOf(), 76 | { verbose: true }, 77 | )} 78 | 79 | 80 | )} 81 |
    82 |
    83 | 84 |

    89 | Headers 90 |

    91 |
    92 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | {Object.entries(entry.data.headers).map(([key, value]) => ( 107 | 113 | 128 | 137 | 138 | ))} 139 | 140 |
    NameValue
    119 | 125 | {key} 126 | 127 | 135 | {value} 136 |
    141 |
    142 |
    143 | 144 |

    149 | Content 150 |

    151 |
    152 | {entry.data.headers['content-type']?.startsWith('application/json') ? ( 153 | 154 | ) : entry.data.headers['content-type']?.startsWith('text/html') ? ( 155 | 156 | ) : ( 157 |
    {entry.data.body}
    158 | )} 159 |
    160 | 161 | ) 162 | } 163 | 164 | function StatusTag({ status }: { status: number }) { 165 | const fixedHash = 166 | status >= 500 167 | ? 0x05 168 | : status >= 400 169 | ? 0x0c 170 | : status > 300 171 | ? 0x01 172 | : status >= 200 173 | ? 0x03 174 | : 0xff 175 | return ( 176 | 177 | {status.toString()} 178 | 179 | ) 180 | } 181 | 182 | function JSONContent({ value }: { value: string }) { 183 | return ( 184 |
    185 |       {JSON.stringify(JSON.parse(value), null, 2)}
    186 |     
    187 | ) 188 | } 189 | 190 | function HTMLContent({ value }: { value: string }) { 191 | return ( 192 | <> 193 |
    {value}
    194 |