Skip to content

Commit

Permalink
fix(cache): dont include file version in cache (#6494)
Browse files Browse the repository at this point in the history
**Problem:**
The current cache cached `ParseFileResult`, which contained the file
version, which was incorrect when saving files from the vs code.

**Fix:**
Cache `ParsedTextFile` (which is only the parse result itself), which
doesn't rely on the file version.

**Manual Tests:**
I hereby swear that:

- [X] I opened a hydrogen project and it loaded
- [X] I could navigate to various routes in Play mode
  • Loading branch information
liady authored Oct 10, 2024
1 parent 0ff29ca commit 4eb10e3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { URL_HASH } from '../../../common/env-vars'
import { type ParseCacheOptions } from '../../../core/shared/parse-cache-utils'
import {
ARBITRARY_CODE_FILE_NAME,
createParseFileResult,
type ParseFile,
type ParseFileResult,
} from '../common/worker-types'
import localforage from 'localforage'
import type { ParsedTextFile } from 'utopia-shared/src/types'

export const CACHE_DB_NAME = 'editor-cache'
export const PARSE_CACHE_STORE_NAME = 'file-parse-cache'
Expand Down Expand Up @@ -66,25 +68,25 @@ export async function getParseResultFromCache(
file: ParseFile,
parsingCacheOptions: ParseCacheOptions,
): Promise<ParseFileResult | null> {
const { filename, content } = file
const { filename, content, versionNumber } = file
if (!shouldUseCacheForFile(filename, parsingCacheOptions)) {
return null
}
const cacheKey = getCacheKey(filename)
//check localforage for cache
const cachedResult = await getParseCacheStore().getItem<CachedParseResult>(cacheKey)
const cachedResultForContent = cachedResult?.[getCacheIndexKeyWithVersion(content)]
if (cachedResultForContent?.parseResult?.type === 'PARSE_SUCCESS') {
if (cachedResultForContent?.type === 'PARSE_SUCCESS') {
logCacheMessage(parsingCacheOptions, 'Cache hit for', ...stringIdentifiers(filename, content))
return cachedResultForContent
return createParseFileResult(filename, cachedResultForContent, versionNumber)
}
logCacheMessage(parsingCacheOptions, 'Cache miss for', ...stringIdentifiers(filename, content))
return null
}

export async function storeParseResultInCache(
file: ParseFile,
result: ParseFileResult,
result: ParsedTextFile,
parsingCacheOptions: ParseCacheOptions,
): Promise<void> {
const { filename, content } = file
Expand Down Expand Up @@ -121,4 +123,4 @@ export async function clearParseCache(parsingCacheOptions: ParseCacheOptions) {
})
}

type CachedParseResult = { [fileContent: string]: ParseFileResult }
type CachedParseResult = { [fileContent: string]: ParsedTextFile }
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export function getParseFileResult(
parseOptions.parsingCacheOptions.useParsingCache
) {
// non blocking cache write
void storeParseResultInCache(file, result, parseOptions.parsingCacheOptions)
void storeParseResultInCache(file, result.parseResult, parseOptions.parsingCacheOptions)
}

return result
Expand Down

0 comments on commit 4eb10e3

Please sign in to comment.