-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: an implementation to cache tags to disk?
- Loading branch information
Showing
4 changed files
with
221 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,171 @@ | ||
import { getAllPosts } from './blog'; | ||
import { getAllNewsletters } from './newsletters'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
import type { MarkdownDocument } from '../data/content-types'; | ||
|
||
const CACHE_FILE = path.join( | ||
process.cwd(), | ||
'.next', | ||
'cache', | ||
'tag-registry.json' | ||
); | ||
|
||
export const parseTag = (tag: string) => { | ||
return tag.split(' ').join('-').toLocaleLowerCase(); | ||
}; | ||
|
||
export const getAllTags = async () => { | ||
try { | ||
// Safely fetch content with fallbacks | ||
const posts = await getAllPosts().catch(() => []); | ||
const newsletters = await getAllNewsletters().catch(() => []); | ||
|
||
const allTags = new Set<string>(); | ||
const allContent = [...posts, ...newsletters]; | ||
|
||
// More defensive processing of content | ||
allContent.forEach((content) => { | ||
const tags = content?.frontmatter?.tags; | ||
if (!tags) return; | ||
|
||
if (!Array.isArray(tags)) return; | ||
|
||
tags.forEach((tag) => { | ||
if (typeof tag === 'string') { | ||
const parsedTag = parseTag(tag); | ||
if (parsedTag) { | ||
allTags.add(parsedTag); | ||
} | ||
} | ||
}); | ||
type TagMap = { | ||
[tag: string]: string[]; // tag -> array of content slugs | ||
}; | ||
|
||
// Singleton to manage tags across the application | ||
class TagRegistry { | ||
private static instance: TagRegistry; | ||
private tagMap: Map<string, Set<string>> = new Map(); // tag -> set of content slugs | ||
private initialized = false; | ||
|
||
private constructor() {} | ||
|
||
static getInstance(): TagRegistry { | ||
if (!TagRegistry.instance) { | ||
TagRegistry.instance = new TagRegistry(); | ||
} | ||
return TagRegistry.instance; | ||
} | ||
|
||
private registerContentTags(content: MarkdownDocument) { | ||
const tags = content?.frontmatter?.tags; | ||
if (!tags || !Array.isArray(tags)) return; | ||
|
||
const slug = content.frontmatter.slug; | ||
if (!slug) return; | ||
|
||
tags.forEach((tag) => { | ||
if (typeof tag !== 'string') return; | ||
const parsedTag = parseTag(tag); | ||
if (!parsedTag) return; | ||
|
||
if (!this.tagMap.has(parsedTag)) { | ||
this.tagMap.set(parsedTag, new Set()); | ||
} | ||
this.tagMap.get(parsedTag)?.add(slug); | ||
}); | ||
} | ||
|
||
registerContent(content: MarkdownDocument[]) { | ||
content.forEach((item) => this.registerContentTags(item)); | ||
this.initialized = true; | ||
} | ||
|
||
isInitialized(): boolean { | ||
return this.initialized; | ||
} | ||
|
||
getAllTags(): string[] { | ||
return Array.from(this.tagMap.keys()).sort(); | ||
} | ||
|
||
getContentSlugsForTag(tag: string): string[] { | ||
const parsedTag = parseTag(tag); | ||
return Array.from(this.tagMap.get(parsedTag) || []); | ||
} | ||
|
||
hasTag(tag: string): boolean { | ||
return this.tagMap.has(parseTag(tag)); | ||
} | ||
|
||
// Save the current state to disk | ||
saveToCache(): void { | ||
const cacheDir = path.dirname(CACHE_FILE); | ||
if (!fs.existsSync(cacheDir)) { | ||
fs.mkdirSync(cacheDir, { recursive: true }); | ||
} | ||
|
||
const serializedMap: TagMap = {}; | ||
this.tagMap.forEach((slugs, tag) => { | ||
serializedMap[tag] = Array.from(slugs); | ||
}); | ||
|
||
// Convert to array and sort | ||
const uniqueTags = Array.from(allTags).filter(Boolean).sort(); | ||
fs.writeFileSync(CACHE_FILE, JSON.stringify(serializedMap, null, 2)); | ||
} | ||
|
||
// Load state from disk | ||
loadFromCache(): boolean { | ||
try { | ||
if (!fs.existsSync(CACHE_FILE)) { | ||
return false; | ||
} | ||
|
||
const data = JSON.parse(fs.readFileSync(CACHE_FILE, 'utf-8')) as TagMap; | ||
this.tagMap.clear(); | ||
|
||
Object.entries(data).forEach(([tag, slugs]) => { | ||
this.tagMap.set(tag, new Set(slugs)); | ||
}); | ||
|
||
this.initialized = true; | ||
return true; | ||
} catch (error) { | ||
console.error('Error loading tag registry cache:', error); | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
// Export functions that use the registry | ||
export const initializeTagRegistry = async (content: MarkdownDocument[]) => { | ||
try { | ||
const registry = TagRegistry.getInstance(); | ||
|
||
return uniqueTags; | ||
// Try to load from cache first | ||
if (!registry.isInitialized() && !registry.loadFromCache()) { | ||
// If cache doesn't exist or is invalid, rebuild and save | ||
registry.registerContent(content); | ||
registry.saveToCache(); | ||
} | ||
} catch (error) { | ||
console.error('Error initializing tag registry:', error); | ||
} | ||
}; | ||
|
||
export const getAllTags = async () => { | ||
try { | ||
const registry = TagRegistry.getInstance(); | ||
if (!registry.isInitialized() && !registry.loadFromCache()) { | ||
console.warn('Tag registry accessed before initialization'); | ||
return []; | ||
} | ||
return registry.getAllTags(); | ||
} catch (error) { | ||
console.error('Error in getAllTags:', error); | ||
return []; | ||
} | ||
}; | ||
|
||
export const getContentSlugsForTag = async (tag: string) => { | ||
try { | ||
const registry = TagRegistry.getInstance(); | ||
if (!registry.isInitialized() && !registry.loadFromCache()) { | ||
console.warn('Tag registry accessed before initialization'); | ||
return []; | ||
} | ||
return registry.getContentSlugsForTag(tag); | ||
} catch (error) { | ||
console.error('Error getting content for tag:', error); | ||
return []; | ||
} | ||
}; | ||
|
||
export const hasTag = async (tag: string) => { | ||
try { | ||
const registry = TagRegistry.getInstance(); | ||
if (!registry.isInitialized() && !registry.loadFromCache()) { | ||
console.warn('Tag registry accessed before initialization'); | ||
return false; | ||
} | ||
return registry.hasTag(tag); | ||
} catch (error) { | ||
console.error('Error checking tag existence:', error); | ||
return false; | ||
} | ||
}; |