From 6ad154e84d117035616adc1148e88009ca9f7e04 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 2 Oct 2026 08:23:31 +0000 Subject: [PATCH] chore: sync storefront from monorepo Source: coded-letter/coded-letter-monorepo@74afb625d10563a97388090116efa738a4cc717d --- .../src/lib/protectedPageEligibility.ts | 52 +++++++++++++++++++ .../storefront/src/lib/protectedPages.test.ts | 22 ++++++++ apps/storefront/src/lib/protectedPages.ts | 26 +++++++++- 3 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 apps/storefront/src/lib/protectedPageEligibility.ts diff --git a/apps/storefront/src/lib/protectedPageEligibility.ts b/apps/storefront/src/lib/protectedPageEligibility.ts new file mode 100644 index 0000000..b6ef45a --- /dev/null +++ b/apps/storefront/src/lib/protectedPageEligibility.ts @@ -0,0 +1,52 @@ +const NON_PAGE_ROUTE_ROOTS = new Set([ + "product", + "product-category", + "kategoria-produktu", + "pro-cat", + "pro-category", + "product-tag", + "pro-tag", + "brand", + "product-brand", + "category", + "tag", + "author", + "cart", + "checkout", + "wishlist", + "reading-list", + "account", + "auth", + "login", + "register", + "forgot-password", + "order-success", + "unsubscribe", + "community", +]); + +export function shouldProbeProtectedPage(uri: string): boolean { + let segments: string[]; + try { + segments = new URL(uri, "https://storefront.invalid").pathname + .split("/") + .filter(Boolean) + .map((segment) => decodeURIComponent(segment).toLowerCase()); + } catch { + return false; + } + + if (/^[a-z]{2}(?:-[a-z0-9]+)?$/.test(segments[0] || "")) { + segments = segments.slice(1); + } + + const [root, child] = segments; + if (!root || NON_PAGE_ROUTE_ROOTS.has(root)) return false; + if (root === "shop") return segments.length === 1; + if (root === "sklep") return segments.length === 1; + if (root === "blog") { + return segments.length === 1 && !["category", "tag", "author"].includes(child); + } + + return true; +} diff --git a/apps/storefront/src/lib/protectedPages.test.ts b/apps/storefront/src/lib/protectedPages.test.ts index 3001875..e39ce3d 100644 --- a/apps/storefront/src/lib/protectedPages.test.ts +++ b/apps/storefront/src/lib/protectedPages.test.ts @@ -1,6 +1,7 @@ import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import test from "node:test"; +import { shouldProbeProtectedPage } from "./protectedPageEligibility.ts"; const source = (relative: string) => readFileSync(new URL(relative, import.meta.url), "utf8"); @@ -14,6 +15,9 @@ test("protected pages use authenticated non-cacheable REST access and strict log assert.match(client, /X-FunkyCommerce-Page-Proof/); assert.match(client, /cachePrivate:\s*true/); assert.match(client, /isRestRouteAvailable\(PROTECTED_PAGE_ROUTE/); + assert.match(client, /!shouldProbeProtectedPage\(normalizedUri\) \|\| unprotectedPageUris\.has\(normalizedUri\)/); + assert.match(client, /protectedPageRequests\.get\(normalizedUri\)/); + assert.match(client, /unprotectedPageUris\.add\(uri\)/); assert.match(gate, /parseStorefrontAuthRef\(`\$\{pathname\}\$\{search\}\$\{hash\}`\)/); assert.match(gate, /role="alert"/); assert.match(gate, /aria-labelledby="protected-page-title"/); @@ -21,6 +25,24 @@ test("protected pages use authenticated non-cacheable REST access and strict log assert.match(client, /response\.status === 429/); }); +test("protected-page fallback skips known storefront and taxonomy routes", () => { + for (const uri of [ + "/kategoria-produktu/oliwa-i-przyprawy/", + "/pl/kategoria-produktu/oliwa-i-przyprawy/", + "/product-category/olive-oil/", + "/shop/category/olive-oil/", + "/product-tag/sale/", + "/blog/category/recipes/", + "/author/store-owner/", + "/product/sample-product/", + "/checkout/", + ]) { + assert.equal(shouldProbeProtectedPage(uri), false, uri); + } + + assert.equal(shouldProbeProtectedPage("/company/shipping-policy/"), true); +}); + test("CMS behaviors follow the global code-controls preference after protected-page updates", () => { const content = source("../components/CmsPageContent.tsx"); diff --git a/apps/storefront/src/lib/protectedPages.ts b/apps/storefront/src/lib/protectedPages.ts index 8bed598..0c2e5c5 100644 --- a/apps/storefront/src/lib/protectedPages.ts +++ b/apps/storefront/src/lib/protectedPages.ts @@ -1,10 +1,13 @@ import { BACKEND_ORIGIN } from "@funky/sdk"; import { authStore } from "./auth.ts"; import type { CmsPage } from "./pages.ts"; +import { shouldProbeProtectedPage } from "./protectedPageEligibility.ts"; import { isRestRouteAvailable } from "./restRouteAvailability.ts"; const PROOF_PREFIX = "funkycommerce-page-proof:"; const PROTECTED_PAGE_ROUTE = "/funkycommerce/v1/protected-page"; +const unprotectedPageUris = new Set(); +const protectedPageRequests = new Map>(); export class ProtectedPageError extends Error { readonly kind: "auth-required" | "password-required" | "invalid-password"; @@ -55,9 +58,28 @@ async function protectedPageRequest(uri: string, password?: string): Promise { +export function getProtectedPageByUri(uri: string): Promise { + const normalizedUri = new URL(uri, "https://storefront.invalid").pathname; + if (!shouldProbeProtectedPage(normalizedUri) || unprotectedPageUris.has(normalizedUri)) { + return Promise.resolve(null); + } + const pendingRequest = protectedPageRequests.get(normalizedUri); + if (pendingRequest) return pendingRequest; + + const request = loadProtectedPage(normalizedUri).finally(() => { + protectedPageRequests.delete(normalizedUri); + }); + protectedPageRequests.set(normalizedUri, request); + return request; +} + +async function loadProtectedPage(uri: string): Promise { const response = await protectedPageRequest(uri); - if (!response || response.status === 404) return null; + if (!response) return null; + if (response.status === 404) { + unprotectedPageUris.add(uri); + return null; + } if (response.status === 401) throw new ProtectedPageError("auth-required", "Sign in with permission to read this private page."); if (response.status === 403) throw new ProtectedPageError("password-required", "This page requires a password."); if (!response.ok) throw new Error(`Protected page request failed with status ${response.status}`);