Skip to content

Commit

Permalink
Invalidate routes with dynamic parts (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
ofhouse committed Oct 30, 2021
1 parent 0362875 commit a52d2a4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ describe('deploy-trigger', () => {
describe('get or create manifest', () => {
let bucket: BucketHandler;
const fileNames = [
'_nest/static/chunks/b5500a63de92ae71a2560a1f3ee9c7923c1de4ef.1f52a3ec41a5d5095e70.js',
'_nest/static/chunks/framework.972e47ad649981044547.js',
'_nest/static/chunks/pages/[teamSlug]/[id]-08ca39a64982590c011d.js',
'_next/static/chunks/b5500a63de92ae71a2560a1f3ee9c7923c1de4ef.1f52a3ec41a5d5095e70.js',
'_next/static/chunks/framework.972e47ad649981044547.js',
'_next/static/chunks/pages/[teamSlug]/[id]-08ca39a64982590c011d.js',
'404',
];

Expand Down
33 changes: 32 additions & 1 deletion packages/deploy-trigger/src/__test__/update-manifest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '../constants';
import { getOrCreateManifest } from '../get-or-create-manifest';
import { Manifest } from '../types';
import { updateManifest } from '../update-manifest';
import { getInvalidationKeys, updateManifest } from '../update-manifest';
import { generateRandomBuildId } from '../utils';
import { addFilesToS3Bucket, generateS3ClientForTesting } from './utils';

Expand Down Expand Up @@ -266,3 +266,34 @@ describe('deploy-trigger', () => {
expect(invalidate.sort()).toEqual(['/dynamic-route*'].sort());
});
});

describe('[deploy-trigger] getInvalidationKeys', () => {
test.each([
['test/[slug]', 'test*'],
['test/[slug]/abc', 'test*'],
['test/[...slug]', 'test*'],
['test/[[...slug]]', 'test*'],
['test/[testId]/index', 'test*'],
['test/[testId]/[otherId]', 'test*'],
])('Generated invalidationKey from %s should be %s', (input, output) => {
const invalidationKeys = getInvalidationKeys([input]);

expect(invalidationKeys).toEqual([output]);
});

test('Root index route', () => {
const invalidationKeys = getInvalidationKeys(['index']);

expect(invalidationKeys).toEqual(['/', '/?*']);
});

test('Skip routes from _next', () => {
const invalidationKeys = getInvalidationKeys([
'_next/abc',
'_next/def',
'_next/ghi',
]);

expect(invalidationKeys).toEqual([]);
});
});
17 changes: 16 additions & 1 deletion packages/deploy-trigger/src/update-manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { S3 } from 'aws-sdk';
import { expireTagKey, expireTagValue } from './constants';
import { ExpireValue, FileResult, Manifest, ManifestFile } from './types';

// Regex mather for paths with dynamic parts
// e.g. - [id]/index
// - test/[...slug]/index
const dynamicPartMatcher = new RegExp(/\/?\[[^\]]+\].*/);

async function expireFiles(s3: S3, bucketId: string, files: string[]) {
// Set the expiration tags on the expired files
for (const file of files) {
Expand Down Expand Up @@ -107,6 +112,14 @@ function getInvalidationKeys(files: string[]) {
continue;
}

// Check if the a dynamic part is in the filename
// e.g. - [abc]/index -> *
// - test/[...slug] -> test/*
if (file.match(dynamicPartMatcher)) {
invalidations.push(file.replace(dynamicPartMatcher, '*'));
continue;
}

// Default index routes
// /some/route/index -> /some/route*
if (file.endsWith('/index')) {
Expand Down Expand Up @@ -144,7 +157,7 @@ interface Response {
*
* It returns an array of keys that should be expired
*/
export async function updateManifest({
async function updateManifest({
files,
buildId,
s3,
Expand Down Expand Up @@ -272,3 +285,5 @@ export async function updateManifest({

return { expire, restore, manifest: newManifest, invalidate, deleted };
}

export { updateManifest, getInvalidationKeys };

0 comments on commit a52d2a4

Please sign in to comment.