@decocms/nextjs 7.28.0-beta.1 → 7.28.0-beta.2

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/nextjs",
3
- "version": "7.28.0-beta.1",
3
+ "version": "7.28.0-beta.2",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=24"
@@ -29,8 +29,8 @@
29
29
  "lint:unused": "knip"
30
30
  },
31
31
  "dependencies": {
32
- "@decocms/blocks": "7.28.0-beta.1",
33
- "@decocms/blocks-admin": "7.28.0-beta.1"
32
+ "@decocms/blocks": "7.28.0-beta.2",
33
+ "@decocms/blocks-admin": "7.28.0-beta.2"
34
34
  },
35
35
  "peerDependencies": {
36
36
  "next": ">=15.0.0",
@@ -13,7 +13,10 @@ import { afterEach, describe, expect, it, vi } from "vitest";
13
13
 
14
14
  // The gate must stop us reaching cookies() at all when the feature is off.
15
15
  const cookiesMock = vi.fn(async () => ({ get: () => undefined }));
16
- vi.mock("next/headers", () => ({ cookies: () => cookiesMock() }));
16
+ vi.mock("next/headers", () => ({
17
+ cookies: () => cookiesMock(),
18
+ headers: async () => new Headers(),
19
+ }));
17
20
 
18
21
  // `connection()` is Next's runtime opt-out from static rendering; outside a
19
22
  // request scope it throws, so it is stubbed to a no-op here.
@@ -39,7 +42,7 @@ function seedPublishedHome() {
39
42
  afterEach(() => {
40
43
  cookiesMock.mockClear();
41
44
  connectionMock.mockClear();
42
- delete process.env.DECO_DRAFT_PREVIEW;
45
+ delete process.env.DECO_DRAFT_PREVIEW_HOST;
43
46
  delete process.env.DECO_SANDBOX_ORIGIN_SUFFIXES;
44
47
  });
45
48
 
@@ -72,8 +75,7 @@ describe("createDecoPage — draft preview gate", () => {
72
75
  });
73
76
 
74
77
  it("reads cookies() once the feature is fully configured", async () => {
75
- process.env.DECO_DRAFT_PREVIEW = "1";
76
- process.env.DECO_SANDBOX_ORIGIN_SUFFIXES = ".preview-studio.decocms.com";
78
+ process.env.DECO_DRAFT_PREVIEW_HOST = "preview.example";
77
79
  seedPublishedHome();
78
80
 
79
81
  const { default: Page } = createDecoPage({ siteName: "test-site" });
@@ -90,7 +90,8 @@ export function createDecoPage({ siteName }: CreateDecoPageOptions) {
90
90
  * first wins for both. So this must be awaited at the top of BOTH, not
91
91
  * just the page. `cache()` here makes the second call free.
92
92
  *
93
- * Gated on `isDraftPreviewEnabled()` — a plain env read, not a dynamic API —
93
+ * Gated on `isDraftPreviewEnabled()` (DECO_DRAFT_PREVIEW_HOST non-empty) — a
94
+ * plain env read, not a dynamic API —
94
95
  * so sites that never opt in behave exactly as before. That gate is doing
95
96
  * real work: `cookies()` and `searchParams` are both dynamic, and touching
96
97
  * them unconditionally would opt EVERY page out of static/ISR rendering.
package/src/draft.ts CHANGED
@@ -22,8 +22,12 @@
22
22
  * i.e. the page component. `ensureDraft()` exists so each site makes one call
23
23
  * instead of re-deriving that ordering rule.
24
24
  */
25
- import { resolveDraftDecofile, setDraftOverrideGetter } from "@decocms/blocks/cms";
26
- import { cookies } from "next/headers";
25
+ import {
26
+ isDraftHostAllowed,
27
+ resolveDraftDecofile,
28
+ setDraftOverrideGetter,
29
+ } from "@decocms/blocks/cms";
30
+ import { cookies, headers } from "next/headers";
27
31
  import { cache } from "react";
28
32
 
29
33
  /** Cookie that carries the pointer across in-preview navigation. */
@@ -116,6 +120,14 @@ export async function ensureDraft(searchParams?: DraftSearchParams): Promise<boo
116
120
  const pointer = selectDraftPointer(searchParams, cookieStore.get(DRAFT_COOKIE)?.value);
117
121
  if (!pointer) return false;
118
122
 
123
+ // Host gate, checked only once a pointer exists (headers() is a dynamic
124
+ // API): the same build may serve the preview domain and the production
125
+ // domain, and only hosts named in DECO_DRAFT_PREVIEW_HOST may render
126
+ // drafts — production stays published no matter what the URL carries.
127
+ const requestHeaders = await headers();
128
+ const host = requestHeaders.get("x-forwarded-host") ?? requestHeaders.get("host");
129
+ if (!isDraftHostAllowed(host)) return false;
130
+
119
131
  const blocks = await resolveDraftDecofile({ pointer });
120
132
  if (!blocks) return false;
121
133
 
@@ -1,9 +1,17 @@
1
1
  import { NextRequest, NextResponse } from "next/server";
2
- import { describe, expect, it } from "vitest";
2
+ import { afterEach, beforeEach, describe, expect, it } from "vitest";
3
3
 
4
4
  import { DRAFT_COOKIE } from "./draft";
5
5
  import { applyDraft, prepareDraft, rewriteToDraftRoute } from "./draftMiddleware";
6
6
 
7
+ beforeEach(() => {
8
+ // The middleware is host-gated; these tests run as the allowed host.
9
+ process.env.DECO_DRAFT_PREVIEW_HOST = "site.example";
10
+ });
11
+ afterEach(() => {
12
+ delete process.env.DECO_DRAFT_PREVIEW_HOST;
13
+ });
14
+
7
15
  function request(url: string, cookie?: string): NextRequest {
8
16
  const req = new NextRequest(new URL(url), {
9
17
  headers: cookie ? { cookie: `${DRAFT_COOKIE}=${cookie}` } : {},
@@ -20,6 +28,14 @@ describe("prepareDraft", () => {
20
28
  expect(prepareDraft(request("https://site.example/p", "abc@v1")).pointer).toBe("abc@v1");
21
29
  });
22
30
 
31
+ it("is inert on a host outside the allowlist — the production domain", () => {
32
+ // Same build, different Host: no cookie, no rewrite, nothing touched.
33
+ const req = request("https://fila.com.br/p?__draft=abc@v1");
34
+ process.env.DECO_DRAFT_PREVIEW_HOST = "fila.vtex.app";
35
+ expect(prepareDraft(req)).toEqual({ pointer: null, setCookie: null, clearCookie: false });
36
+ expect(rewriteToDraftRoute(req, prepareDraft(req))).toBeNull();
37
+ });
38
+
23
39
  it("ignores a client-supplied draft header — the page owns the decision", () => {
24
40
  // There is no request-header path any more; the page reads searchParams
25
41
  // and cookies itself, so a forged header is simply not an input.
@@ -19,8 +19,9 @@
19
19
  * Lives on its own subpath so middleware (edge runtime) never imports the root
20
20
  * barrel, which pulls in the client component graph.
21
21
  */
22
- import { type NextRequest, NextResponse } from "next/server";
23
22
 
23
+ import { isDraftHostAllowed } from "@decocms/blocks/cms";
24
+ import { type NextRequest, NextResponse } from "next/server";
24
25
  import {
25
26
  DRAFT_COOKIE,
26
27
  DRAFT_COOKIE_OPTIONS,
@@ -36,16 +37,28 @@ import {
36
37
  * nothing has to be forwarded through the request. That also means a draft
37
38
  * still works on routes this middleware never matches.
38
39
  */
40
+ const INERT: DraftMiddlewareDecision = { pointer: null, setCookie: null, clearCookie: false };
41
+
39
42
  export function prepareDraft(request: NextRequest): DraftMiddlewareDecision {
43
+ // Host gate first: on a host not named in DECO_DRAFT_PREVIEW_HOST the
44
+ // decision is inert — no cookie written, no rewrite, no headers touched —
45
+ // so the production domain behaves as if the feature didn't exist.
46
+ const host =
47
+ request.headers.get("x-forwarded-host") ??
48
+ request.headers.get("host") ??
49
+ request.nextUrl.host;
50
+ if (!isDraftHostAllowed(host)) return INERT;
40
51
  return decideDraft(new URL(request.url), request.cookies.get(DRAFT_COOKIE)?.value ?? null);
41
52
  }
42
53
 
43
54
  /**
44
- * Route prefix a drafted request is rewritten onto.
55
+ * URL prefix a drafted request is rewritten onto.
45
56
  *
46
- * Deliberately a `_`-prefixed segment: Next treats those as private folders and
47
- * excludes them from routing, so a site cannot accidentally expose it as a real
48
- * page — it is reachable only through the middleware rewrite below.
57
+ * The site must mount the matching route as `app/%5Fdraft/...` — URL-encoded.
58
+ * A literal `_draft/` directory is a Next "private folder", excluded from
59
+ * routing entirely, so the rewrite would fall through to whatever catch-all
60
+ * follows and 404. The encoded directory serves the same `/_draft` URL while
61
+ * staying routable.
49
62
  */
50
63
  export const DRAFT_ROUTE_PREFIX = "/_draft";
51
64