Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

feat: append response header rules for last-match precedence (#6460)

Oleg Isonene1e5bc152ea4
diff --git a/apps/builder/app/shared/instance-utils/fragment.test.tsx b/apps/builder/app/shared/instance-utils/fragment.test.tsx
index 1a6bc06c09..7b70a88e02 100644
--- a/apps/builder/app/shared/instance-utils/fragment.test.tsx
+++ b/apps/builder/app/shared/instance-utils/fragment.test.tsx
@@ -1791,16 +1791,16 @@ describe("insert webstudio fragment copy", () => {
});
expect(Array.from(data.styleSourceSelections.values())).toEqual([
{
- instanceId: expect.not.stringMatching("box"),
- values: [expect.not.stringMatching("localId"), "tokenId"],
+ instanceId: expect.not.stringMatching(/^box$/),
+ values: [expect.not.stringMatching(/^localId$/), "tokenId"],
},
]);
expect(Array.from(data.styleSources.values())).toEqual([
- { id: expect.not.stringMatching("localId"), type: "local" },
+ { id: expect.not.stringMatching(/^localId$/), type: "local" },
]);
expect(Array.from(data.styles.values())).toEqual([
{
- styleSourceId: expect.not.stringMatching("localId"),
+ styleSourceId: expect.not.stringMatching(/^localId$/),
breakpointId: "base",
property: "color",
value: { type: "keyword", value: "red" },
diff --git a/apps/builder/app/shared/project-settings/section-headers.test.tsx b/apps/builder/app/shared/project-settings/section-headers.test.tsx
index f4a94403cb..c51038b5d2 100644
--- a/apps/builder/app/shared/project-settings/section-headers.test.tsx
+++ b/apps/builder/app/shared/project-settings/section-headers.test.tsx
@@ -283,8 +283,8 @@ test("adds an arbitrary header and displays existing custom headers", async () =
input: {
meta: {
customHeaders: [
- { name: "Access-Control-Allow-Origin", value: "*" },
{ name: "Cache-Control", value: "no-store" },
+ { name: "Access-Control-Allow-Origin", value: "*" },
],
},
},
diff --git a/apps/builder/app/shared/project-settings/section-headers.tsx b/apps/builder/app/shared/project-settings/section-headers.tsx
index 2a4122c12d..2660482640 100644
--- a/apps/builder/app/shared/project-settings/section-headers.tsx
+++ b/apps/builder/app/shared/project-settings/section-headers.tsx
@@ -98,7 +98,9 @@ export const SectionHeaders = ({
<>
<Text>
Set response headers for all paths or a route. Routes use the
- same syntax as Authentication, including :params and *.
+ same syntax as Authentication, including :params and *. If
+ multiple rules set the same header on a path, the last matching
+ rule in this list wins.
</Text>
<br />
<Text>
diff --git a/packages/project-build/src/runtime/project-settings.test.ts b/packages/project-build/src/runtime/project-settings.test.ts
index c0a6f0c0fd..3912369539 100644
--- a/packages/project-build/src/runtime/project-settings.test.ts
+++ b/packages/project-build/src/runtime/project-settings.test.ts
@@ -103,14 +103,14 @@ describe("project settings runtime", () => {
op: "replace",
path: ["meta", "customHeaders"],
value: [
- { route: "/", name: "Cache-Control", value: "private" },
allPaths,
+ { route: "/", name: "Cache-Control", value: "private" },
],
},
]);
state.projectSettings!.meta.customHeaders = [
- { route: "/", name: "Cache-Control", value: "private" },
allPaths,
+ { route: "/", name: "Cache-Control", value: "private" },
];
expect(listResponseHeaders(state).headers).toEqual(
state.projectSettings!.meta.customHeaders
diff --git a/packages/sdk/src/schema/response-headers.test.ts b/packages/sdk/src/schema/response-headers.test.ts
index 1597afcbd6..df1240946b 100644
--- a/packages/sdk/src/schema/response-headers.test.ts
+++ b/packages/sdk/src/schema/response-headers.test.ts
@@ -61,8 +61,8 @@ describe("response header settings", () => {
{ name: "cache-control", value: "updated" }
);
expect(updated).toEqual([
- { name: "cache-control", value: "updated" },
headers[1],
+ { name: "cache-control", value: "updated" },
]);
expect(
editCustomResponseHeaders(
@@ -78,6 +78,18 @@ describe("response header settings", () => {
).toThrow();
});
+ test("new rules appear last, where they take precedence on overlap", () => {
+ const global = { name: "Cache-Control", value: "private" };
+ const route = {
+ route: "/public",
+ name: "Cache-Control",
+ value: "public, max-age=60",
+ };
+ expect(
+ editCustomResponseHeaders([global], customResponseHeaderKey(route), route)
+ ).toEqual([global, route]);
+ });
+
test("a rule requires a nonempty string value", () => {
for (const value of [null, "", " "]) {
expect(
diff --git a/packages/sdk/src/schema/response-headers.ts b/packages/sdk/src/schema/response-headers.ts
index 2014c24a64..192631af5f 100644
--- a/packages/sdk/src/schema/response-headers.ts
+++ b/packages/sdk/src/schema/response-headers.ts
@@ -135,7 +135,8 @@ export const editCustomResponseHeaders = (
(header) => customResponseHeaderKey(header) !== key
);
if (next !== undefined) {
- updated.unshift(next);
+ // Later matching rules override earlier ones when a site is published.
+ updated.push(next);
}
return customResponseHeaders.parse(updated);
};