Skip to content

Commit 6026820

Browse files
authored
Invalidate routes with dynamic parts (#229)
1 parent ea7fde5 commit 6026820

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

packages/deploy-trigger/src/__test__/get-or-create-manifest.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ describe('deploy-trigger', () => {
2020
describe('get or create manifest', () => {
2121
let bucket: BucketHandler;
2222
const fileNames = [
23-
'_nest/static/chunks/b5500a63de92ae71a2560a1f3ee9c7923c1de4ef.1f52a3ec41a5d5095e70.js',
24-
'_nest/static/chunks/framework.972e47ad649981044547.js',
25-
'_nest/static/chunks/pages/[teamSlug]/[id]-08ca39a64982590c011d.js',
23+
'_next/static/chunks/b5500a63de92ae71a2560a1f3ee9c7923c1de4ef.1f52a3ec41a5d5095e70.js',
24+
'_next/static/chunks/framework.972e47ad649981044547.js',
25+
'_next/static/chunks/pages/[teamSlug]/[id]-08ca39a64982590c011d.js',
2626
'404',
2727
];
2828

packages/deploy-trigger/src/__test__/update-manifest.test.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from '../constants';
1212
import { getOrCreateManifest } from '../get-or-create-manifest';
1313
import { Manifest } from '../types';
14-
import { updateManifest } from '../update-manifest';
14+
import { getInvalidationKeys, updateManifest } from '../update-manifest';
1515
import { generateRandomBuildId } from '../utils';
1616
import { addFilesToS3Bucket, generateS3ClientForTesting } from './utils';
1717

@@ -266,3 +266,34 @@ describe('deploy-trigger', () => {
266266
expect(invalidate.sort()).toEqual(['/dynamic-route*'].sort());
267267
});
268268
});
269+
270+
describe('[deploy-trigger] getInvalidationKeys', () => {
271+
test.each([
272+
['test/[slug]', 'test*'],
273+
['test/[slug]/abc', 'test*'],
274+
['test/[...slug]', 'test*'],
275+
['test/[[...slug]]', 'test*'],
276+
['test/[testId]/index', 'test*'],
277+
['test/[testId]/[otherId]', 'test*'],
278+
])('Generated invalidationKey from %s should be %s', (input, output) => {
279+
const invalidationKeys = getInvalidationKeys([input]);
280+
281+
expect(invalidationKeys).toEqual([output]);
282+
});
283+
284+
test('Root index route', () => {
285+
const invalidationKeys = getInvalidationKeys(['index']);
286+
287+
expect(invalidationKeys).toEqual(['/', '/?*']);
288+
});
289+
290+
test('Skip routes from _next', () => {
291+
const invalidationKeys = getInvalidationKeys([
292+
'_next/abc',
293+
'_next/def',
294+
'_next/ghi',
295+
]);
296+
297+
expect(invalidationKeys).toEqual([]);
298+
});
299+
});

packages/deploy-trigger/src/update-manifest.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import { S3 } from 'aws-sdk';
33
import { expireTagKey, expireTagValue } from './constants';
44
import { ExpireValue, FileResult, Manifest, ManifestFile } from './types';
55

6+
// Regex mather for paths with dynamic parts
7+
// e.g. - [id]/index
8+
// - test/[...slug]/index
9+
const dynamicPartMatcher = new RegExp(/\/?\[[^\]]+\].*/);
10+
611
async function expireFiles(s3: S3, bucketId: string, files: string[]) {
712
// Set the expiration tags on the expired files
813
for (const file of files) {
@@ -107,6 +112,14 @@ function getInvalidationKeys(files: string[]) {
107112
continue;
108113
}
109114

115+
// Check if the a dynamic part is in the filename
116+
// e.g. - [abc]/index -> *
117+
// - test/[...slug] -> test/*
118+
if (file.match(dynamicPartMatcher)) {
119+
invalidations.push(file.replace(dynamicPartMatcher, '*'));
120+
continue;
121+
}
122+
110123
// Default index routes
111124
// /some/route/index -> /some/route*
112125
if (file.endsWith('/index')) {
@@ -144,7 +157,7 @@ interface Response {
144157
*
145158
* It returns an array of keys that should be expired
146159
*/
147-
export async function updateManifest({
160+
async function updateManifest({
148161
files,
149162
buildId,
150163
s3,
@@ -271,3 +284,5 @@ export async function updateManifest({
271284

272285
return { expire, restore, manifest: newManifest, invalidate, deleted };
273286
}
287+
288+
export { updateManifest, getInvalidationKeys };

0 commit comments

Comments
 (0)