@decocms/blocks-cli 7.77.11 → 7.77.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@decocms/blocks-cli",
3
- "version": "7.77.11",
3
+ "version": "7.77.12",
4
4
  "type": "module",
5
5
  "description": "Deco codegen (generate-blocks, generate-schema, generate-invoke) and Fresh-to-TanStack migration tooling",
6
6
  "repository": {
@@ -33,7 +33,7 @@
33
33
  "lint:unused": "knip"
34
34
  },
35
35
  "dependencies": {
36
- "@decocms/blocks": "7.77.11",
36
+ "@decocms/blocks": "7.77.12",
37
37
  "ts-morph": "^27.0.0",
38
38
  "tsx": "^4.22.5"
39
39
  },
@@ -409,7 +409,7 @@ describe("readDecofileFromDir — CSV redirects", () => {
409
409
  const { blocks } = readDecofileFromDir(blocksDir, { silent: true });
410
410
  const split = splitExactRedirects(blocks);
411
411
 
412
- expect(split.exact).toEqual([{ path: "/old", to: "/new", status: 301 }]);
412
+ expect(split.exact).toEqual([{ path: "/old", from: "/old", to: "/new", status: 301 }]);
413
413
  expect(JSON.stringify(split.blocks)).not.toContain('"/old"');
414
414
  expect(split.blocks["__csv_redirects__r.csv"]).toMatchObject({ redirects: [{ from: "/g/*" }] });
415
415
  });
