-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: moved revalidate timings out of the prerender manifest
- Loading branch information
Showing
7 changed files
with
223 additions
and
47 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
61 changes: 61 additions & 0 deletions
61
packages/next/src/server/lib/incremental-cache/shared-revalidate-timings.test.ts
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { SharedRevalidateTimings } from './shared-revalidate-timings' | ||
|
||
describe('SharedRevalidateTimings', () => { | ||
let sharedRevalidateTimings: SharedRevalidateTimings | ||
let prerenderManifest | ||
|
||
beforeEach(() => { | ||
prerenderManifest = { | ||
routes: { | ||
'/route1': { | ||
initialRevalidateSeconds: 10, | ||
dataRoute: null, | ||
srcRoute: null, | ||
prefetchDataRoute: null, | ||
experimentalPPR: undefined, | ||
}, | ||
'/route2': { | ||
initialRevalidateSeconds: 20, | ||
dataRoute: null, | ||
srcRoute: null, | ||
prefetchDataRoute: null, | ||
experimentalPPR: undefined, | ||
}, | ||
}, | ||
} | ||
sharedRevalidateTimings = new SharedRevalidateTimings(prerenderManifest) | ||
}) | ||
|
||
afterEach(() => { | ||
sharedRevalidateTimings.clear() | ||
}) | ||
|
||
it('should get revalidate timing from in-memory cache', () => { | ||
sharedRevalidateTimings.set('/route1', 15) | ||
const revalidate = sharedRevalidateTimings.get('/route1') | ||
expect(revalidate).toBe(15) | ||
}) | ||
|
||
it('should get revalidate timing from prerender manifest if not in cache', () => { | ||
const revalidate = sharedRevalidateTimings.get('/route2') | ||
expect(revalidate).toBe(20) | ||
}) | ||
|
||
it('should return undefined if revalidate timing not found', () => { | ||
const revalidate = sharedRevalidateTimings.get('/route3') | ||
expect(revalidate).toBeUndefined() | ||
}) | ||
|
||
it('should set revalidate timing in cache', () => { | ||
sharedRevalidateTimings.set('/route3', 30) | ||
const revalidate = sharedRevalidateTimings.get('/route3') | ||
expect(revalidate).toBe(30) | ||
}) | ||
|
||
it('should clear the in-memory cache', () => { | ||
sharedRevalidateTimings.set('/route3', 30) | ||
sharedRevalidateTimings.clear() | ||
const revalidate = sharedRevalidateTimings.get('/route3') | ||
expect(revalidate).toBeUndefined() | ||
}) | ||
}) |
63 changes: 63 additions & 0 deletions
63
packages/next/src/server/lib/incremental-cache/shared-revalidate-timings.ts
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import type { PrerenderManifest } from '../../../build' | ||
import type { Revalidate } from '../revalidate' | ||
|
||
/** | ||
* A shared cache of revalidate timings for routes. This cache is used so we | ||
* don't have to modify the prerender manifest when we want to update the | ||
* revalidate timings for a route. | ||
*/ | ||
export class SharedRevalidateTimings { | ||
/** | ||
* The in-memory cache of revalidate timings for routes. This cache is | ||
* populated when the cache is updated with new timings. | ||
*/ | ||
private static readonly timings = new Map<string, Revalidate>() | ||
|
||
constructor( | ||
/** | ||
* The prerender manifest that contains the initial revalidate timings for | ||
* routes. | ||
*/ | ||
private readonly prerenderManifest: Pick<PrerenderManifest, 'routes'> | ||
) {} | ||
|
||
/** | ||
* Try to get the revalidate timings for a route. This will first try to get | ||
* the timings from the in-memory cache. If the timings are not present in the | ||
* in-memory cache, then the timings will be sourced from the prerender | ||
* manifest. | ||
* | ||
* @param route the route to get the revalidate timings for | ||
* @returns the revalidate timings for the route, or undefined if the timings | ||
* are not present in the in-memory cache or the prerender manifest | ||
*/ | ||
public get(route: string): Revalidate | undefined { | ||
// This is a copy on write cache that is updated when the cache is updated. | ||
// If the cache is never written to, then the timings will be sourced from | ||
// the prerender manifest. | ||
let revalidate = SharedRevalidateTimings.timings.get(route) | ||
if (typeof revalidate !== 'undefined') return revalidate | ||
|
||
revalidate = this.prerenderManifest.routes[route]?.initialRevalidateSeconds | ||
if (typeof revalidate !== 'undefined') return revalidate | ||
|
||
return undefined | ||
} | ||
|
||
/** | ||
* Set the revalidate timings for a route. | ||
* | ||
* @param route the route to set the revalidate timings for | ||
* @param revalidate the revalidate timings for the route | ||
*/ | ||
public set(route: string, revalidate: Revalidate) { | ||
SharedRevalidateTimings.timings.set(route, revalidate) | ||
} | ||
|
||
/** | ||
* Clear the in-memory cache of revalidate timings for routes. | ||
*/ | ||
public clear() { | ||
SharedRevalidateTimings.timings.clear() | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { toRoute } from './to-route' | ||
|
||
describe('toRoute Function', () => { | ||
it('should remove trailing slash', () => { | ||
const result = toRoute('/example/') | ||
expect(result).toBe('/example') | ||
}) | ||
|
||
it('should remove trailing `/index`', () => { | ||
const result = toRoute('/example/index') | ||
expect(result).toBe('/example') | ||
}) | ||
|
||
it('should return `/` when input is `/index`', () => { | ||
const result = toRoute('/index') | ||
expect(result).toBe('/') | ||
}) | ||
|
||
it('should return `/` when input is `/index/`', () => { | ||
const result = toRoute('/index/') | ||
expect(result).toBe('/') | ||
}) | ||
|
||
it('should return `/` when input is only a slash', () => { | ||
const result = toRoute('/') | ||
expect(result).toBe('/') | ||
}) | ||
|
||
it('should return `/` when input is empty', () => { | ||
const result = toRoute('') | ||
expect(result).toBe('/') | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* This transforms a URL pathname into a route. It removes any trailing slashes | ||
* and the `/index` suffix. | ||
* | ||
* @param {string} pathname - The URL path that needs to be optimized. | ||
* @returns {string} - The route | ||
* | ||
* @example | ||
* // returns '/example' | ||
* toRoute('/example/index/'); | ||
* | ||
* @example | ||
* // returns '/example' | ||
* toRoute('/example/'); | ||
* | ||
* @example | ||
* // returns '/' | ||
* toRoute('/index/'); | ||
* | ||
* @example | ||
* // returns '/' | ||
* toRoute('/'); | ||
*/ | ||
export function toRoute(pathname: string): string { | ||
return pathname.replace(/(?:\/index)?\/?$/, '') || '/' | ||
} |
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