@rsc-kit/core 0.36.0 → 0.38.0

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/dist/host.js CHANGED
@@ -16,7 +16,7 @@
16
16
  import { allowFor, matchApiRoute, matchIntercept, layoutChain, matchRoute, retentionKey, sharedDepth, } from "./routing.js";
17
17
  import { PARAM_PLACEHOLDER, pathKey, patternKey } from "./prerender.js";
18
18
  import { apiKey } from "./apiPrerender.js";
19
- import { bareHost, hostPath, hostSegment, routableHost } from "./hostRouting.js";
19
+ import { bareHost, hostPath, hostSegment, routableHost, siteFromRequest, subdomainHref, subdomainRedirect } from "./hostRouting.js";
20
20
  import { withRevalidation } from "./revalidate.js";
21
21
  import { withoutPayloadKey } from "./js/payloadKey.js";
22
22
  import { cachingFor, createPageCache, finishCacheable, keptBodyOf, noteReadsSoFar, pageMemoryStore } from "./pageCache.js";
@@ -595,6 +595,8 @@ export function createRscHandler(options) {
595
595
  const payloadName = manifest.build?.payloadName || "";
596
596
  /** The site's own hosts; any other host is routed with its segment in front of the path. */
597
597
  const siteHosts = manifest.build?.hosts ?? [];
598
+ /** rscKit({ subdomains }): the directories served on their own host, and only there. */
599
+ const subdomains = manifest.build?.subdomains ?? [];
598
600
  /** Whether responses say what built them. How they were served is always said. */
599
601
  const identify = Boolean(manifest.build?.identify);
600
602
  // rscKit({ headers }): every response, and every page an edge serves.
@@ -930,8 +932,49 @@ export function createRscHandler(options) {
930
932
  })));
931
933
  }
932
934
  ;
935
+ /**
936
+ * route(), with what rscKit({ subdomains }) asks of every answer: a redirect
937
+ * to a path under one of those directories, or to the site from one of
938
+ * them, names the host it belongs on. redirect('/merchant/onboarding')
939
+ * thrown on merchant.example.com would otherwise send the browser to
940
+ * merchant.example.com/merchant/onboarding - the subdomain's
941
+ * /merchant/merchant/onboarding. A payload's redirect header is left to
942
+ * the client, which places it the same way.
943
+ */
933
944
  async function route(request) {
945
+ const response = await routeOnHost(request);
946
+ const location = response?.headers.get("location");
947
+ if (!response || subdomains.length === 0 || !location?.startsWith("/") || location.startsWith("//"))
948
+ return response;
949
+ const host = request.headers.get("x-forwarded-host") ?? request.headers.get("host") ?? new URL(request.url).host;
950
+ const placed = subdomainHref(location, subdomains, siteFromRequest(host, siteHosts, subdomains), host);
951
+ if (placed === location)
952
+ return response;
953
+ const headers = new Headers(response.headers);
954
+ headers.set("location", placed);
955
+ return new Response(response.body, { status: response.status, statusText: response.statusText, headers });
956
+ }
957
+ async function routeOnHost(request) {
934
958
  let url = new URL(request.url);
959
+ // rscKit({ subdomains }): a page under one of those directories has one
960
+ // url, on its subdomain, so the path form on the site's own host is sent
961
+ // there - a document load only; the client never asks for a payload by
962
+ // that path, since it places the link first. Behind any backend, since
963
+ // it is this process that answers the page.
964
+ if (subdomains.length > 0 &&
965
+ (request.method === "GET" || request.method === "HEAD") &&
966
+ !request.headers.has(HEADER.rsc) &&
967
+ pageForPayload(url.pathname) === null) {
968
+ const host = request.headers.get("x-forwarded-host") ?? request.headers.get("host") ?? url.host;
969
+ const to = subdomainRedirect(host, url.pathname, siteHosts, subdomains);
970
+ if (to !== null) {
971
+ const proto = request.headers.get("x-forwarded-proto")?.split(",")[0].trim() || url.protocol.replace(/:$/, "");
972
+ return new Response(null, {
973
+ status: 301,
974
+ headers: { Location: `${proto}://${to}${url.search}`, "Cache-Control": "no-store" },
975
+ });
976
+ }
977
+ }
935
978
  // A host other than the site's own is matched with its segment in front
936
979
  // of the path, once, here: everything below - the api match, the page
937
980
  // match, the stored answer, the payload - sees that path and nothing
@@ -1607,7 +1650,21 @@ export function createRscHandler(options) {
1607
1650
  nonce: edgeNonce ? EDGE.noncePlaceholder : null,
1608
1651
  }),
1609
1652
  }
1610
- : pageHeaders;
1653
+ : { ...pageHeaders };
1654
+ // A static page's Cache-Control, as its document goes out from here:
1655
+ // the host's, which wins over the app's on the document too. Without it the
1656
+ // edge's copy went out with none, and a browser or a cache behind the
1657
+ // edge kept it by heuristic. Always REVALIDATE: a guarded page, the one
1658
+ // whose document is PER_CLIENT, never goes whole (pageAt answers 404). A
1659
+ // shell's document is the resumed one, rendered per request, so a shell
1660
+ // names none.
1661
+ if (complete) {
1662
+ for (const name of Object.keys(headers)) {
1663
+ if (name.toLowerCase() === "cache-control")
1664
+ delete headers[name];
1665
+ }
1666
+ headers["Cache-Control"] = REVALIDATE;
1667
+ }
1611
1668
  return new Response(request.method === "HEAD"
1612
1669
  ? null
1613
1670
  : JSON.stringify({