@krate/runtime 0.5.0-rc.1 → 0.5.0-rc.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/dist/actions.js +2 -5
- package/dist/base-path.d.ts +7 -0
- package/dist/base-path.js +19 -0
- package/dist/base-path.test.d.ts +1 -0
- package/dist/base-path.test.js +88 -0
- package/dist/dom.js +15 -2
- package/dist/index.d.ts +5 -0
- package/dist/krate-core.js +1 -1
- package/dist/krate-f-actions.js +1 -1
- package/dist/krate-f-router-lite.js +1 -1
- package/dist/krate-f-router.js +1 -1
- package/dist/krate-hydrate-lite.js +5 -5
- package/dist/krate-hydrate.js +5 -5
- package/dist/krate-runtime.js +5 -5
- package/dist/metadata.js +3 -0
- package/dist/router-lite.js +3 -9
- package/dist/router.js +12 -17
- package/dist/server-handler.d.ts +1 -0
- package/dist/server-handler.js +39 -5
- package/dist/server-handler.test.js +46 -0
- package/dist/server.js +2 -1
- package/package.json +1 -1
package/dist/metadata.js
CHANGED
|
@@ -170,6 +170,9 @@ function resolveBaseJoin(value, base) {
|
|
|
170
170
|
// metadataBase's path is a prefix: "/docs" + "/a" = "/docs/a".
|
|
171
171
|
const prefix = b.pathname.replace(/\/$/, '');
|
|
172
172
|
const path = s.startsWith('/') ? s : '/' + s;
|
|
173
|
+
// A convention image URL already carries the site base path.
|
|
174
|
+
if (prefix && (path === prefix || path.startsWith(prefix + '/')))
|
|
175
|
+
return b.origin + path;
|
|
173
176
|
return b.origin + prefix + path;
|
|
174
177
|
}
|
|
175
178
|
catch {
|
package/dist/router-lite.js
CHANGED
|
@@ -1,13 +1,7 @@
|
|
|
1
1
|
// Router API for sites with the SPA router disabled (`spa: false`). Every
|
|
2
2
|
// navigation is a full document load; the API surface matches router.ts so
|
|
3
3
|
// application code (and the Next.js migration shim) works unchanged.
|
|
4
|
-
|
|
5
|
-
const cfg = globalThis.__KRATE_CFG__;
|
|
6
|
-
const bp = (cfg && cfg.basePath) || '';
|
|
7
|
-
if (!bp || bp === '/')
|
|
8
|
-
return '';
|
|
9
|
-
return '/' + bp.replace(/^\/+|\/+$/g, '');
|
|
10
|
-
}
|
|
4
|
+
import { basePath, withBase } from './base-path.js';
|
|
11
5
|
export function getNavigationState() {
|
|
12
6
|
if (typeof location === 'undefined')
|
|
13
7
|
return { url: '', pathname: '/', search: '', hash: '', status: 'idle' };
|
|
@@ -39,9 +33,9 @@ export function navigate(url, opts = {}) {
|
|
|
39
33
|
if (typeof location === 'undefined')
|
|
40
34
|
return true;
|
|
41
35
|
if (opts.replace)
|
|
42
|
-
location.replace(url);
|
|
36
|
+
location.replace(withBase(url));
|
|
43
37
|
else
|
|
44
|
-
location.assign(url);
|
|
38
|
+
location.assign(withBase(url));
|
|
45
39
|
return true;
|
|
46
40
|
}
|
|
47
41
|
export const krateRouter = {
|
package/dist/router.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
// place. All router state lives at module scope so `reinitRouter()` (called
|
|
6
6
|
// by every page's hydration script) only refreshes per-page bindings and never
|
|
7
7
|
// loses the prefetch cache, saved scroll positions or the in-flight navigation.
|
|
8
|
+
import { basePath, withBase } from './base-path.js';
|
|
8
9
|
/**
|
|
9
10
|
* The shared runtime chunk is loaded once and must persist across SPA
|
|
10
11
|
* navigations. Page-specific hydration scripts, in contrast, must be
|
|
@@ -15,13 +16,6 @@ const RUNTIME_CHUNK_RE = /\/chunks\/runtime\.[^/]+\.js$/;
|
|
|
15
16
|
function runtimeConfig() {
|
|
16
17
|
return globalThis.__KRATE_CFG__ || {};
|
|
17
18
|
}
|
|
18
|
-
/** Normalizes the configured base path to "" or "/prefix" (no trailing slash). */
|
|
19
|
-
function basePath() {
|
|
20
|
-
const bp = runtimeConfig().basePath || '';
|
|
21
|
-
if (!bp || bp === '/')
|
|
22
|
-
return '';
|
|
23
|
-
return '/' + bp.replace(/^\/+|\/+$/g, '');
|
|
24
|
-
}
|
|
25
19
|
/** Strips the base path from a pathname so route comparisons are consistent. */
|
|
26
20
|
function stripBase(pathname) {
|
|
27
21
|
const bp = basePath();
|
|
@@ -445,14 +439,15 @@ function setupSpeculationRules() {
|
|
|
445
439
|
try {
|
|
446
440
|
const script = document.createElement('script');
|
|
447
441
|
script.type = 'speculationrules';
|
|
442
|
+
const bp = basePath();
|
|
448
443
|
script.textContent = JSON.stringify({
|
|
449
444
|
prefetch: [
|
|
450
445
|
{
|
|
451
446
|
where: {
|
|
452
447
|
and: [
|
|
453
|
-
{ href_matches: '/*' },
|
|
454
|
-
{ not: { href_matches: '/__krate/*' } },
|
|
455
|
-
{ not: { href_matches: '/api/*' } },
|
|
448
|
+
{ href_matches: bp + '/*' },
|
|
449
|
+
{ not: { href_matches: bp + '/__krate/*' } },
|
|
450
|
+
{ not: { href_matches: bp + '/api/*' } },
|
|
456
451
|
],
|
|
457
452
|
},
|
|
458
453
|
eagerness: 'moderate',
|
|
@@ -1712,18 +1707,18 @@ function refresh() {
|
|
|
1712
1707
|
function push(url, opts = {}) {
|
|
1713
1708
|
if (!initialized) {
|
|
1714
1709
|
if (typeof location !== 'undefined')
|
|
1715
|
-
location.assign(url);
|
|
1710
|
+
location.assign(withBase(url));
|
|
1716
1711
|
return Promise.resolve();
|
|
1717
1712
|
}
|
|
1718
|
-
return start({ url, kind: 'push', scroll: opts.scroll !== false, state: opts.state });
|
|
1713
|
+
return start({ url: withBase(url), kind: 'push', scroll: opts.scroll !== false, state: opts.state });
|
|
1719
1714
|
}
|
|
1720
1715
|
function replace(url, opts = {}) {
|
|
1721
1716
|
if (!initialized) {
|
|
1722
1717
|
if (typeof location !== 'undefined')
|
|
1723
|
-
location.replace(url);
|
|
1718
|
+
location.replace(withBase(url));
|
|
1724
1719
|
return Promise.resolve();
|
|
1725
1720
|
}
|
|
1726
|
-
return start({ url, kind: 'replace', scroll: opts.scroll !== false, state: opts.state });
|
|
1721
|
+
return start({ url: withBase(url), kind: 'replace', scroll: opts.scroll !== false, state: opts.state });
|
|
1727
1722
|
}
|
|
1728
1723
|
/**
|
|
1729
1724
|
* The router API. Every navigation method resolves once the new route's DOM
|
|
@@ -1752,7 +1747,7 @@ export const krateRouter = {
|
|
|
1752
1747
|
return Promise.resolve();
|
|
1753
1748
|
let u;
|
|
1754
1749
|
try {
|
|
1755
|
-
u = new URL(url, location.href);
|
|
1750
|
+
u = new URL(withBase(url), location.href);
|
|
1756
1751
|
}
|
|
1757
1752
|
catch {
|
|
1758
1753
|
return Promise.resolve();
|
|
@@ -1777,9 +1772,9 @@ export function navigate(url, opts = {}) {
|
|
|
1777
1772
|
}
|
|
1778
1773
|
if (typeof location !== 'undefined') {
|
|
1779
1774
|
if (opts.replace)
|
|
1780
|
-
location.replace(url);
|
|
1775
|
+
location.replace(withBase(url));
|
|
1781
1776
|
else
|
|
1782
|
-
location.assign(url);
|
|
1777
|
+
location.assign(withBase(url));
|
|
1783
1778
|
}
|
|
1784
1779
|
return true;
|
|
1785
1780
|
}
|
package/dist/server-handler.d.ts
CHANGED
package/dist/server-handler.js
CHANGED
|
@@ -186,6 +186,9 @@ export function createKrateHandler(options) {
|
|
|
186
186
|
const trustProxy = options.trustProxy !== undefined ? options.trustProxy : cfg.trustProxy === undefined ? null : cfg.trustProxy;
|
|
187
187
|
const platform = PLATFORMS[target];
|
|
188
188
|
const basePath = cfg.basePath || "";
|
|
189
|
+
// Server-rendered <Link>s read the base path from here, as in the browser.
|
|
190
|
+
if (basePath)
|
|
191
|
+
globalThis.__KRATE_CFG__ = { basePath };
|
|
189
192
|
const hashed = new Set(data.hashedAssets);
|
|
190
193
|
const staticOnly = new Set(data.staticOnlyRoutes.map(normalizeRoutePattern));
|
|
191
194
|
const sha256Hex = options.sha256Hex || portableSha256Hex;
|
|
@@ -881,14 +884,33 @@ export function createKrateHandler(options) {
|
|
|
881
884
|
return new Response(body, { status: 200, headers: rs.res });
|
|
882
885
|
}
|
|
883
886
|
// Redirects and rewrites
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
+
// Under a base path, basePath: false rules match the full path before the
|
|
888
|
+
// base is stripped (see handle) and stay out of this list.
|
|
889
|
+
const insideRedirects = basePath ? cfg.redirects.filter((rd) => rd.basePath !== false) : cfg.redirects;
|
|
890
|
+
const outsideRedirects = basePath ? cfg.redirects.filter((rd) => rd.basePath === false) : [];
|
|
891
|
+
function configRedirect(rs, p, rules) {
|
|
892
|
+
for (const rd of rules) {
|
|
887
893
|
if (matchRedirect(p, rd.source)) {
|
|
888
894
|
const dest = rewriteDestination(p, rd.source, rd.destination);
|
|
889
895
|
return goRedirect(dest, p, rs.method, rd.permanent ? 301 : 302, rs.res);
|
|
890
896
|
}
|
|
891
897
|
}
|
|
898
|
+
return null;
|
|
899
|
+
}
|
|
900
|
+
/** Prefixes a root-relative Location from inside the base path. */
|
|
901
|
+
function withBaseLocation(res) {
|
|
902
|
+
const loc = res.headers.get("location");
|
|
903
|
+
if (!loc || !loc.startsWith("/") || loc.startsWith("//"))
|
|
904
|
+
return res;
|
|
905
|
+
const headers = new Headers(res.headers);
|
|
906
|
+
headers.set("location", basePath + loc);
|
|
907
|
+
return new Response(res.body, { status: res.status, statusText: res.statusText, headers });
|
|
908
|
+
}
|
|
909
|
+
async function redirectRewriteHandler(rs) {
|
|
910
|
+
const p = rs.path;
|
|
911
|
+
const redirected = configRedirect(rs, p, insideRedirects);
|
|
912
|
+
if (redirected)
|
|
913
|
+
return redirected;
|
|
892
914
|
for (const rw of cfg.rewrites) {
|
|
893
915
|
if (matchRedirect(p, rw.source)) {
|
|
894
916
|
const rewritten = rewriteDestination(p, rw.source, rw.destination);
|
|
@@ -1797,9 +1819,17 @@ export function createKrateHandler(options) {
|
|
|
1797
1819
|
};
|
|
1798
1820
|
try {
|
|
1799
1821
|
// Mount under the base path: requests outside it redirect (GET/HEAD) or 404.
|
|
1822
|
+
// The app then sees base-less paths, so a root-relative Location it
|
|
1823
|
+
// answers with gets the base on the way out.
|
|
1824
|
+
let mounted = false;
|
|
1800
1825
|
if (basePath) {
|
|
1801
1826
|
const p = rs.path;
|
|
1802
1827
|
if (!(p.startsWith("/__krate/") || p === "/healthz" || p === "/readyz")) {
|
|
1828
|
+
const outside = configRedirect(rs, p, outsideRedirects);
|
|
1829
|
+
if (outside) {
|
|
1830
|
+
applySecurityAndHeaders(rs);
|
|
1831
|
+
return outside;
|
|
1832
|
+
}
|
|
1803
1833
|
if (p === basePath || p === basePath + "/")
|
|
1804
1834
|
rs.path = "/";
|
|
1805
1835
|
else if (p.startsWith(basePath + "/"))
|
|
@@ -1810,14 +1840,18 @@ export function createKrateHandler(options) {
|
|
|
1810
1840
|
return goRedirect(basePath + (p.startsWith("/") ? p : "/" + p), p, rs.method, 307, rs.res);
|
|
1811
1841
|
return notFoundText(rs);
|
|
1812
1842
|
}
|
|
1843
|
+
mounted = true;
|
|
1844
|
+
if (rs.rawPath.startsWith(basePath))
|
|
1845
|
+
rs.rawPath = rs.rawPath.slice(basePath.length) || "/";
|
|
1813
1846
|
}
|
|
1814
1847
|
}
|
|
1815
1848
|
applySecurityAndHeaders(rs);
|
|
1816
1849
|
const preflight = applyCORS(rs);
|
|
1817
1850
|
if (preflight)
|
|
1818
|
-
return preflight;
|
|
1851
|
+
return mounted ? withBaseLocation(preflight) : preflight;
|
|
1819
1852
|
await sleepMicrotask();
|
|
1820
|
-
|
|
1853
|
+
const res = await mux(rs);
|
|
1854
|
+
return mounted ? withBaseLocation(res) : res;
|
|
1821
1855
|
}
|
|
1822
1856
|
catch (err) {
|
|
1823
1857
|
console.error(`[krate] panic recovered: ${rs.method} ${rs.path}: ${(err && err.stack) || err}`);
|
|
@@ -533,3 +533,49 @@ describe("middleware exports", () => {
|
|
|
533
533
|
error.mockRestore();
|
|
534
534
|
});
|
|
535
535
|
});
|
|
536
|
+
describe("redirects under a base path", () => {
|
|
537
|
+
const based = {
|
|
538
|
+
...config,
|
|
539
|
+
basePath: "/docs",
|
|
540
|
+
redirects: [
|
|
541
|
+
{ source: "/old", destination: "/new", permanent: true },
|
|
542
|
+
{ source: "/outside", destination: "/elsewhere", basePath: false },
|
|
543
|
+
{ source: "/docs/gone", destination: "https://other.example/", basePath: false },
|
|
544
|
+
],
|
|
545
|
+
};
|
|
546
|
+
const handler = createKrateHandler({
|
|
547
|
+
target: "node",
|
|
548
|
+
data: data({ config: based, apiRoutes: ["hello", "items/[id]", "go"] }),
|
|
549
|
+
modules: {
|
|
550
|
+
...modules,
|
|
551
|
+
api: { ...modules.api, go: { GET: () => new Response(null, { status: 302, headers: { location: "/login?next=1" } }) } },
|
|
552
|
+
},
|
|
553
|
+
});
|
|
554
|
+
const get = (path) => handler(new Request("http://localhost" + path, { redirect: "manual" }));
|
|
555
|
+
it("puts config redirect source and destination under the base", async () => {
|
|
556
|
+
const r = await get("/docs/old");
|
|
557
|
+
expect(r.status).toBe(301);
|
|
558
|
+
expect(r.headers.get("location")).toBe("/docs/new");
|
|
559
|
+
});
|
|
560
|
+
it("adds the base to a root-relative Location from a route handler", async () => {
|
|
561
|
+
const r = await get("/docs/api/go");
|
|
562
|
+
expect(r.status).toBe(302);
|
|
563
|
+
expect(r.headers.get("location")).toBe("/docs/login?next=1");
|
|
564
|
+
});
|
|
565
|
+
it("matches basePath: false rules on the full path and sends them as written", async () => {
|
|
566
|
+
const out = await get("/outside");
|
|
567
|
+
expect(out.status).toBe(302);
|
|
568
|
+
expect(out.headers.get("location")).toBe("/elsewhere");
|
|
569
|
+
const gone = await get("/docs/gone");
|
|
570
|
+
expect(gone.headers.get("location")).toBe("https://other.example/");
|
|
571
|
+
});
|
|
572
|
+
it("does not double the base on the path-cleaning redirect", async () => {
|
|
573
|
+
const r = await get("/docs//new");
|
|
574
|
+
expect(r.headers.get("location")).toBe("/docs/new");
|
|
575
|
+
});
|
|
576
|
+
it("keeps the canonical slash redirect relative", async () => {
|
|
577
|
+
const r = await get("/docs/live");
|
|
578
|
+
expect(r.status).toBe(301);
|
|
579
|
+
expect(r.headers.get("location")).toBe("./live/");
|
|
580
|
+
});
|
|
581
|
+
});
|
package/dist/server.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// @krate/runtime/server - Server-side rendering runtime
|
|
2
2
|
// Provides the same API as the client runtime but renders to HTML strings.
|
|
3
3
|
import { ATTR_ALIASES } from './htmlspec-attributes.js';
|
|
4
|
+
import { withBase } from './base-path.js';
|
|
4
5
|
import { highlight as highlightCode, trimSpace } from './highlight.js';
|
|
5
6
|
export { getActionContext, headers, cookies, redirect } from './action-context.js';
|
|
6
7
|
export { revalidatePath, revalidateTag, unstable_cache, unstable_noStore } from './cache.js';
|
|
@@ -488,7 +489,7 @@ export function Link(props) {
|
|
|
488
489
|
const { href = "", prefetch, replace, scroll, children, ...rest } = props;
|
|
489
490
|
const attrs = [];
|
|
490
491
|
if (href)
|
|
491
|
-
attrs.push(`href="${escapeHTML(href)}"`);
|
|
492
|
+
attrs.push(`href="${escapeHTML(withBase(href))}"`);
|
|
492
493
|
const external = /^(https?:|mailto:|tel:|#|javascript:)/.test(href) ||
|
|
493
494
|
href.startsWith("//") ||
|
|
494
495
|
rest.target === "_blank" ||
|