diff --git a/libs/isr/server/src/cache-handlers/filesystem-cache-handler.spec.ts b/libs/isr/server/src/cache-handlers/filesystem-cache-handler.spec.ts new file mode 100644 index 000000000..a43092412 --- /dev/null +++ b/libs/isr/server/src/cache-handlers/filesystem-cache-handler.spec.ts @@ -0,0 +1,35 @@ +import { + convertFileNameToRoute, + convertRouteToFileName, +} from './filesystem-cache-handler'; + +// Use the functions as needed +describe('Route and File Name Conversion', () => { + describe('convertRouteToFileName', () => { + it('should convert a simple route without query parameters', () => { + const route = '/users/profile'; + const expectedFileName = '__users__profile'; + expect(convertRouteToFileName(route)).toEqual(expectedFileName); + }); + + it('should convert a route with query parameters', () => { + const route = '/search?query=test'; + const expectedFileName = '__search++query=test'; + expect(convertRouteToFileName(route)).toEqual(expectedFileName); + }); + }); + + describe('convertFileNameToRoute', () => { + it('should convert a simple file name back to a route', () => { + const fileName = '__users__profile'; + const expectedRoute = '/users/profile'; + expect(convertFileNameToRoute(fileName)).toEqual(expectedRoute); + }); + + it('should convert a file name with "++" back to a route with a query parameter', () => { + const fileName = '__search++query=test'; + const expectedRoute = '/search?query=test'; + expect(convertFileNameToRoute(fileName)).toEqual(expectedRoute); + }); + }); +}); diff --git a/libs/isr/server/src/cache-handlers/filesystem-cache-handler.ts b/libs/isr/server/src/cache-handlers/filesystem-cache-handler.ts index a816ca3a4..e934b8f0c 100644 --- a/libs/isr/server/src/cache-handlers/filesystem-cache-handler.ts +++ b/libs/isr/server/src/cache-handlers/filesystem-cache-handler.ts @@ -338,16 +338,20 @@ function getFileFullPath(fileName: string, cacheFolderPath: string): string { * @param {string} route - The string representing the route to be converted into a file name. * @returns {string} The modified string representing the file name obtained by replacing '/' characters with '__'. */ -function convertRouteToFileName(route: string): string { +export function convertRouteToFileName(route: string): string { // replace all occurrences of '/' character in the 'route' string with '__' using regular expression - return route.replace(new RegExp('/', 'g'), '__'); + return route + .replace(new RegExp('/', 'g'), '__') + .replace(new RegExp('\\?', 'g'), '++'); } /** * This function takes a string parameter 'fileName' and replaces all '__' strings in it with '/' and returns the modified string. * @param fileName - The string representing the file name to be converted into a route. */ -function convertFileNameToRoute(fileName: string): string { +export function convertFileNameToRoute(fileName: string): string { // replace all occurrences of '__' string in the 'fileName' string with '/' using regular expression - return fileName.replace(new RegExp('__', 'g'), '/'); + return fileName + .replace(new RegExp('\\+\\+', 'g'), '?') + .replace(new RegExp('__', 'g'), '/'); }