Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: merge query for no-hash routes #286

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/iframe-coordinator/src/HostRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,18 @@ function applyRoute(urlStr: string, route: string): string {
const newUrl = new URL(urlStr, window.location.href);
if (newUrl.hash) {
const baseClientRoute = stripTrailingSlash(newUrl.hash);
// This supports non-standard query params in hash route directly
newUrl.hash = `${baseClientRoute}/${route}`;
} else {
const baseClientPath = stripTrailingSlash(newUrl.pathname);
newUrl.pathname = `${baseClientPath}/${route}`;
// Here, we merge host query params and route query params
const routeUrl = new URL(route, window.location.href);
newUrl.pathname = `${baseClientPath}${routeUrl.pathname}`;
const query = new URLSearchParams(newUrl.search);
for (const [key, value] of routeUrl.searchParams) {
query.set(key, value);
}
newUrl.search = query.toString();
}
return newUrl.toString();
}
Expand Down
36 changes: 36 additions & 0 deletions packages/iframe-coordinator/src/specs/HostRouter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ describe("HostRouter", () => {
url: "http://example.com/my/pushstate/app/?query=works",
assignedRoute: "noHash",
},
noClientHashNoQuery: {
url: "http://example.com/my/pushstate/app/noquery/",
assignedRoute: "noHash/noQuery",
},
noClientHashMergeQuery: {
url: "http://example.com/my/pushstate/app/mergequery/?alpha=true",
assignedRoute: "noHash/mergeQuery",
},
withSandboxAndAllow: {
url: clientUrl,
assignedRoute: "route/two",
Expand Down Expand Up @@ -85,6 +93,34 @@ describe("HostRouter", () => {
expect(clientInfo.id).toBe("noClientHash");
});

it("should append to the path when the client url has no hash and no query", () => {
const clientInfo = hostRouter.getClientTarget(
"noHash/noQuery/foo/bar?query=works",
);
if (!clientInfo) {
fail();
return;
}
expect(clientInfo.url).toBe(
"http://example.com/my/pushstate/app/noquery/foo/bar?query=works",
);
expect(clientInfo.id).toBe("noClientHashNoQuery");
});

it("should append to the path when the client url has no hash and merging query", () => {
const clientInfo = hostRouter.getClientTarget(
"noHash/mergeQuery/foo/bar?beta=true",
);
if (!clientInfo) {
fail();
return;
}
expect(clientInfo.url).toBe(
"http://example.com/my/pushstate/app/mergequery/foo/bar?alpha=true&beta=true",
);
expect(clientInfo.id).toBe("noClientHashMergeQuery");
});

it('should return "allow" and "sandbox" config options if they exist', () => {
const clientInfo = hostRouter.getClientTarget("route/two/");
if (!clientInfo) {
Expand Down
Loading