From 4eb10e3f3b30a153796376c3c372ea163b8cee0e Mon Sep 17 00:00:00 2001 From: Liad Yosef Date: Thu, 10 Oct 2024 11:42:18 +0300 Subject: [PATCH] fix(cache): dont include file version in cache (#6494) **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 --- .../parser-printer/parse-cache-utils.worker.ts | 12 +++++++----- .../workers/parser-printer/parser-printer-worker.ts | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/editor/src/core/workers/parser-printer/parse-cache-utils.worker.ts b/editor/src/core/workers/parser-printer/parse-cache-utils.worker.ts index 463be361a299..7687e09f1cc3 100644 --- a/editor/src/core/workers/parser-printer/parse-cache-utils.worker.ts +++ b/editor/src/core/workers/parser-printer/parse-cache-utils.worker.ts @@ -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' @@ -66,7 +68,7 @@ export async function getParseResultFromCache( file: ParseFile, parsingCacheOptions: ParseCacheOptions, ): Promise { - const { filename, content } = file + const { filename, content, versionNumber } = file if (!shouldUseCacheForFile(filename, parsingCacheOptions)) { return null } @@ -74,9 +76,9 @@ export async function getParseResultFromCache( //check localforage for cache const cachedResult = await getParseCacheStore().getItem(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 @@ -84,7 +86,7 @@ export async function getParseResultFromCache( export async function storeParseResultInCache( file: ParseFile, - result: ParseFileResult, + result: ParsedTextFile, parsingCacheOptions: ParseCacheOptions, ): Promise { const { filename, content } = file @@ -121,4 +123,4 @@ export async function clearParseCache(parsingCacheOptions: ParseCacheOptions) { }) } -type CachedParseResult = { [fileContent: string]: ParseFileResult } +type CachedParseResult = { [fileContent: string]: ParsedTextFile } diff --git a/editor/src/core/workers/parser-printer/parser-printer-worker.ts b/editor/src/core/workers/parser-printer/parser-printer-worker.ts index e89289472261..9de424eb7f26 100644 --- a/editor/src/core/workers/parser-printer/parser-printer-worker.ts +++ b/editor/src/core/workers/parser-printer/parser-printer-worker.ts @@ -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