@@ -467,7 +467,7 @@ describe("syncRedirectsToKv", () => {
467
467
  return { client: client as unknown as KvRestClient, store };
468
468
  }
469
469
 
470
- const rule = (path: string, to: string, status: 301 | 302 = 301) => ({ path, to, status });
470
+ const rule = (path: string, to: string, status: 301 | 307 = 301) => ({ path, to, status });
471
471
 
472
472
  it("adds new rules under redirect:<id>:<path>", async () => {
473
473
  const { client, store } = makeBulkClient();
@@ -83,6 +83,30 @@ describe("buildCsvRedirectBlocks", () => {
83
83
  expect(matchRedirect("/dup", map)).toMatchObject({ to: "/from-cms" });
84
84
  });
85
85
 
86
+ it("carries discardQueryParameters and the loader's forcePermanentRedirects", () => {
87
+ writeCsv("f.csv", "from,to\n/a,/b,true\n/c,/d\n/e,/f,temporary\n");
88
+ const blocks = {
89
+ x: {
90
+ __resolveType: "website/loaders/redirectsFromCsv.ts",
91
+ from: "f.csv",
92
+ forcePermanentRedirects: true,
93
+ },
94
+ };
95
+ const csvBlocks = buildCsvRedirectBlocks(blocks, { blocksDir, silent: true });
96
+ expect(csvBlocks["__csv_redirects__f.csv"]).toEqual({
97
+ __resolveType: "website/loaders/redirects.ts",
98
+ redirects: [
99
+ { from: "/a", to: "/b", type: "permanent", discardQueryParameters: true },
100
+ { from: "/c", to: "/d", type: "permanent" },
101
+ { from: "/e", to: "/f", type: "temporary" },
102
+ ],
103
+ });
104
+ const map = loadRedirects(csvBlocks);
105
+ expect(matchRedirect("/a", map)).toMatchObject({ status: 301, discardQueryParameters: true });
106
+ // Temporary under the default (legacy) semantics.
107
+ expect(matchRedirect("/e", map)).toMatchObject({ status: 302 });
108
+ });
109
+
86
110
  it("returns {} when no CSV loader is referenced", () => {
87
111
  const blocks = { a: { __resolveType: "website/loaders/redirect.ts" } };
88
112
  expect(buildCsvRedirectBlocks(blocks, { blocksDir, silent: true })).toEqual({});
@@ -20,8 +20,11 @@ import { parseRedirectsCsv } from "@decocms/blocks/sdk/redirects";
20
20
  const CSV_REDIRECT_RESOLVE_TYPE = "website/loaders/redirectsFromCsv.ts";
21
21
  const REDIRECTS_RESOLVE_TYPE = "website/loaders/redirects.ts";
22
22
 
23
- /** Recursively collect `from` CSV paths referenced by redirectsFromCsv nodes. */
24
- function collectCsvRefs(node: unknown, out: Set<string>): void {
23
+ /**
24
+ * Recursively collect the CSV paths referenced by redirectsFromCsv nodes, with
25
+ * the loader's `forcePermanentRedirects` flag (true if any referrer sets it).
26
+ */
27
+ function collectCsvRefs(node: unknown, out: Map<string, boolean>): void {
25
28
  if (!node || typeof node !== "object") return;
26
29
  if (Array.isArray(node)) {
27
30
  for (const item of node) collectCsvRefs(item, out);
@@ -29,7 +32,7 @@ function collectCsvRefs(node: unknown, out: Set<string>): void {
29
32
  }
30
33
  const obj = node as Record<string, unknown>;
31
34
  if (obj.__resolveType === CSV_REDIRECT_RESOLVE_TYPE && typeof obj.from === "string") {
32
- out.add(obj.from);
35
+ out.set(obj.from, (out.get(obj.from) ?? false) || obj.forcePermanentRedirects === true);
33
36
  }
34
37
  for (const value of Object.values(obj)) collectCsvRefs(value, out);
35
38
  }
@@ -68,12 +71,12 @@ export function buildCsvRedirectBlocks(
68
71
  const silent = options.silent ?? false;
69
72
  const publicDir = path.resolve(options.blocksDir, "../../public");
70
73
 
71
- const refs = new Set<string>();
74
+ const refs = new Map<string, boolean>();
72
75
  for (const value of Object.values(blocks)) collectCsvRefs(value, refs);
73
76
  if (refs.size === 0) return {};
74
77
 
75
78
  const csvBlocks: Record<string, unknown> = {};
76
- for (const from of refs) {
79
+ for (const [from, forcePermanentRedirects] of refs) {
77
80
  const csvPath = resolveCsvPath(from, publicDir);
78
81
  let csv: string;
79
82
  try {
@@ -87,11 +90,12 @@ export function buildCsvRedirectBlocks(
87
90
 
88
91
  // `loadRedirects` reads `entry.type` ("permanent" | "temporary"), while
89
92
  // `parseRedirectsCsv` returns a numeric `status` — map it back so 301s stay
90
- // 301s (a raw status field would be ignored and default to 302).
91
- const entries = parseRedirectsCsv(csv).map((r) => ({
93
+ // 301s (a raw status field would be ignored and default to 307).
94
+ const entries = parseRedirectsCsv(csv, { forcePermanentRedirects }).map((r) => ({
92
95
  from: r.from,
93
96
  to: r.to,
94
97
  type: r.status === 301 ? "permanent" : "temporary",
98
+ ...(r.discardQueryParameters ? { discardQueryParameters: true } : {}),
95
99
  }));
96
100
 
97
101
  const key = `__csv_redirects__${path.basename(csvPath)}`;
@@ -17,7 +17,7 @@ import {
17
17
  revisionKey,
18
18
  snapshotKey,
19
19
  } from "@decocms/blocks/cms";
20
- import type { ExactRedirect } from "@decocms/blocks/sdk/redirects";
20
+ import { type ExactRedirect, storedExactRedirectValue } from "@decocms/blocks/sdk/redirects";
21
21
  import type { KvRestClient } from "./cf-kv-rest";
22
22
 
23
23
  export interface Snapshot {
@@ -94,7 +94,7 @@ export async function syncRedirectsToKv(
94
94
  id: string,
95
95
  ): Promise<{ written: number; deleted: number }> {
96
96
  const desired = new Map(
97
- redirects.map((r) => [redirectKey(id, r.path), JSON.stringify({ to: r.to, status: r.status })]),
97
+ redirects.map((r) => [redirectKey(id, r.path), JSON.stringify(storedExactRedirectValue(r))]),
98
98
  );
99
99
 
100
100
  const existing = await client.list(redirectPrefix(id));