@rsc-kit/core 0.38.0 → 0.39.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.
@@ -69,8 +69,10 @@ export declare function sayUnroutedHost(host: string, segment: string): void;
69
69
  * server and in the browser, so an anchor hydrates as it was sent. Any other
70
70
  * root-relative path written on one of those subdomains is the site's, and
71
71
  * that one needs the current host, so it is only rewritten where `current`
72
- * is known. Everything else - `//`, a full url, `#`, a relative path - is
73
- * left as written.
72
+ * is known. On a host that is neither the site nor one of those subdomains -
73
+ * a preview, a platform's own name, localhost - nothing moves: it has no
74
+ * subdomain to go to, and serves the folder by path. Everything else - `//`,
75
+ * a full url, `#`, a relative path - is left as written.
74
76
  */
75
77
  export declare function subdomainHref(href: string, subdomains: readonly string[], site: string | null | undefined, current?: string | null): string;
76
78
  /**
@@ -133,20 +133,23 @@ function splitFirst(href) {
133
133
  * server and in the browser, so an anchor hydrates as it was sent. Any other
134
134
  * root-relative path written on one of those subdomains is the site's, and
135
135
  * that one needs the current host, so it is only rewritten where `current`
136
- * is known. Everything else - `//`, a full url, `#`, a relative path - is
137
- * left as written.
136
+ * is known. On a host that is neither the site nor one of those subdomains -
137
+ * a preview, a platform's own name, localhost - nothing moves: it has no
138
+ * subdomain to go to, and serves the folder by path. Everything else - `//`,
139
+ * a full url, `#`, a relative path - is left as written.
138
140
  */
139
141
  export function subdomainHref(href, subdomains, site, current) {
140
142
  if (!site || subdomains.length === 0 || !href.startsWith("/") || href.startsWith("//"))
141
143
  return href;
144
+ const bare = bareHost(current);
145
+ const siteBare = bareHost(site);
146
+ const onLabel = bare !== "" && subdomains.some((label) => bare === `${label}.${siteBare}`);
147
+ if (bare !== "" && !onLabel && bare.replace(/^www\./, "") !== siteBare)
148
+ return href;
142
149
  const split = splitFirst(href);
143
150
  if (split && subdomains.includes(split[0]))
144
151
  return `//${split[0]}.${site}${split[1]}`;
145
- const bare = bareHost(current);
146
- const siteBare = bareHost(site);
147
- if (bare && subdomains.some((label) => bare === `${label}.${siteBare}`))
148
- return `//${site}${href}`;
149
- return href;
152
+ return onLabel ? `//${site}${href}` : href;
150
153
  }
