fix(site): isolate template layout cache by organization (#32952)
> 🤖 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.
<details>
<summary>Implementation and regression notes</summary>
- 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.
</details>
This commit is contained in:
1 parent
e7ed38fc53
commit
49b1c7e294
2 files changed
+47
-1
No files matched your search
@@ -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(<TemplateLayout />, {
|
||||
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(<TemplateLayout />, {
|
||||
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);
|
||||
});
|
||||
@@ -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<React.PropsWithChildren> = ({
|
||||
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({
|
||||
|
||||
Reference in new issue
Block a user