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({