Skip to content

Commit

Permalink
feat: handle query string for filesystem cache rx-angular#1690
Browse files Browse the repository at this point in the history
  • Loading branch information
maxisam committed Aug 23, 2024
1 parent bca4374 commit 116c288
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
12 changes: 8 additions & 4 deletions libs/isr/server/src/cache-handlers/filesystem-cache-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'), '/');
}

0 comments on commit 116c288

Please sign in to comment.