diff --git a/scripts/origin/cache-purge.js b/scripts/origin/cache-purge.js index 42d22cd..1672b62 100644 --- a/scripts/origin/cache-purge.js +++ b/scripts/origin/cache-purge.js @@ -48,7 +48,6 @@ rl.on('line', line => { !['', '.', './'].includes(directoryName) && !pathsToPurge.includes(directoryName) ) { - pathsToPurge.push(directoryName); pathsToPurge.push(directoryName + '/'); } }); diff --git a/src/util.ts b/src/util.ts index 406e117..15239bb 100644 --- a/src/util.ts +++ b/src/util.ts @@ -79,6 +79,21 @@ export function mapUrlPathToBucketPath( return bucketPath !== undefined ? decodeURIComponent(bucketPath) : undefined; } +/** + * Get all of the directories beginning with 'latest' in a + * directory + * @param prefix Directory to look through + */ +function getAllLatestDirectories(prefix: string): Set { + const paths = new Set(); + for (const [k] of REDIRECT_MAP) { + if (k.startsWith(`${prefix}/latest`)) { + paths.add(k.substring(prefix.length) + '/'); + } + } + return paths; +} + /** * Maps a path in the R2 bucket to the urls used to access it * @param bucketPath Path to map @@ -91,19 +106,65 @@ export function mapBucketPathToUrlPath( env: Pick ): string[] | undefined { // @TODO: Use a switch statement or a Design Pattern here + // I strongly dislike the workers cache api if (bucketPath.startsWith(DIST_PATH_PREFIX)) { - const path = bucketPath.substring(15); - return [`/dist${path}`, `/download/releases${path}`]; - } else if ( - bucketPath.startsWith(API_PATH_PREFIX) || - bucketPath.startsWith('nodejs/docs/latest/api') - ) { - const path = bucketPath.substring(22); - return [`/api${path}`, `/docs/latest/api${path}`]; + const path = bucketPath.substring(DIST_PATH_PREFIX.length); + + const possibleUrlPaths = new Set(); + + // Purge directory listing of /dist/ and /download/release/ + possibleUrlPaths.add('/dist/'); + possibleUrlPaths.add('/download/release/'); + + // Purge whatever the paths we're updating + possibleUrlPaths.add(`/dist${path}`); + possibleUrlPaths.add(`/download/release${path}`); + + // Purge all of the directory listings of folders starting with 'latest' + // (e.g. `/dist/latest-hydrogen`) + // Bit of a hack, but I think this is the best we can do. The redirects + // we have in `src/constants/redirectLinks.json` will be out of date since + // a new version was uploaded and thus the latest has changed. We can't + // really determine the new latest here unless we run something + // similar to the `scripts/update-redirect-links.js` script here. + const latestDirectories = getAllLatestDirectories('nodejs/release'); + + for (const directory of latestDirectories) { + possibleUrlPaths.add(`/dist${directory}`); + possibleUrlPaths.add(`/download/release${directory}`); + } + + return [...possibleUrlPaths]; } else if (bucketPath.startsWith(DOCS_PATH_PREFIX)) { - return [`/docs${bucketPath.substring(11)}`]; + const path = bucketPath.substring(DOCS_PATH_PREFIX.length); + + const possibleUrlPaths = new Set(); + + // Purge directory listings for /docs/ and /download/docs/ + possibleUrlPaths.add('/docs/'); + possibleUrlPaths.add('/download/docs/'); + + possibleUrlPaths.add(`/docs${path}`); + possibleUrlPaths.add(`/download/docs${path}`); + + if (bucketPath.includes('/api/')) { + // Html file, purge it + possibleUrlPaths.add(`/api${path.substring('api'.length)}`); + } + + // Purge all of the directory listings of folders starting with 'latest' + // (e.g. `/docs/latest`) + // Refer to previous call for explanation + const latestDirectories = getAllLatestDirectories('nodejs/docs'); + + for (const directory of latestDirectories) { + possibleUrlPaths.add(`/docs${directory}`); + possibleUrlPaths.add(`/download/docs${directory}`); + } + + return [...possibleUrlPaths]; } else if (bucketPath.startsWith(DOWNLOAD_PATH_PREFIX)) { - return [`/download${bucketPath.substring(6)}`]; + return [`/download${bucketPath.substring(DOWNLOAD_PATH_PREFIX.length)}`]; } else if (bucketPath.startsWith('metrics')) { return ['/' + bucketPath]; }