From 49b1c7e2944fc9f68f1882dc502eb4dfef0fefa7 Mon Sep 17 00:00:00 2001 From: Jake Howell Date: Fri, 4 Dec 2026 11:05:47 +1100 Subject: [PATCH] fix(site): isolate template layout cache by organization (#32952) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit > 🤖 This PR was modified by Coder Agents on behalf of Jake Howell. Fix same-named templates sharing layout data across organizations, including stale content and Create workspace targets during loading and history navigation. Keep the layout's composite result separate from ordinary template query data.
Implementation and regression notes - Use `[...templateByNameKey(organizationName, templateName), "layout"]`. The shared prefix preserves by-name invalidation, while the suffix separates `{ template, activeVersion, permissions }` from plain `Template` entries. - Focused tests verify that changing organizations requests the correct template and that layout data stays separate from by-name and by-ID template caches, using shared fixtures and handlers. - Restoring the old key fails the organization-switch and by-ID cases. Removing the layout suffix fails the by-name case. - All 48 TemplatePage tests, `pnpm format`, `pnpm check`, `pnpm lint`, and `make lint/emdash` pass. Independent frontend review has no remaining findings.
--- .../TemplatePage/TemplateLayout.test.tsx | 45 +++++++++++++++++++ .../src/pages/TemplatePage/TemplateLayout.tsx | 3 +- 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 site/src/pages/TemplatePage/TemplateLayout.test.tsx diff --git a/site/src/pages/TemplatePage/TemplateLayout.test.tsx b/site/src/pages/TemplatePage/TemplateLayout.test.tsx new file mode 100644 index 00000000000..0dffe1074b0 --- /dev/null +++ b/site/src/pages/TemplatePage/TemplateLayout.test.tsx @@ -0,0 +1,45 @@ +import { act, screen, waitFor } from "@testing-library/react"; +import { API } from "#/api/api"; +import { template, templateByName } from "#/api/queries/templates"; +import { MockTemplate } from "#/testHelpers/entities"; +import { renderWithAuth } from "#/testHelpers/renderHelpers"; +import { TemplateLayout } from "./TemplateLayout"; + +afterEach(() => { + vi.restoreAllMocks(); +}); + +test("fetches the same-named template when the organization changes", async () => { + const getTemplate = vi.spyOn(API, "getTemplateByName"); + const { router } = renderWithAuth(, { + path: "/templates/:organization/:template", + route: `/templates/research/${MockTemplate.name}`, + }); + await screen.findByRole("link", { name: "Source code" }); + expect(getTemplate).toHaveBeenCalledWith("research", MockTemplate.name); + + await act(() => router.navigate(`/templates/platform/${MockTemplate.name}`)); + await waitFor(() => + expect(getTemplate).toHaveBeenLastCalledWith("platform", MockTemplate.name), + ); +}); + +test.each([ + ["by name", templateByName("default", MockTemplate.name)], + ["by ID", template(MockTemplate.id)], +])("keeps layout data separate from templates cached %s", async (_, query) => { + const getVersion = vi.spyOn(API, "getTemplateVersion"); + const { queryClient } = renderWithAuth(, { + path: "/templates/:template", + route: `/templates/${MockTemplate.name}`, + }); + queryClient.setQueryDefaults(query.queryKey, { + staleTime: Number.POSITIVE_INFINITY, + gcTime: Number.POSITIVE_INFINITY, + }); + queryClient.setQueryData(query.queryKey, MockTemplate); + + await screen.findByRole("link", { name: "Source code" }); + expect(getVersion).toHaveBeenCalledWith(MockTemplate.active_version_id); + expect(queryClient.getQueryData(query.queryKey)).toEqual(MockTemplate); +}); diff --git a/site/src/pages/TemplatePage/TemplateLayout.tsx b/site/src/pages/TemplatePage/TemplateLayout.tsx index d6ed4841afe..1f53572c314 100644 --- a/site/src/pages/TemplatePage/TemplateLayout.tsx +++ b/site/src/pages/TemplatePage/TemplateLayout.tsx @@ -3,6 +3,7 @@ import { useQuery } from "react-query"; import { Outlet, useLocation, useNavigate, useParams } from "react-router"; import { API } from "#/api/api"; import { checkAuthorization } from "#/api/queries/authCheck"; +import { templateByNameKey } from "#/api/queries/templates"; import { ErrorAlert } from "#/components/Alert/ErrorAlert"; import { Loader } from "#/components/Loader/Loader"; import { Margins } from "#/components/Margins/Margins"; @@ -60,7 +61,7 @@ export const TemplateLayout: React.FC = ({ const { organization: organizationName = "default", template: templateName } = useParams() as { organization?: string; template: string }; const { data, error, isLoading } = useQuery({ - queryKey: ["template", templateName], + queryKey: [...templateByNameKey(organizationName, templateName), "layout"], queryFn: () => fetchTemplate(organizationName, templateName), }); const workspacePermissionsQuery = useQuery({