151
154
  /**
152
155
  * The subdomain url a request on the site's own host is sent to, when its
@@ -1 +1 @@
1
- {"version":3,"file":"hostRouting.js","sourceRoot":"","sources":["../src/hostRouting.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,4EAA4E;AAC5E,MAAM,UAAU,QAAQ,CAAC,IAA+B;IACtD,OAAO,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC;AAClD,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,WAAW,CACzB,IAA+B,EAC/B,GAAsB;IAEtB,MAAM,IAAI,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC;IAE5B,IAAI,CAAC,IAAI,IAAI,GAAG,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,IAAI,CAAC;IAC3C,IAAI,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC;QAAE,OAAO,IAAI,CAAC;IACpC,IAAI,IAAI,KAAK,WAAW,IAAI,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC;QACvE,OAAO,IAAI,CAAC;IAEd,KAAK,MAAM,IAAI,IAAI,GAAG,EAAE,CAAC;QACvB,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,GAAG,IAAI,CAAC,EAAE,CAAC;YAC9B,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC;YAE9C,OAAO,GAAG,KAAK,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC;QACpC,CAAC;IACH,CAAC;IAED,OAAO,I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,GAAG,UAAU,CAAC,QAAQ,CAAC,CAAC;IAEnC,IAAI,CAAC,KAAK,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QAAE,OAAO,IAAI,CAAC;IAE1D,OAAO,GAAG,KAAK,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;AACjE,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAC7B,IAA+B,EAC/B,GAAsB,EACtB,UAA6B;IAE7B,IAAI,CAAC,IAAI;QAAE,OAAO,IAAI,CAAC;IAEvB,MAAM,IAAI,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC;IAE5B,IAAI,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC;QAAE,OAAO,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC;IAE3D,KAAK,MAAM,KAAK,IAAI,UAAU,EAAE,CAAC;QAC/B,IAAI,IAAI,CAAC,UAAU,CAAC,KAAK,GAAG,GAAG,CAAC,IAAI,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;YAAE,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IACtH,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC","sourcesContent":["/**\n * A host as a route segment.\n *\n * A request's host is matched the way Next matches one after a middleware\n * rewrite: prepended to the path. The difference is that the build can see\n * it. `admin.example.com/users` matches `app/admin/users/page.tsx`, the\n * same file `example.com/admin/users` reaches by path; `acme.example.com/`\n * matches `app/[domain]/page.tsx` with `domain = \"acme\"`; a custom domain,\n * `acme.com/`, matches the same file with `domain = \"acme.com\"`.\n *\n * What is prepended:\n * - nothing, for the site's own hosts (metadataBase, `www.` of it, and any\n * `hosts` named in rscKit()) - the apex keeps path routing, so an app\n * adds tenants without moving a file;\n * - the subdomain, for a host under an own one: `admin.example.com` ->\n * `admin`, `a.b.example.com` -> `a.b`;\n * - the whole host otherwise: `acme.com` -> `acme.com`.\n *\n * Off entirely when the build knows no own host: every request is then the\n * site's own, and nothing here changes a path.\n *\n * Pure, and the same function in the browser: a link to `/settings` on\n * `acme.example.com` is matched as `/acme/settings` on both sides, so the\n * payload the client asks for is the one the server would render.\n */\n\n/** The host without a port, lower-cased; the form every comparison uses. */\nexport function bareHost(host: string | null | undefined): string {\n return (host ?? \"\").split(\":\")[0].toLowerCase();\n}\n\n/**\n * The segment a host contributes, or null for the site's own host.\n * `own` is the build's list, already bare and lower-case.\n */\nexport function hostSegment(\n host: string | null | undefined,\n own: readonly string[],\n): string | null {\n const bare = bareHost(host);\n\n if (!bare || own.length === 0) return null;\n if (own.includes(bare)) return null;\n if (bare === \"localhost\" || /^[\\d.]+$/.test(bare) || bare.startsWith(\"[\"))\n return null;\n\n for (const site of own) {\n if (bare.endsWith(\".\" + site)) {\n const sub = bare.slice(0, -(site.length + 1));\n\n return sub === \"www\" ? null : sub;\n }\n }\n\n return bare;\n}\n\n/** The pathname to match, with the host's segment in front when it has one. */\nexport function hostPath(\n host: string | null | undefined,\n pathname: string,\n own: readonly string[],\n): string {\n const segment = hostSegment(host, own);\n\n if (segment === null) return pathname;\n\n const rest = pathname === \"/\" ? \"\" : pathname;\n\n return \"/\" + segment + rest;\n}\n\n/**\n * The site's own hosts, from what the build was told. `www.` of each is\n * included, so `www.example.com` is never a tenant called `www`.\n */\nexport function ownHosts(\n metadataBase: string | URL | null | undefined,\n hosts: readonly string[] = [],\n): string[] {\n const out = new Set<string>();\n\n if (metadataBase) {\n try {\n out.add(bareHost(new URL(String(metadataBase)).host));\n } catch {\n // Not a url the build could read; the option is checked elsewhere.\n }\n }\n\n for (const host of hosts) out.add(bareHost(host));\n\n for (const host of [...out]) {\n if (!host.startsWith(\"www.\")) out.add(\"www.\" + host);\n }\n\n return [...out].filter(Boolean).sort();\n}\n\n/**\n * Whether a host's segment is one some route can begin with: a host segment\n * ([domain] at the top of app/), or a static directory of that name. Without\n * one, the host is treated as the site's own - so an app that declares a\n * metadataBase and has no tenant tree routes every host by path, and a\n * proxy that forwards to the app by an internal name is not turned into a\n * tenant called \"internal\".\n */\nexport function routableHost(\n segment: string,\n routes: readonly { segments: readonly { type: string; value: string }[] }[],\n): boolean {\n for (const route of routes) {\n const first = route.segments[0];\n\n if (!first) continue;\n if (first.type === \"host\") return true;\n if (first.type === \"static\" && first.value === segment) return true;\n }\n\n return false;\n}\n\n/**\n * A subdomain of the site that no route begins with is served by path, as\n * the site's own: a preview deployment under the production domain keeps\n * working. In development it is said, once per host - otherwise\n * merchant.example.test showed the home page and nothing said why.\n */\nexport function unroutedHostMessage(host: string, segment: string): string {\n return (\n `[rsc-kit] ${bareHost(host)} has no app/${segment}/ - served by path, as the site's own. ` +\n `Add app/${segment}/ to route the subdomain there.`\n );\n}\n\nconst saidUnrouted = new Set<string>();\n\n/** Says unroutedHostMessage once per host; what the dev server passes as onUnroutedHost. */\nexport function sayUnroutedHost(host: string, segment: string): void {\n const bare = bareHost(host);\n\n if (saidUnrouted.has(bare)) return;\n\n saidUnrouted.add(bare);\n console.warn(unroutedHostMessage(bare, segment));\n}\n\n/** `/merchant/orders?x=1` as `merchant` and `/orders?x=1`; null when the path has no first segment. */\nfunction splitFirst(href: string): [string, string] | null {\n const match = /^\\/([^/?#]+)(.*)$/.exec(href);\n\n if (!match) return null;\n\n const rest = match[2];\n\n return [match[1], rest === \"\" ? \"/\" : rest.startsWith(\"/\") ? rest : \"/\" + rest];\n}\n\n/**\n * Where a root-relative href goes once rscKit({ subdomains }) has named the\n * directories served on their own host.\n *\n * A path under one is that subdomain's, whatever host it is written on:\n * `/merchant/orders` is `//merchant.<site>/orders` - the same string on the\n * server and in the browser, so an anchor hydrates as it was sent. Any other\n * root-relative path written on one of those subdomains is the site's, and\n * that one needs the current host, so it is only rewritten where `current`\n * is known. Everything else - `//`, a full url, `#`, a relative path - is\n * left as written.\n */\nexport function subdomainHref(\n href: string,\n subdomains: readonly string[],\n site: string | null | undefined,\n current?: string | null,\n): string {\n if (!site || subdomains.length === 0 || !href.startsWith(\"/\") || href.startsWith(\"//\")) return href;\n\n const split = splitFirst(href);\n\n if (split && subdomains.includes(split[0])) return `//${split[0]}.${site}${split[1]}`;\n\n const bare = bareHost(current);\n const siteBare = bareHost(site);\n\n if (bare && subdomains.some((label) => bare === `${label}.${siteBare}`)) return `//${site}${href}`;\n\n return href;\n}\n\n/**\n * The subdomain url a request on the site's own host is sent to, when its\n * path is under a directory rscKit({ subdomains }) names: one url per page,\n * so a session, a cache entry and a share card all agree on it. Null for\n * every other request, and for a host that is not the site's - localhost,\n * an address, a proxy's internal name - which keeps the path form, since\n * there is no subdomain of it to go to.\n */\nexport function subdomainRedirect(\n host: string | null | undefined,\n pathname: string,\n own: readonly string[],\n subdomains: readonly string[],\n): string | null {\n if (subdomains.length === 0 || !host || !own.includes(bareHost(host))) return null;\n\n const split = splitFirst(pathname);\n\n if (!split || !subdomains.includes(split[0])) return null;\n\n return `${split[0]}.${host.replace(/^www\\./i, \"\")}${split[1]}`;\n}\n\n/**\n * The site's host as a request on `host` would write it - the request's own\n * host on the site, the part after the label on one of its subdomains, port\n * kept - or null for a host that is neither.\n */\nexport function siteFromRequest(\n host: string | null | undefined,\n own: readonly string[],\n subdomains: readonly string[],\n): string | null {\n if (!host) return null;\n\n const bare = bareHost(host);\n\n if (own.includes(bare)) return host.replace(/^www\\./i, \"\");\n\n for (const label of subdomains) {\n if (bare.startsWith(label + \".\") && own.includes(bare.slice(label.length + 1))) return host.slice(label.length + 1);\n }\n\n return null;\n}\n"]}
1
+ {"version":3,"file":"hostRouting.js","sourceRoot":"","sources":["../src/hostRouting.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,4EAA4E;AAC5E,M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,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QAAE,OAAO,IAAI,CAAC;IAE1D,OAAO,GAAG,KAAK,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;AACjE,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAC7B,IAA+B,EAC/B,GAAsB,EACtB,UAA6B;IAE7B,IAAI,CAAC,IAAI;QAAE,OAAO,IAAI,CAAC;IAEvB,MAAM,IAAI,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC;IAE5B,IAAI,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC;QAAE,OAAO,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC;IAE3D,KAAK,MAAM,KAAK,IAAI,UAAU,EAAE,CAAC;QAC/B,IAAI,IAAI,CAAC,UAAU,CAAC,KAAK,GAAG,GAAG,CAAC,IAAI,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;YAAE,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IACtH,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC","sourcesContent":["/**\n * A host as a route segment.\n *\n * A request's host is matched the way Next matches one after a middleware\n * rewrite: prepended to the path. The difference is that the build can see\n * it. `admin.example.com/users` matches `app/admin/users/page.tsx`, the\n * same file `example.com/admin/users` reaches by path; `acme.example.com/`\n * matches `app/[domain]/page.tsx` with `domain = \"acme\"`; a custom domain,\n * `acme.com/`, matches the same file with `domain = \"acme.com\"`.\n *\n * What is prepended:\n * - nothing, for the site's own hosts (metadataBase, `www.` of it, and any\n * `hosts` named in rscKit()) - the apex keeps path routing, so an app\n * adds tenants without moving a file;\n * - the subdomain, for a host under an own one: `admin.example.com` ->\n * `admin`, `a.b.example.com` -> `a.b`;\n * - the whole host otherwise: `acme.com` -> `acme.com`.\n *\n * Off entirely when the build knows no own host: every request is then the\n * site's own, and nothing here changes a path.\n *\n * Pure, and the same function in the browser: a link to `/settings` on\n * `acme.example.com` is matched as `/acme/settings` on both sides, so the\n * payload the client asks for is the one the server would render.\n */\n\n/** The host without a port, lower-cased; the form every comparison uses. */\nexport function bareHost(host: string | null | undefined): string {\n return (host ?? \"\").split(\":\")[0].toLowerCase();\n}\n\n/**\n * The segment a host contributes, or null for the site's own host.\n * `own` is the build's list, already bare and lower-case.\n */\nexport function hostSegment(\n host: string | null | undefined,\n own: readonly string[],\n): string | null {\n const bare = bareHost(host);\n\n if (!bare || own.length === 0) return null;\n if (own.includes(bare)) return null;\n if (bare === \"localhost\" || /^[\\d.]+$/.test(bare) || bare.startsWith(\"[\"))\n return null;\n\n for (const site of own) {\n if (bare.endsWith(\".\" + site)) {\n const sub = bare.slice(0, -(site.length + 1));\n\n return sub === \"www\" ? null : sub;\n }\n }\n\n return bare;\n}\n\n/** The pathname to match, with the host's segment in front when it has one. */\nexport function hostPath(\n host: string | null | undefined,\n pathname: string,\n own: readonly string[],\n): string {\n const segment = hostSegment(host, own);\n\n if (segment === null) return pathname;\n\n const rest = pathname === \"/\" ? \"\" : pathname;\n\n return \"/\" + segment + rest;\n}\n\n/**\n * The site's own hosts, from what the build was told. `www.` of each is\n * included, so `www.example.com` is never a tenant called `www`.\n */\nexport function ownHosts(\n metadataBase: string | URL | null | undefined,\n hosts: readonly string[] = [],\n): string[] {\n const out = new Set<string>();\n\n if (metadataBase) {\n try {\n out.add(bareHost(new URL(String(metadataBase)).host));\n } catch {\n // Not a url the build could read; the option is checked elsewhere.\n }\n }\n\n for (const host of hosts) out.add(bareHost(host));\n\n for (const host of [...out]) {\n if (!host.startsWith(\"www.\")) out.add(\"www.\" + host);\n }\n\n return [...out].filter(Boolean).sort();\n}\n\n/**\n * Whether a host's segment is one some route can begin with: a host segment\n * ([domain] at the top of app/), or a static directory of that name. Without\n * one, the host is treated as the site's own - so an app that declares a\n * metadataBase and has no tenant tree routes every host by path, and a\n * proxy that forwards to the app by an internal name is not turned into a\n * tenant called \"internal\".\n */\nexport function routableHost(\n segment: string,\n routes: readonly { segments: readonly { type: string; value: string }[] }[],\n): boolean {\n for (const route of routes) {\n const first = route.segments[0];\n\n if (!first) continue;\n if (first.type === \"host\") return true;\n if (first.type === \"static\" && first.value === segment) return true;\n }\n\n return false;\n}\n\n/**\n * A subdomain of the site that no route begins with is served by path, as\n * the site's own: a preview deployment under the production domain keeps\n * working. In development it is said, once per host - otherwise\n * merchant.example.test showed the home page and nothing said why.\n */\nexport function unroutedHostMessage(host: string, segment: string): string {\n return (\n `[rsc-kit] ${bareHost(host)} has no app/${segment}/ - served by path, as the site's own. ` +\n `Add app/${segment}/ to route the subdomain there.`\n );\n}\n\nconst saidUnrouted = new Set<string>();\n\n/** Says unroutedHostMessage once per host; what the dev server passes as onUnroutedHost. */\nexport function sayUnroutedHost(host: string, segment: string): void {\n const bare = bareHost(host);\n\n if (saidUnrouted.has(bare)) return;\n\n saidUnrouted.add(bare);\n console.warn(unroutedHostMessage(bare, segment));\n}\n\n/** `/merchant/orders?x=1` as `merchant` and `/orders?x=1`; null when the path has no first segment. */\nfunction splitFirst(href: string): [string, string] | null {\n const match = /^\\/([^/?#]+)(.*)$/.exec(href);\n\n if (!match) return null;\n\n const rest = match[2];\n\n return [match[1], rest === \"\" ? \"/\" : rest.startsWith(\"/\") ? rest : \"/\" + rest];\n}\n\n/**\n * Where a root-relative href goes once rscKit({ subdomains }) has named the\n * directories served on their own host.\n *\n * A path under one is that subdomain's, whatever host it is written on:\n * `/merchant/orders` is `//merchant.<site>/orders` - the same string on the\n * server and in the browser, so an anchor hydrates as it was sent. Any other\n * root-relative path written on one of those subdomains is the site's, and\n * that one needs the current host, so it is only rewritten where `current`\n * is known. On a host that is neither the site nor one of those subdomains -\n * a preview, a platform's own name, localhost - nothing moves: it has no\n * subdomain to go to, and serves the folder by path. Everything else - `//`,\n * a full url, `#`, a relative path - is left as written.\n */\nexport function subdomainHref(\n href: string,\n subdomains: readonly string[],\n site: string | null | undefined,\n current?: string | null,\n): string {\n if (!site || subdomains.length === 0 || !href.startsWith(\"/\") || href.startsWith(\"//\")) return href;\n\n const bare = bareHost(current);\n const siteBare = bareHost(site);\n const onLabel = bare !== \"\" && subdomains.some((label) => bare === `${label}.${siteBare}`);\n\n if (bare !== \"\" && !onLabel && bare.replace(/^www\\./, \"\") !== siteBare) return href;\n\n const split = splitFirst(href);\n\n if (split && subdomains.includes(split[0])) return `//${split[0]}.${site}${split[1]}`;\n\n return onLabel ? `//${site}${href}` : href;\n}\n\n/**\n * The subdomain url a request on the site's own host is sent to, when its\n * path is under a directory rscKit({ subdomains }) names: one url per page,\n * so a session, a cache entry and a share card all agree on it. Null for\n * every other request, and for a host that is not the site's - localhost,\n * an address, a proxy's internal name - which keeps the path form, since\n * there is no subdomain of it to go to.\n */\nexport function subdomainRedirect(\n host: string | null | undefined,\n pathname: string,\n own: readonly string[],\n subdomains: readonly string[],\n): string | null {\n if (subdomains.length === 0 || !host || !own.includes(bareHost(host))) return null;\n\n const split = splitFirst(pathname);\n\n if (!split || !subdomains.includes(split[0])) return null;\n\n return `${split[0]}.${host.replace(/^www\\./i, \"\")}${split[1]}`;\n}\n\n/**\n * The site's host as a request on `host` would write it - the request's own\n * host on the site, the part after the label on one of its subdomains, port\n * kept - or null for a host that is neither.\n */\nexport function siteFromRequest(\n host: string | null | undefined,\n own: readonly string[],\n subdomains: readonly string[],\n): string | null {\n if (!host) return null;\n\n const bare = bareHost(host);\n\n if (own.includes(bare)) return host.replace(/^www\\./i, \"\");\n\n for (const label of subdomains) {\n if (bare.startsWith(label + \".\") && own.includes(bare.slice(label.length + 1))) return host.slice(label.length + 1);\n }\n\n return null;\n}\n"]}
@@ -569,10 +569,15 @@ export function placed(url, host = location.hostname) {
569
569
  const labels = typeof __RSC_SUBDOMAINS__ == "object" && __RSC_SUBDOMAINS__;
570
570
  if (!site || !labels || url[0] != "/" || url[1] == "/")
571
571
  return url;
572
+ const bare = site.split(":")[0];
573
+ const onLabel = labels.some((label) => host == label + "." + bare);
574
+ // A preview, a platform's name, localhost: no subdomain to go to.
575
+ if (!onLabel && host.replace(/^www\./, "") != bare)
576
+ return url;
572
577
  const [, first, rest] = /^\/([^/?#]+)(.*)/.exec(url) ?? [];
573
578
  if (labels.includes(first))
574
579
  return `//${first}.${site}${rest[0] == "/" ? rest : "/" + rest}`;
575
- return labels.some((label) => host == label + "." + site.split(":")[0]) ? "//" + site + url : url;
580
+ return onLabel ? "//" + site + url : url;
576
581
  }
577
582
  function isExternalUrl(url) {
578
583
  try {