From 75edba86ddd47e2a8633ea3a314c68d53d2aa437 Mon Sep 17 00:00:00 2001 From: Ike Saunders Date: Mon, 5 Aug 2024 09:52:20 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix=20redirects=20from=20draft?= =?UTF-8?q?=20gdoc=20deletion?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- adminSiteServer/apiRouter.ts | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/adminSiteServer/apiRouter.ts b/adminSiteServer/apiRouter.ts index 27c0f0e726c..6d606b4ddf7 100644 --- a/adminSiteServer/apiRouter.ts +++ b/adminSiteServer/apiRouter.ts @@ -2276,13 +2276,19 @@ deleteRouteWithRWTransaction(apiRouter, "/gdocs/:id", async (req, res, trx) => { // Assets have TTL of one week in Cloudflare. Add a redirect to make sure // the page is no longer accessible. // https://developers.cloudflare.com/pages/configuration/serving-pages/#asset-retention - await db.knexRawInsert( - trx, - `INSERT INTO redirects (source, target, ttl) - VALUES (?, ?, DATE_ADD(NOW(), INTERVAL 8 DAY))`, - [getCanonicalUrl("", gdoc), "/"] - ) - await triggerStaticBuild(res.locals.user, `Deleting ${gdoc.slug}`) + const gdocSlug = getCanonicalUrl("", gdoc) + if (gdoc.published) { + if (gdocSlug && gdocSlug !== "/") { + console.log(`Creating redirect for "${gdocSlug}" to "/"`) + await db.knexRawInsert( + trx, + `INSERT INTO redirects (source, target, ttl) + VALUES (?, ?, DATE_ADD(NOW(), INTERVAL 8 DAY))`, + [gdocSlug, "/"] + ) + } + await triggerStaticBuild(res.locals.user, `Deleting ${gdocSlug}`) + } return {} })