From 37ae1dbd6ef68e38e87726a156343247a469b8fd Mon Sep 17 00:00:00 2001 From: Darshan Date: Tue, 26 Aug 2025 20:58:14 +0530 Subject: [PATCH] add: redirections. --- .../database-[database]/[...rest]/+page.ts | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/routes/(console)/project-[region]-[project]/databases/database-[database]/[...rest]/+page.ts diff --git a/src/routes/(console)/project-[region]-[project]/databases/database-[database]/[...rest]/+page.ts b/src/routes/(console)/project-[region]-[project]/databases/database-[database]/[...rest]/+page.ts new file mode 100644 index 000000000..361b537c0 --- /dev/null +++ b/src/routes/(console)/project-[region]-[project]/databases/database-[database]/[...rest]/+page.ts @@ -0,0 +1,46 @@ +import { base } from '$app/paths'; +import type { PageLoad } from './$types'; +import { redirect } from '@sveltejs/kit'; +import { AppwriteException } from '@appwrite.io/console'; + +const LEGACY_ROUTE_MAPPINGS = [ + { pattern: /^collections$/, replacement: 'tables' }, + { pattern: /^collection-([^/]+)/, replacement: 'table-$1' }, + // document detail page redirect to their parent table + { pattern: /^document-([^/]+)/, replacement: 'table-$1' }, + // attribute detail redirect to table columns + { pattern: /^attribute-([^/]+)/, replacement: 'table-$1/columns' }, + // attributes list page redirect to table columns + { pattern: /^attributes$/, replacement: 'columns' } +] as const; + +function isLegacyRoute(segments: string[]): boolean { + return segments.some((segment) => + LEGACY_ROUTE_MAPPINGS.some((mapping) => mapping.pattern.test(segment)) + ); +} + +function rewriteLegacySegments(segments: string[]): string[] { + return segments.map((segment) => { + for (const mapping of LEGACY_ROUTE_MAPPINGS) { + if (mapping.pattern.test(segment)) { + return segment.replace(mapping.pattern, mapping.replacement); + } + } + return segment; + }); +} + +export const load: PageLoad = async ({ params, url }) => { + const restSegments = params.rest ? params.rest.split('/').filter(Boolean) : []; + const baseUrl = `${base}/project-${params.region}-${params.project}/databases/database-${params.database}`; + + if (isLegacyRoute(restSegments)) { + const rewrittenSegments = rewriteLegacySegments(restSegments); + const newPath = `${baseUrl}/${rewrittenSegments.join('/')}`; + + redirect(308, newPath + url.search); + } + + throw new AppwriteException('Not Found', 404); +};