@krate/runtime 0.5.0-alpha.3 → 0.5.0-alpha.4
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/action-context.d.ts +9 -2
- package/dist/action-context.js +39 -3
- package/dist/actions.js +18 -1
- package/dist/actions.test.js +26 -0
- package/dist/cache-scope.d.ts +118 -0
- package/dist/cache-scope.js +311 -0
- package/dist/cache.d.ts +55 -0
- package/dist/cache.js +155 -0
- package/dist/cache.test.d.ts +1 -0
- package/dist/cache.test.js +217 -0
- package/dist/effects.js +74 -3
- package/dist/index.d.ts +2 -2
- package/dist/index.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/portal.d.ts +5 -1
- package/dist/portal.js +15 -4
- package/dist/request-context.d.ts +62 -0
- package/dist/request-context.js +107 -0
- package/dist/router-layout.test.d.ts +1 -0
- package/dist/router-layout.test.js +179 -0
- package/dist/router-lite.d.ts +25 -0
- package/dist/router-lite.js +73 -0
- package/dist/router-nav.test.d.ts +1 -0
- package/dist/router-nav.test.js +399 -0
- package/dist/router-slots.test.d.ts +1 -0
- package/dist/router-slots.test.js +144 -0
- package/dist/router.d.ts +66 -2
- package/dist/router.js +1535 -650
- package/dist/server-async.test.d.ts +1 -0
- package/dist/server-async.test.js +174 -0
- package/dist/server-renderer.js +455 -118
- package/dist/server.d.ts +23 -0
- package/dist/server.js +157 -0
- package/dist/signal.d.ts +38 -2
- package/dist/signal.js +150 -5
- package/dist/slots.d.ts +5 -1
- package/dist/slots.js +28 -2
- package/package.json +5 -1
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Request context for server components, exported from `@krate/runtime/server`.
|
|
3
|
+
*
|
|
4
|
+
* The SSR sidecar renders every page and region inside an AsyncLocalStorage
|
|
5
|
+
* scope and publishes it on `globalThis[Symbol.for('krate.request.store')]`.
|
|
6
|
+
* `headers()` and `cookies()` read that scope while a server component renders
|
|
7
|
+
* (and the action scope while a server action runs), so the same imports work
|
|
8
|
+
* in both places.
|
|
9
|
+
*/
|
|
10
|
+
const REQUEST_STORE = Symbol.for('krate.request.store');
|
|
11
|
+
/** The current render request, or undefined outside a server render. */
|
|
12
|
+
export function getRequestContext() {
|
|
13
|
+
const store = globalThis[REQUEST_STORE];
|
|
14
|
+
return store && typeof store.getStore === 'function' ? store.getStore() : undefined;
|
|
15
|
+
}
|
|
16
|
+
/** Parses a Cookie request header into name/value pairs. */
|
|
17
|
+
export function parseCookieHeader(header) {
|
|
18
|
+
const out = new Map();
|
|
19
|
+
if (!header)
|
|
20
|
+
return out;
|
|
21
|
+
for (const part of header.split(';')) {
|
|
22
|
+
const eq = part.indexOf('=');
|
|
23
|
+
if (eq <= 0)
|
|
24
|
+
continue;
|
|
25
|
+
const name = part.slice(0, eq).trim();
|
|
26
|
+
let value = part.slice(eq + 1).trim();
|
|
27
|
+
if (value.startsWith('"') && value.endsWith('"'))
|
|
28
|
+
value = value.slice(1, -1);
|
|
29
|
+
try {
|
|
30
|
+
value = decodeURIComponent(value);
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
// keep the raw value when it is not valid percent-encoding
|
|
34
|
+
}
|
|
35
|
+
if (name && !out.has(name))
|
|
36
|
+
out.set(name, value);
|
|
37
|
+
}
|
|
38
|
+
return out;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* thenable makes a value usable both synchronously and with `await`, matching
|
|
42
|
+
* Next.js 15 where `params`, `searchParams`, `cookies()` and `headers()` are
|
|
43
|
+
* Promises: `params.slug` and `(await params).slug` both work. The `then` is
|
|
44
|
+
* non-enumerable, so spreading or JSON-serializing the value is unaffected, and
|
|
45
|
+
* it resolves to a plain copy so awaiting never loops.
|
|
46
|
+
*/
|
|
47
|
+
export function thenable(value, resolved) {
|
|
48
|
+
Object.defineProperty(value, 'then', {
|
|
49
|
+
value: (onFulfilled, onRejected) => Promise.resolve(resolved()).then(onFulfilled, onRejected),
|
|
50
|
+
enumerable: false,
|
|
51
|
+
configurable: true,
|
|
52
|
+
});
|
|
53
|
+
return value;
|
|
54
|
+
}
|
|
55
|
+
/** thenableRecord wraps a plain record (route params, search params). */
|
|
56
|
+
export function thenableRecord(rec) {
|
|
57
|
+
const plain = { ...(rec || {}) };
|
|
58
|
+
return thenable({ ...plain }, () => ({ ...plain }));
|
|
59
|
+
}
|
|
60
|
+
export function requestCookies(jar) {
|
|
61
|
+
const readOnly = (op) => {
|
|
62
|
+
throw new Error(`cookies().${op}() cannot be called while a server component renders; ` +
|
|
63
|
+
`modify cookies in a server action ("use server") or an API route instead`);
|
|
64
|
+
};
|
|
65
|
+
const make = () => ({
|
|
66
|
+
get: (name) => (jar.has(name) ? { name, value: jar.get(name) } : undefined),
|
|
67
|
+
getAll: () => [...jar.entries()].map(([name, value]) => ({ name, value })),
|
|
68
|
+
has: (name) => jar.has(name),
|
|
69
|
+
set: () => readOnly('set'),
|
|
70
|
+
delete: () => readOnly('delete'),
|
|
71
|
+
});
|
|
72
|
+
return thenable(make(), make);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Stops rendering and responds with the 404 page. Works in server components
|
|
76
|
+
* (async pages and their children) rendered by the SSR sidecar.
|
|
77
|
+
*/
|
|
78
|
+
export function notFound() {
|
|
79
|
+
const err = new Error('NEXT_NOT_FOUND');
|
|
80
|
+
err.__krateNotFound = true;
|
|
81
|
+
err.digest = 'NEXT_NOT_FOUND';
|
|
82
|
+
throw err;
|
|
83
|
+
}
|
|
84
|
+
/** isNotFoundSignal reports whether err was thrown by notFound(). */
|
|
85
|
+
export function isNotFoundSignal(err) {
|
|
86
|
+
if (!err || typeof err !== 'object')
|
|
87
|
+
return false;
|
|
88
|
+
const e = err;
|
|
89
|
+
return e.__krateNotFound === true || e.digest === 'NEXT_NOT_FOUND' || e.message === 'NEXT_NOT_FOUND' ||
|
|
90
|
+
(typeof e.digest === 'string' && e.digest.startsWith('NEXT_HTTP_ERROR_FALLBACK;404'));
|
|
91
|
+
}
|
|
92
|
+
/** redirectTarget returns the URL of a redirect() signal, or "" for other errors. */
|
|
93
|
+
export function redirectTarget(err) {
|
|
94
|
+
if (!err || typeof err !== 'object')
|
|
95
|
+
return '';
|
|
96
|
+
const e = err;
|
|
97
|
+
if (typeof e.__krateRedirect === 'string')
|
|
98
|
+
return e.__krateRedirect;
|
|
99
|
+
if (typeof e.message === 'string' && e.message.startsWith('NEXT_REDIRECT:'))
|
|
100
|
+
return e.message.slice('NEXT_REDIRECT:'.length);
|
|
101
|
+
if (typeof e.digest === 'string' && e.digest.startsWith('NEXT_REDIRECT;')) {
|
|
102
|
+
const parts = e.digest.split(';');
|
|
103
|
+
if (parts.length >= 3)
|
|
104
|
+
return parts[2];
|
|
105
|
+
}
|
|
106
|
+
return '';
|
|
107
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
|
2
|
+
import { initRouter, destroyRouter, krateRouter } from './router';
|
|
3
|
+
import { createEffect, createSignal, onCleanup, disposeAll, krateDisposeExcept, krateEnterScope, krateLeaveScope, krateKeepScopes, } from './signal';
|
|
4
|
+
function routeInfo(chain) {
|
|
5
|
+
return `<script type="application/json" id="__krate_route">${JSON.stringify({ v: 1, chain })}</script>`;
|
|
6
|
+
}
|
|
7
|
+
const LAYOUT = { id: '_lroot_', k: 'layout', d: 0, p: true };
|
|
8
|
+
function shell(inner, title = 'Nav') {
|
|
9
|
+
return `<header id="nav"><input id="q">${title}</header><!--kl:_lroot_-->${inner}<!--/kl:_lroot_-->`;
|
|
10
|
+
}
|
|
11
|
+
function doc(title, rootHTML, chain) {
|
|
12
|
+
return `<!doctype html><html><head><title>${title}</title></head><body><div id="root">${rootHTML}</div>${routeInfo(chain)}</body></html>`;
|
|
13
|
+
}
|
|
14
|
+
let routes;
|
|
15
|
+
function setup(rootHTML, chain, path = '/a') {
|
|
16
|
+
history.replaceState(null, '', path);
|
|
17
|
+
document.head.innerHTML = '<title>A</title>';
|
|
18
|
+
document.body.innerHTML = `<div id="root">${rootHTML}</div>${routeInfo(chain)}`;
|
|
19
|
+
initRouter();
|
|
20
|
+
}
|
|
21
|
+
beforeEach(() => {
|
|
22
|
+
destroyRouter();
|
|
23
|
+
disposeAll();
|
|
24
|
+
globalThis.__KRATE_CFG__ = { viewTransitions: false };
|
|
25
|
+
const g = globalThis;
|
|
26
|
+
g.krateDisposeExcept = krateDisposeExcept;
|
|
27
|
+
g.krateKeepScopes = krateKeepScopes;
|
|
28
|
+
g.disposeAll = disposeAll;
|
|
29
|
+
g.__krateHardNavigate = () => { };
|
|
30
|
+
routes = {};
|
|
31
|
+
vi.stubGlobal('fetch', vi.fn(async (input) => {
|
|
32
|
+
const u = new URL(input, location.href);
|
|
33
|
+
const body = routes[u.pathname];
|
|
34
|
+
return {
|
|
35
|
+
ok: !!body,
|
|
36
|
+
status: body ? 200 : 404,
|
|
37
|
+
url: u.href,
|
|
38
|
+
headers: new Headers({ 'content-type': 'text/html' }),
|
|
39
|
+
text: async () => body || '',
|
|
40
|
+
};
|
|
41
|
+
}));
|
|
42
|
+
});
|
|
43
|
+
afterEach(() => {
|
|
44
|
+
destroyRouter();
|
|
45
|
+
vi.unstubAllGlobals();
|
|
46
|
+
const g = globalThis;
|
|
47
|
+
delete g.krateDisposeExcept;
|
|
48
|
+
delete g.krateKeepScopes;
|
|
49
|
+
delete g.disposeAll;
|
|
50
|
+
delete g.__krateHardNavigate;
|
|
51
|
+
delete g.__KRATE_CFG__;
|
|
52
|
+
});
|
|
53
|
+
describe('layout persistence', () => {
|
|
54
|
+
it('keeps a shared layout mounted and swaps only its children', async () => {
|
|
55
|
+
setup(shell('<main>Page A</main>'), [LAYOUT]);
|
|
56
|
+
const nav = document.getElementById('nav');
|
|
57
|
+
document.getElementById('q').value = 'typed';
|
|
58
|
+
routes['/b'] = doc('B', shell('<main>Page B</main>'), [LAYOUT]);
|
|
59
|
+
await krateRouter.push('/b');
|
|
60
|
+
expect(document.querySelector('main').textContent).toBe('Page B');
|
|
61
|
+
expect(document.getElementById('nav')).toBe(nav);
|
|
62
|
+
expect(document.getElementById('q').value).toBe('typed');
|
|
63
|
+
});
|
|
64
|
+
it('keeps effects of the kept layout scope and disposes page effects', async () => {
|
|
65
|
+
setup(shell('<main>Page A</main>'), [LAYOUT]);
|
|
66
|
+
const log = [];
|
|
67
|
+
const [count, setCount] = createSignal(0);
|
|
68
|
+
// Layout hydration code runs inside its route scope.
|
|
69
|
+
expect(krateEnterScope('_lroot_')).toBe(true);
|
|
70
|
+
createEffect(() => {
|
|
71
|
+
log.push('layout:' + count());
|
|
72
|
+
onCleanup(() => log.push('layout-cleanup'));
|
|
73
|
+
});
|
|
74
|
+
krateLeaveScope();
|
|
75
|
+
// Page hydration code (no scope).
|
|
76
|
+
createEffect(() => {
|
|
77
|
+
onCleanup(() => log.push('page-cleanup'));
|
|
78
|
+
});
|
|
79
|
+
onCleanup(() => log.push('page-root-cleanup'));
|
|
80
|
+
log.length = 0;
|
|
81
|
+
routes['/b'] = doc('B', shell('<main>Page B</main>'), [LAYOUT]);
|
|
82
|
+
await krateRouter.push('/b');
|
|
83
|
+
expect(log).toContain('page-cleanup');
|
|
84
|
+
expect(log).toContain('page-root-cleanup');
|
|
85
|
+
expect(log).not.toContain('layout-cleanup');
|
|
86
|
+
log.length = 0;
|
|
87
|
+
setCount(1);
|
|
88
|
+
await new Promise((r) => setTimeout(r, 0));
|
|
89
|
+
// The layout effect is still live.
|
|
90
|
+
expect(log).toEqual(['layout-cleanup', 'layout:1']);
|
|
91
|
+
});
|
|
92
|
+
it('remounts layouts the compiler marked as not persistable', async () => {
|
|
93
|
+
globalThis.__KRATE_CFG__ = { viewTransitions: false, dev: true };
|
|
94
|
+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => { });
|
|
95
|
+
const blocked = { ...LAYOUT, p: false, why: 'the page uses class components' };
|
|
96
|
+
setup(shell('<main>Page A</main>'), [blocked]);
|
|
97
|
+
const nav = document.getElementById('nav');
|
|
98
|
+
routes['/b'] = doc('B', shell('<main>Page B</main>'), [blocked]);
|
|
99
|
+
await krateRouter.push('/b');
|
|
100
|
+
expect(document.getElementById('nav')).not.toBe(nav);
|
|
101
|
+
expect(warn.mock.calls.some((c) => String(c[0]).includes('class components'))).toBe(true);
|
|
102
|
+
warn.mockRestore();
|
|
103
|
+
});
|
|
104
|
+
it('swaps the whole root when the layouts differ', async () => {
|
|
105
|
+
setup(shell('<main>Page A</main>'), [LAYOUT]);
|
|
106
|
+
const nav = document.getElementById('nav');
|
|
107
|
+
const other = { id: '_lother_', k: 'layout', d: 0, p: true };
|
|
108
|
+
routes['/b'] = doc('B', '<aside>other</aside><!--kl:_lother_--><main>B</main><!--/kl:_lother_-->', [other]);
|
|
109
|
+
await krateRouter.push('/b');
|
|
110
|
+
expect(nav.isConnected).toBe(false);
|
|
111
|
+
expect(document.querySelector('aside')).not.toBeNull();
|
|
112
|
+
});
|
|
113
|
+
it('remounts a layout under a dynamic segment when the segment value changes', async () => {
|
|
114
|
+
const section = { id: '_lpost_', k: 'layout', d: 2, p: true };
|
|
115
|
+
const html = (slug) => shell(`<div id="post-shell">${slug}<!--kl:_lpost_--><p>${slug}</p><!--/kl:_lpost_--></div>`);
|
|
116
|
+
setup(html('a'), [LAYOUT, section], '/blog/a');
|
|
117
|
+
const nav = document.getElementById('nav');
|
|
118
|
+
const postShell = document.getElementById('post-shell');
|
|
119
|
+
routes['/blog/b'] = doc('B', html('b'), [LAYOUT, section]);
|
|
120
|
+
await krateRouter.push('/blog/b');
|
|
121
|
+
// The root layout stays, the [slug] layout remounts.
|
|
122
|
+
expect(document.getElementById('nav')).toBe(nav);
|
|
123
|
+
expect(document.getElementById('post-shell')).not.toBe(postShell);
|
|
124
|
+
expect(document.querySelector('p').textContent).toBe('b');
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
describe('templates', () => {
|
|
128
|
+
it('remounts the template on every navigation, keeping the parent layout', async () => {
|
|
129
|
+
const tpl = { id: '_ltpl_', k: 'template', d: 0, p: false };
|
|
130
|
+
const html = (page) => shell(`<section id="tpl"><input id="t"><!--kl:_ltpl_--><main>${page}</main><!--/kl:_ltpl_--></section>`);
|
|
131
|
+
setup(html('A'), [LAYOUT, tpl]);
|
|
132
|
+
const nav = document.getElementById('nav');
|
|
133
|
+
const section = document.getElementById('tpl');
|
|
134
|
+
document.getElementById('t').value = 'stale';
|
|
135
|
+
routes['/b'] = doc('B', html('B'), [LAYOUT, tpl]);
|
|
136
|
+
await krateRouter.push('/b');
|
|
137
|
+
expect(document.getElementById('nav')).toBe(nav);
|
|
138
|
+
expect(document.getElementById('tpl')).not.toBe(section);
|
|
139
|
+
expect(document.getElementById('t').value).toBe('');
|
|
140
|
+
expect(document.querySelector('main').textContent).toBe('B');
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
describe('route scopes', () => {
|
|
144
|
+
it('krateEnterScope skips kept scopes only while the router keeps them', () => {
|
|
145
|
+
krateKeepScopes(['_lroot_']);
|
|
146
|
+
expect(krateEnterScope('_lroot_')).toBe(false);
|
|
147
|
+
expect(krateEnterScope('_lother_')).toBe(true);
|
|
148
|
+
krateLeaveScope();
|
|
149
|
+
krateKeepScopes([]);
|
|
150
|
+
expect(krateEnterScope('_lroot_')).toBe(true);
|
|
151
|
+
krateLeaveScope();
|
|
152
|
+
});
|
|
153
|
+
it('krateDisposeExcept([]) is a full teardown', () => {
|
|
154
|
+
const log = [];
|
|
155
|
+
krateEnterScope('_lroot_');
|
|
156
|
+
onCleanup(() => log.push('layout'));
|
|
157
|
+
krateLeaveScope();
|
|
158
|
+
onCleanup(() => log.push('page'));
|
|
159
|
+
krateDisposeExcept([]);
|
|
160
|
+
expect(log.sort()).toEqual(['layout', 'page']);
|
|
161
|
+
});
|
|
162
|
+
it('effects created later by a kept effect inherit its scope', async () => {
|
|
163
|
+
const log = [];
|
|
164
|
+
const [show, setShow] = createSignal(false);
|
|
165
|
+
krateEnterScope('_lroot_');
|
|
166
|
+
createEffect(() => {
|
|
167
|
+
if (show()) {
|
|
168
|
+
createEffect(() => onCleanup(() => log.push('nested')));
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
krateLeaveScope();
|
|
172
|
+
setShow(true);
|
|
173
|
+
await new Promise((r) => setTimeout(r, 0));
|
|
174
|
+
krateDisposeExcept(['_lroot_']);
|
|
175
|
+
expect(log).toEqual([]);
|
|
176
|
+
disposeAll();
|
|
177
|
+
expect(log).toEqual(['nested']);
|
|
178
|
+
});
|
|
179
|
+
});
|
package/dist/router-lite.d.ts
CHANGED
|
@@ -3,6 +3,31 @@ export interface NavigateOptions {
|
|
|
3
3
|
scroll?: boolean;
|
|
4
4
|
state?: unknown;
|
|
5
5
|
}
|
|
6
|
+
export interface NavigationState {
|
|
7
|
+
url: string;
|
|
8
|
+
pathname: string;
|
|
9
|
+
search: string;
|
|
10
|
+
hash: string;
|
|
11
|
+
status: 'idle' | 'loading';
|
|
12
|
+
}
|
|
13
|
+
export declare function getNavigationState(): NavigationState;
|
|
14
|
+
/** Without the SPA router the URL only changes in-document on hash changes. */
|
|
15
|
+
export declare function subscribeNavigation(fn: (s: NavigationState) => void): () => void;
|
|
16
|
+
export declare function getHistoryState(): unknown;
|
|
17
|
+
/** No prefetch cache exists without the SPA router. */
|
|
18
|
+
export declare function invalidatePrefetchCache(_url?: string): void;
|
|
6
19
|
export declare function initRouter(): void;
|
|
7
20
|
export declare function reinitRouter(): void;
|
|
21
|
+
export declare function destroyRouter(): void;
|
|
8
22
|
export declare function navigate(url: string, opts?: NavigateOptions): boolean;
|
|
23
|
+
export declare const krateRouter: {
|
|
24
|
+
push(url: string, opts?: NavigateOptions): Promise<void>;
|
|
25
|
+
replace(url: string, opts?: NavigateOptions): Promise<void>;
|
|
26
|
+
back(): void;
|
|
27
|
+
forward(): void;
|
|
28
|
+
refresh(): Promise<void>;
|
|
29
|
+
prefetch(_url: string): Promise<void>;
|
|
30
|
+
subscribe: typeof subscribeNavigation;
|
|
31
|
+
getState: typeof getNavigationState;
|
|
32
|
+
invalidate: typeof invalidatePrefetchCache;
|
|
33
|
+
};
|
package/dist/router-lite.js
CHANGED
|
@@ -1,5 +1,40 @@
|
|
|
1
|
+
// Router API for sites with the SPA router disabled (`spa: false`). Every
|
|
2
|
+
// navigation is a full document load; the API surface matches router.ts so
|
|
3
|
+
// application code (and the Next.js migration shim) works unchanged.
|
|
4
|
+
function basePath() {
|
|
5
|
+
const cfg = globalThis.__KRATE_CFG__;
|
|
6
|
+
const bp = (cfg && cfg.basePath) || '';
|
|
7
|
+
if (!bp || bp === '/')
|
|
8
|
+
return '';
|
|
9
|
+
return '/' + bp.replace(/^\/+|\/+$/g, '');
|
|
10
|
+
}
|
|
11
|
+
export function getNavigationState() {
|
|
12
|
+
if (typeof location === 'undefined')
|
|
13
|
+
return { url: '', pathname: '/', search: '', hash: '', status: 'idle' };
|
|
14
|
+
const bp = basePath();
|
|
15
|
+
let pathname = location.pathname;
|
|
16
|
+
if (bp && (pathname === bp || pathname.startsWith(bp + '/')))
|
|
17
|
+
pathname = pathname.slice(bp.length) || '/';
|
|
18
|
+
return { url: location.href, pathname, search: location.search, hash: location.hash, status: 'idle' };
|
|
19
|
+
}
|
|
20
|
+
/** Without the SPA router the URL only changes in-document on hash changes. */
|
|
21
|
+
export function subscribeNavigation(fn) {
|
|
22
|
+
if (typeof window === 'undefined')
|
|
23
|
+
return () => { };
|
|
24
|
+
const handler = () => fn(getNavigationState());
|
|
25
|
+
window.addEventListener('hashchange', handler);
|
|
26
|
+
return () => {
|
|
27
|
+
window.removeEventListener('hashchange', handler);
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
export function getHistoryState() {
|
|
31
|
+
return typeof history === 'undefined' ? null : history.state;
|
|
32
|
+
}
|
|
33
|
+
/** No prefetch cache exists without the SPA router. */
|
|
34
|
+
export function invalidatePrefetchCache(_url) { }
|
|
1
35
|
export function initRouter() { }
|
|
2
36
|
export function reinitRouter() { }
|
|
37
|
+
export function destroyRouter() { }
|
|
3
38
|
export function navigate(url, opts = {}) {
|
|
4
39
|
if (typeof location === 'undefined')
|
|
5
40
|
return true;
|
|
@@ -9,3 +44,41 @@ export function navigate(url, opts = {}) {
|
|
|
9
44
|
location.assign(url);
|
|
10
45
|
return true;
|
|
11
46
|
}
|
|
47
|
+
export const krateRouter = {
|
|
48
|
+
push(url, opts = {}) {
|
|
49
|
+
navigate(url, { ...opts, replace: false });
|
|
50
|
+
return Promise.resolve();
|
|
51
|
+
},
|
|
52
|
+
replace(url, opts = {}) {
|
|
53
|
+
navigate(url, { ...opts, replace: true });
|
|
54
|
+
return Promise.resolve();
|
|
55
|
+
},
|
|
56
|
+
back() {
|
|
57
|
+
if (typeof history !== 'undefined')
|
|
58
|
+
history.back();
|
|
59
|
+
},
|
|
60
|
+
forward() {
|
|
61
|
+
if (typeof history !== 'undefined')
|
|
62
|
+
history.forward();
|
|
63
|
+
},
|
|
64
|
+
refresh() {
|
|
65
|
+
if (typeof location !== 'undefined')
|
|
66
|
+
location.reload();
|
|
67
|
+
return Promise.resolve();
|
|
68
|
+
},
|
|
69
|
+
// Full-page navigations go through the browser's HTTP cache; there is no
|
|
70
|
+
// client document cache to warm.
|
|
71
|
+
prefetch(_url) {
|
|
72
|
+
return Promise.resolve();
|
|
73
|
+
},
|
|
74
|
+
subscribe: subscribeNavigation,
|
|
75
|
+
getState: getNavigationState,
|
|
76
|
+
invalidate: invalidatePrefetchCache,
|
|
77
|
+
};
|
|
78
|
+
if (typeof window !== 'undefined') {
|
|
79
|
+
// Revalidated server data is only visible after a reload without the SPA router.
|
|
80
|
+
window.addEventListener('krate:revalidate', () => {
|
|
81
|
+
if (typeof location !== 'undefined')
|
|
82
|
+
location.reload();
|
|
83
|
+
});
|
|
84
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|