@krate/runtime 0.5.0-alpha.4 → 0.5.0-beta.1
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 +5 -1
- package/dist/action-context.js +33 -3
- package/dist/action-context.test.js +26 -1
- package/dist/actions.d.ts +12 -1
- package/dist/actions.js +83 -4
- package/dist/actions.test.js +61 -1
- package/dist/cache-scope.js +4 -1
- package/dist/cache.test.js +16 -0
- package/dist/class-helpers.d.ts +4 -4
- package/dist/class-helpers.js +357 -32
- package/dist/class-helpers.test.d.ts +1 -0
- package/dist/class-helpers.test.js +169 -0
- package/dist/context.d.ts +16 -2
- package/dist/context.js +44 -3
- package/dist/dev-overlay.js +75 -2
- package/dist/dev-overlay.test.js +22 -0
- package/dist/dom.d.ts +7 -0
- package/dist/dom.js +69 -3
- package/dist/error-overlay.js +4 -4
- package/dist/hook-args.d.ts +7 -0
- package/dist/hook-args.js +10 -0
- package/dist/htmlspec-attributes.d.ts +1 -0
- package/dist/htmlspec-attributes.js +159 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/krate-core.js +2 -0
- package/dist/krate-dev.js +65 -2
- package/dist/krate-f-actions.js +2 -0
- package/dist/krate-f-classes.js +8 -0
- package/dist/krate-f-helpers.js +2 -0
- package/dist/krate-f-list.js +2 -0
- package/dist/krate-f-router-lite.js +2 -0
- package/dist/krate-f-router.js +2 -0
- package/dist/krate-features.json +239 -0
- package/dist/krate-hydrate-lite.js +5 -5
- package/dist/krate-hydrate.js +5 -5
- package/dist/krate-runtime.js +5 -5
- package/dist/patterns.test.d.ts +1 -0
- package/dist/patterns.test.js +98 -0
- package/dist/reactivity-core.test.d.ts +1 -0
- package/dist/reactivity-core.test.js +152 -0
- package/dist/reconcile.js +4 -2
- package/dist/router-nav.test.js +38 -0
- package/dist/router-speculation.test.d.ts +1 -0
- package/dist/router-speculation.test.js +25 -0
- package/dist/router-styles.test.d.ts +1 -0
- package/dist/router-styles.test.js +98 -0
- package/dist/router.js +116 -32
- package/dist/server-async.test.js +20 -0
- package/dist/server-renderer.js +109 -22
- package/dist/server.d.ts +1 -0
- package/dist/server.js +90 -11
- package/dist/server.test.js +20 -0
- package/dist/signal.d.ts +3 -2
- package/dist/signal.js +104 -19
- package/dist/slots.js +12 -1
- package/package.json +2 -2
package/dist/action-context.d.ts
CHANGED
|
@@ -17,7 +17,11 @@ export interface ActionCookieOptions {
|
|
|
17
17
|
}
|
|
18
18
|
/** Cookie access for the current action request. */
|
|
19
19
|
export interface ActionCookies {
|
|
20
|
-
|
|
20
|
+
/** Returns `{ name, value }` like Next.js, or undefined when the cookie is absent. */
|
|
21
|
+
get(name: string): {
|
|
22
|
+
name: string;
|
|
23
|
+
value: string;
|
|
24
|
+
} | undefined;
|
|
21
25
|
getAll(): Array<{
|
|
22
26
|
name: string;
|
|
23
27
|
value: string;
|
package/dist/action-context.js
CHANGED
|
@@ -5,11 +5,37 @@
|
|
|
5
5
|
* publishes it on `globalThis[Symbol.for('krate.action.store')]`. These helpers
|
|
6
6
|
* read that scope, so they only work while a server action is executing.
|
|
7
7
|
*/
|
|
8
|
+
import { markNoStore } from './cache-scope.js';
|
|
8
9
|
import { getRequestContext, requestCookies, thenable } from './request-context.js';
|
|
9
10
|
/** The current action context, or undefined outside a server action. */
|
|
10
11
|
export function getActionContext() {
|
|
11
12
|
const store = globalThis[Symbol.for('krate.action.store')];
|
|
12
|
-
|
|
13
|
+
const ctx = store && typeof store.getStore === 'function' ? store.getStore() : undefined;
|
|
14
|
+
return ctx ? withNextCookies(ctx) : undefined;
|
|
15
|
+
}
|
|
16
|
+
// The sidecar's jar returns the bare value from get(); expose the Next.js
|
|
17
|
+
// shape ({ name, value }) the server-component view already uses.
|
|
18
|
+
const wrapped = new WeakMap();
|
|
19
|
+
function withNextCookies(ctx) {
|
|
20
|
+
let out = wrapped.get(ctx);
|
|
21
|
+
if (!out) {
|
|
22
|
+
const jar = ctx.cookies;
|
|
23
|
+
const cookies = {
|
|
24
|
+
get: (name) => {
|
|
25
|
+
const v = jar.get(name);
|
|
26
|
+
if (v === undefined || v === null)
|
|
27
|
+
return undefined;
|
|
28
|
+
return typeof v === 'object' ? v : { name, value: String(v) };
|
|
29
|
+
},
|
|
30
|
+
getAll: () => jar.getAll(),
|
|
31
|
+
has: (name) => jar.has(name),
|
|
32
|
+
set: (name, value, options) => jar.set(name, value, options),
|
|
33
|
+
delete: (name, options) => jar.delete(name, options),
|
|
34
|
+
};
|
|
35
|
+
out = { ...ctx, cookies };
|
|
36
|
+
wrapped.set(ctx, out);
|
|
37
|
+
}
|
|
38
|
+
return out;
|
|
13
39
|
}
|
|
14
40
|
function requireContext(api) {
|
|
15
41
|
const ctx = getActionContext();
|
|
@@ -28,8 +54,10 @@ export function headers() {
|
|
|
28
54
|
if (action)
|
|
29
55
|
return awaitableHeaders(action.headers);
|
|
30
56
|
const req = getRequestContext();
|
|
31
|
-
if (req)
|
|
57
|
+
if (req) {
|
|
58
|
+
markNoStore();
|
|
32
59
|
return awaitableHeaders(req.headers);
|
|
60
|
+
}
|
|
33
61
|
return requireContext('headers').headers;
|
|
34
62
|
}
|
|
35
63
|
/**
|
|
@@ -42,8 +70,10 @@ export function cookies() {
|
|
|
42
70
|
if (action)
|
|
43
71
|
return awaitableActionCookies(action.cookies);
|
|
44
72
|
const req = getRequestContext();
|
|
45
|
-
if (req)
|
|
73
|
+
if (req) {
|
|
74
|
+
markNoStore();
|
|
46
75
|
return requestCookies(req.cookies);
|
|
76
|
+
}
|
|
47
77
|
return requireContext('cookies').cookies;
|
|
48
78
|
}
|
|
49
79
|
function awaitableActionCookies(c) {
|
|
@@ -1,9 +1,13 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
1
2
|
import { afterEach, describe, expect, it } from 'vitest';
|
|
2
3
|
import { cookies, getActionContext, headers, redirect } from './action-context.js';
|
|
4
|
+
import { installRenderStore, withCacheScope } from './cache-scope.js';
|
|
3
5
|
import { jsx, renderToString } from './server.js';
|
|
4
6
|
const KEY = Symbol.for('krate.action.store');
|
|
7
|
+
const REQUEST_KEY = Symbol.for('krate.request.store');
|
|
5
8
|
afterEach(() => {
|
|
6
9
|
delete globalThis[KEY];
|
|
10
|
+
delete globalThis[REQUEST_KEY];
|
|
7
11
|
});
|
|
8
12
|
describe('server action context', () => {
|
|
9
13
|
it('throws outside an action with an actionable message', () => {
|
|
@@ -27,9 +31,30 @@ describe('server action context', () => {
|
|
|
27
31
|
};
|
|
28
32
|
globalThis[KEY] = { getStore: () => ctx };
|
|
29
33
|
expect(headers().get('x-test')).toBe('yes');
|
|
30
|
-
expect(cookies().get('sid')).
|
|
34
|
+
expect(cookies().get('sid')).toEqual({ name: 'sid', value: 'abc' });
|
|
35
|
+
expect(cookies().get('sid')?.value).toBe('abc');
|
|
36
|
+
expect(cookies().get('missing')).toBeUndefined();
|
|
37
|
+
expect(getActionContext()?.cookies.get('sid')).toEqual({ name: 'sid', value: 'abc' });
|
|
31
38
|
expect(getActionContext()?.actionId).toBe('a1');
|
|
32
39
|
});
|
|
40
|
+
it('marks a render uncacheable when headers or cookies are read', async () => {
|
|
41
|
+
const ctx = {
|
|
42
|
+
url: '/p',
|
|
43
|
+
method: 'GET',
|
|
44
|
+
headers: new Headers({ 'x-test': 'yes' }),
|
|
45
|
+
cookies: new Map([['sid', 'abc']]),
|
|
46
|
+
params: {},
|
|
47
|
+
searchParams: {},
|
|
48
|
+
};
|
|
49
|
+
globalThis[REQUEST_KEY] = { getStore: () => ctx };
|
|
50
|
+
installRenderStore(new AsyncLocalStorage());
|
|
51
|
+
const { scope } = await withCacheScope('/p', () => {
|
|
52
|
+
headers();
|
|
53
|
+
cookies();
|
|
54
|
+
return 'ok';
|
|
55
|
+
});
|
|
56
|
+
expect(scope.noStore).toBe(true);
|
|
57
|
+
});
|
|
33
58
|
it('redirect() throws the marker the sidecar turns into a navigation', () => {
|
|
34
59
|
let caught;
|
|
35
60
|
try {
|
package/dist/actions.d.ts
CHANGED
|
@@ -88,7 +88,7 @@ export declare function getFormStatus(form: HTMLFormElement): FormStatus;
|
|
|
88
88
|
export declare function requestFormReset(form: HTMLFormElement): void;
|
|
89
89
|
type Getter = () => unknown;
|
|
90
90
|
/** Binds a function action to a form or submit button (`action={fn}` / `formAction={fn}`). */
|
|
91
|
-
export declare function __krateBindFormAction(el: Element, get: Getter): void;
|
|
91
|
+
export declare function __krateBindFormAction(el: Element | null, get: Getter): void;
|
|
92
92
|
interface RunnableAction {
|
|
93
93
|
(payload: unknown): unknown;
|
|
94
94
|
__krateRun?: (payload: unknown) => Promise<boolean>;
|
|
@@ -101,6 +101,17 @@ interface RunnableAction {
|
|
|
101
101
|
export declare function submitFormAction(form: HTMLFormElement, action: unknown, data: FormData, submitter?: HTMLElement | null, url?: string): Promise<boolean>;
|
|
102
102
|
/** Installs the document submit listener once per window. */
|
|
103
103
|
export declare function installActionForms(doc?: Document | undefined): void;
|
|
104
|
+
/**
|
|
105
|
+
* Reads and clears the flash state a native form post left for action `id`:
|
|
106
|
+
* the `krate_flash` cookie the action endpoint sets, or, when the server
|
|
107
|
+
* already rendered it into the page (a no-JS style notice carrying the same
|
|
108
|
+
* payload in `data-krate-flash`), that element, which is removed because the
|
|
109
|
+
* form state now shows it.
|
|
110
|
+
*/
|
|
111
|
+
export declare function takeFlash(id: string): {
|
|
112
|
+
found: boolean;
|
|
113
|
+
value?: unknown;
|
|
114
|
+
};
|
|
104
115
|
/**
|
|
105
116
|
* Backs `const [state, formAction, isPending] = useActionState(fn, initial)`.
|
|
106
117
|
* The returned dispatch can be a form action, a button formAction, or called
|
package/dist/actions.js
CHANGED
|
@@ -42,12 +42,26 @@ function basePath() {
|
|
|
42
42
|
export function isServerAction(value) {
|
|
43
43
|
return typeof value === 'function' && typeof value.$$krateAction === 'string';
|
|
44
44
|
}
|
|
45
|
+
// Counts redirects the client followed, so useActionState can tell that an
|
|
46
|
+
// action navigated away instead of producing a next state.
|
|
47
|
+
let redirectsFollowed = 0;
|
|
45
48
|
function navigateTo(url) {
|
|
49
|
+
const safe = safeRedirect(url);
|
|
50
|
+
if (!safe)
|
|
51
|
+
return;
|
|
46
52
|
const nav = globalThis.navigate;
|
|
47
53
|
if (typeof nav === 'function')
|
|
48
|
-
nav(
|
|
54
|
+
nav(safe);
|
|
49
55
|
else if (typeof location !== 'undefined')
|
|
50
|
-
location.assign(
|
|
56
|
+
location.assign(safe);
|
|
57
|
+
}
|
|
58
|
+
/** Only same-origin absolute paths are followed after an action. */
|
|
59
|
+
function safeRedirect(url) {
|
|
60
|
+
if (!url || !url.startsWith('/') || url.startsWith('//'))
|
|
61
|
+
return '';
|
|
62
|
+
if (url.includes('\\') || /[\u0000-\u001f\u007f]/.test(url))
|
|
63
|
+
return '';
|
|
64
|
+
return url;
|
|
51
65
|
}
|
|
52
66
|
/**
|
|
53
67
|
* Tells the page that the action revalidated server caches. The router
|
|
@@ -114,8 +128,10 @@ export async function callServerAction(url, args) {
|
|
|
114
128
|
}
|
|
115
129
|
if (payload.revalidated)
|
|
116
130
|
dispatchRevalidate(payload.revalidation);
|
|
117
|
-
if (typeof payload.redirect === 'string' && payload.redirect)
|
|
131
|
+
if (typeof payload.redirect === 'string' && payload.redirect) {
|
|
132
|
+
redirectsFollowed++;
|
|
118
133
|
navigateTo(payload.redirect);
|
|
134
|
+
}
|
|
119
135
|
return payload.result;
|
|
120
136
|
}
|
|
121
137
|
function makeProxy(id, bound) {
|
|
@@ -315,6 +331,10 @@ export function requestFormReset(form) {
|
|
|
315
331
|
}
|
|
316
332
|
/** Binds a function action to a form or submit button (`action={fn}` / `formAction={fn}`). */
|
|
317
333
|
export function __krateBindFormAction(el, get) {
|
|
334
|
+
// The ref callback is called with null when the element detaches (a soft
|
|
335
|
+
// navigation replacing the page), which has nothing left to bind.
|
|
336
|
+
if (!el)
|
|
337
|
+
return;
|
|
318
338
|
el.__krateFormAction = get;
|
|
319
339
|
// The native POST target only serves the no-JS fallback; once the function
|
|
320
340
|
// is bound the element matches React's client DOM (no action attribute).
|
|
@@ -456,6 +476,55 @@ export function installActionForms(doc = typeof document !== 'undefined' ? docum
|
|
|
456
476
|
w.__krateActionBound = true;
|
|
457
477
|
doc.addEventListener('submit', onSubmit, false);
|
|
458
478
|
}
|
|
479
|
+
// Flash state from native form posts
|
|
480
|
+
function decodeFlash(value) {
|
|
481
|
+
try {
|
|
482
|
+
const raw = value.replace(/-/g, '+').replace(/_/g, '/');
|
|
483
|
+
const bin = atob(raw + '='.repeat((4 - (raw.length % 4)) % 4));
|
|
484
|
+
return JSON.parse(decodeURIComponent(escape(bin)));
|
|
485
|
+
}
|
|
486
|
+
catch {
|
|
487
|
+
return null;
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
/**
|
|
491
|
+
* Reads and clears the flash state a native form post left for action `id`:
|
|
492
|
+
* the `krate_flash` cookie the action endpoint sets, or, when the server
|
|
493
|
+
* already rendered it into the page (a no-JS style notice carrying the same
|
|
494
|
+
* payload in `data-krate-flash`), that element, which is removed because the
|
|
495
|
+
* form state now shows it.
|
|
496
|
+
*/
|
|
497
|
+
export function takeFlash(id) {
|
|
498
|
+
if (typeof document === 'undefined')
|
|
499
|
+
return { found: false };
|
|
500
|
+
const m = /(?:^|;\s*)krate_flash=([^;]*)/.exec(document.cookie || '');
|
|
501
|
+
if (m) {
|
|
502
|
+
const payload = decodeFlash(m[1]);
|
|
503
|
+
if (payload && payload.a === id) {
|
|
504
|
+
document.cookie = 'krate_flash=; Path=/; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT; SameSite=Lax';
|
|
505
|
+
removeFlashNotices(id);
|
|
506
|
+
return { found: true, value: payload.r };
|
|
507
|
+
}
|
|
508
|
+
return { found: false };
|
|
509
|
+
}
|
|
510
|
+
const notices = document.querySelectorAll('[data-krate-flash]');
|
|
511
|
+
for (let i = 0; i < notices.length; i++) {
|
|
512
|
+
const payload = decodeFlash(notices[i].getAttribute('data-krate-flash') || '');
|
|
513
|
+
if (payload && payload.a === id) {
|
|
514
|
+
removeFlashNotices(id);
|
|
515
|
+
return { found: true, value: payload.r };
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
return { found: false };
|
|
519
|
+
}
|
|
520
|
+
function removeFlashNotices(id) {
|
|
521
|
+
const notices = document.querySelectorAll('[data-krate-flash]');
|
|
522
|
+
for (let i = 0; i < notices.length; i++) {
|
|
523
|
+
const payload = decodeFlash(notices[i].getAttribute('data-krate-flash') || '');
|
|
524
|
+
if (payload && payload.a === id)
|
|
525
|
+
notices[i].parentNode?.removeChild(notices[i]);
|
|
526
|
+
}
|
|
527
|
+
}
|
|
459
528
|
/**
|
|
460
529
|
* Backs `const [state, formAction, isPending] = useActionState(fn, initial)`.
|
|
461
530
|
* The returned dispatch can be a form action, a button formAction, or called
|
|
@@ -466,14 +535,24 @@ export function installActionForms(doc = typeof document !== 'undefined' ? docum
|
|
|
466
535
|
export function __krateActionState(action, getState, setState, setPending) {
|
|
467
536
|
const queue = [];
|
|
468
537
|
let running = false;
|
|
538
|
+
// A native (no-JS or not-yet-hydrated) form post redirected back to this page
|
|
539
|
+
// with the action's returned state in a short-lived cookie: start from it.
|
|
540
|
+
if (isServerAction(action)) {
|
|
541
|
+
const flashed = takeFlash(action.$$krateAction);
|
|
542
|
+
if (flashed.found)
|
|
543
|
+
setState(flashed.value);
|
|
544
|
+
}
|
|
469
545
|
const drain = async () => {
|
|
470
546
|
running = true;
|
|
471
547
|
while (queue.length > 0) {
|
|
472
548
|
const item = queue.shift();
|
|
473
549
|
let ok = false;
|
|
474
550
|
try {
|
|
551
|
+
const before = redirectsFollowed;
|
|
475
552
|
const next = await runActionTransition(() => action(getState(), item.payload));
|
|
476
|
-
|
|
553
|
+
// A redirecting action has no next state: the page is being replaced.
|
|
554
|
+
if (redirectsFollowed === before)
|
|
555
|
+
krateTransitionWrites(() => setState(next));
|
|
477
556
|
ok = true;
|
|
478
557
|
}
|
|
479
558
|
catch (err) {
|
package/dist/actions.test.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
2
|
import { createSignal } from './signal.js';
|
|
3
3
|
import { krateOn } from './events.js';
|
|
4
|
-
import { ACTION_ARGS_FIELD, ServerActionError, __krateAction, __krateActionState, __krateBeginAction, __krateBindFormAction, __krateFormStatusMount, __krateOptimistic, getFormStatus, installActionForms, isServerAction, requestFormReset, setActionTransitionRunner, } from './actions.js';
|
|
4
|
+
import { ACTION_ARGS_FIELD, ServerActionError, __krateAction, __krateActionState, __krateBeginAction, __krateBindFormAction, __krateFormStatusMount, __krateOptimistic, getFormStatus, installActionForms, isServerAction, requestFormReset, setActionTransitionRunner, takeFlash, } from './actions.js';
|
|
5
5
|
function deferred() {
|
|
6
6
|
let resolve;
|
|
7
7
|
let reject;
|
|
@@ -290,6 +290,31 @@ describe('useActionState', () => {
|
|
|
290
290
|
expect(state()).toBe(5);
|
|
291
291
|
});
|
|
292
292
|
});
|
|
293
|
+
describe('useActionState flash from a native form post', () => {
|
|
294
|
+
const flash = (id, result) => btoa(JSON.stringify({ a: id, r: result })).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
|
295
|
+
afterEach(() => {
|
|
296
|
+
document.cookie = 'krate_flash=; Path=/; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT';
|
|
297
|
+
});
|
|
298
|
+
it('starts from the state the server returned to the redirected native post', () => {
|
|
299
|
+
document.cookie = 'krate_flash=' + flash('save', { errors: { name: 'required' } }) + '; Path=/';
|
|
300
|
+
const [state, setState] = createSignal({ errors: {} });
|
|
301
|
+
const [, setPending] = createSignal(false);
|
|
302
|
+
__krateActionState(__krateAction('save'), state, setState, setPending);
|
|
303
|
+
expect(state()).toEqual({ errors: { name: 'required' } });
|
|
304
|
+
// The flash is single use.
|
|
305
|
+
expect(document.cookie).not.toContain('krate_flash=' + flash('save', { errors: { name: 'required' } }));
|
|
306
|
+
const [again, setAgain] = createSignal('init');
|
|
307
|
+
__krateActionState(__krateAction('save'), again, setAgain, setPending);
|
|
308
|
+
expect(again()).toBe('init');
|
|
309
|
+
});
|
|
310
|
+
it('ignores a flash written for another action', () => {
|
|
311
|
+
document.cookie = 'krate_flash=' + flash('other', 'x') + '; Path=/';
|
|
312
|
+
const [state, setState] = createSignal('init');
|
|
313
|
+
const [, setPending] = createSignal(false);
|
|
314
|
+
__krateActionState(__krateAction('save'), state, setState, setPending);
|
|
315
|
+
expect(state()).toBe('init');
|
|
316
|
+
});
|
|
317
|
+
});
|
|
293
318
|
describe('useOptimistic', () => {
|
|
294
319
|
it('applies updates while an action is pending and reverts to the latest base', async () => {
|
|
295
320
|
const [base, setBase] = createSignal(['a']);
|
|
@@ -388,6 +413,20 @@ describe('server action proxies', () => {
|
|
|
388
413
|
await __krateAction('a4')();
|
|
389
414
|
expect(nav).toHaveBeenCalledWith('/next');
|
|
390
415
|
});
|
|
416
|
+
it('keeps the previous useActionState state when the action redirects', async () => {
|
|
417
|
+
vi.stubGlobal('fetch', vi.fn(async () => jsonResponse({ ok: true, result: null, redirect: '/next' })));
|
|
418
|
+
vi.stubGlobal('navigate', vi.fn());
|
|
419
|
+
const proxy = __krateAction('a4b');
|
|
420
|
+
let state = { error: '' };
|
|
421
|
+
const dispatch = __krateActionState((prev, payload) => proxy(prev, payload), () => state, (next) => {
|
|
422
|
+
state = next;
|
|
423
|
+
}, () => { });
|
|
424
|
+
await dispatch.__krateRun(new FormData());
|
|
425
|
+
expect(state).toEqual({ error: '' });
|
|
426
|
+
});
|
|
427
|
+
it('ignores a null element when a form binding is detached', () => {
|
|
428
|
+
expect(() => __krateBindFormAction(null, () => () => { })).not.toThrow();
|
|
429
|
+
});
|
|
391
430
|
it('dispatches krate:revalidate when the action revalidated server caches', async () => {
|
|
392
431
|
const revalidation = { paths: [{ path: '/posts', type: 'page' }], tags: ['posts'] };
|
|
393
432
|
vi.stubGlobal('fetch', vi.fn(async () => jsonResponse({ ok: true, result: 1, revalidated: true, revalidation })));
|
|
@@ -415,3 +454,24 @@ describe('server action proxies', () => {
|
|
|
415
454
|
}
|
|
416
455
|
});
|
|
417
456
|
});
|
|
457
|
+
describe('takeFlash', () => {
|
|
458
|
+
const encode = (payload) => btoa(unescape(encodeURIComponent(JSON.stringify(payload)))).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
|
459
|
+
afterEach(() => {
|
|
460
|
+
document.body.innerHTML = '';
|
|
461
|
+
document.cookie = 'krate_flash=; Path=/; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT';
|
|
462
|
+
});
|
|
463
|
+
it('reads the cookie for the matching action only', () => {
|
|
464
|
+
document.cookie = 'krate_flash=' + encode({ a: 'act1', r: { error: 'bad' } }) + '; Path=/';
|
|
465
|
+
expect(takeFlash('other')).toEqual({ found: false });
|
|
466
|
+
expect(takeFlash('act1')).toEqual({ found: true, value: { error: 'bad' } });
|
|
467
|
+
expect(takeFlash('act1')).toEqual({ found: false });
|
|
468
|
+
});
|
|
469
|
+
it('falls back to the notice the server rendered and removes it', () => {
|
|
470
|
+
document.body.innerHTML =
|
|
471
|
+
'<form><div data-krate-flash="' + encode({ a: 'act2', r: { error: 'server rendered' } }) + '" data-krate-action="act2">server rendered</div></form>';
|
|
472
|
+
expect(takeFlash('act1')).toEqual({ found: false });
|
|
473
|
+
expect(document.querySelector('[data-krate-flash]')).not.toBeNull();
|
|
474
|
+
expect(takeFlash('act2')).toEqual({ found: true, value: { error: 'server rendered' } });
|
|
475
|
+
expect(document.querySelector('[data-krate-flash]')).toBeNull();
|
|
476
|
+
});
|
|
477
|
+
});
|
package/dist/cache-scope.js
CHANGED
|
@@ -301,7 +301,10 @@ export function patchFetch() {
|
|
|
301
301
|
if (res.ok) {
|
|
302
302
|
const bytes = new Uint8Array(await res.clone().arrayBuffer());
|
|
303
303
|
const headers = [];
|
|
304
|
-
res.headers.forEach((v, k) =>
|
|
304
|
+
res.headers.forEach((v, k) => {
|
|
305
|
+
if (k.toLowerCase() !== 'set-cookie')
|
|
306
|
+
headers.push([k, v]);
|
|
307
|
+
});
|
|
305
308
|
const entry = { status: res.status, statusText: res.statusText, headers, body: toBase64(bytes) };
|
|
306
309
|
dataCacheSetJSON(key, JSON.stringify(entry), ttl, tags, epoch);
|
|
307
310
|
}
|
package/dist/cache.test.js
CHANGED
|
@@ -189,6 +189,22 @@ describe('revalidatePath / revalidateTag', () => {
|
|
|
189
189
|
});
|
|
190
190
|
});
|
|
191
191
|
describe('patchFetch', () => {
|
|
192
|
+
it('never replays Set-Cookie from a cached response', async () => {
|
|
193
|
+
const base = vi.fn(async () => new Response('body', {
|
|
194
|
+
status: 200,
|
|
195
|
+
headers: { 'set-cookie': 'sid=secret', 'x-test': 'yes' },
|
|
196
|
+
}));
|
|
197
|
+
vi.stubGlobal('fetch', base);
|
|
198
|
+
cacheRuntime().fetchPatched = false;
|
|
199
|
+
patchFetch();
|
|
200
|
+
const read = () => fetch('http://data/session', { next: { tags: ['session'], revalidate: 60 } });
|
|
201
|
+
const first = await withCacheScope('/p', async () => read());
|
|
202
|
+
expect(first.result.headers.get('x-test')).toBe('yes');
|
|
203
|
+
const second = await withCacheScope('/p', async () => read());
|
|
204
|
+
expect(second.result.headers.get('set-cookie')).toBeNull();
|
|
205
|
+
expect(second.result.headers.get('x-test')).toBe('yes');
|
|
206
|
+
expect(base).toHaveBeenCalledTimes(1);
|
|
207
|
+
});
|
|
192
208
|
it('caches tagged GET responses, records tags, and bypasses no-store', async () => {
|
|
193
209
|
let n = 0;
|
|
194
210
|
const base = vi.fn(async () => new Response(`body-${++n}`, { status: 200, headers: { 'x-n': String(n) } }));
|
package/dist/class-helpers.d.ts
CHANGED
|
@@ -2,10 +2,10 @@ export type ClassValue = string | number | boolean | null | undefined | ClassVal
|
|
|
2
2
|
/** `clsx` - conditional class names from strings, arrays, and objects. */
|
|
3
3
|
export declare function clsx(...inputs: ClassValue[]): string;
|
|
4
4
|
/**
|
|
5
|
-
* Merge conflicting Tailwind utilities
|
|
6
|
-
*
|
|
7
|
-
* `
|
|
8
|
-
* compiler folds literal calls
|
|
5
|
+
* Merge conflicting Tailwind utilities: later classes win over earlier ones in
|
|
6
|
+
* the same utility group and variant chain (`hover:p-2` never conflicts with
|
|
7
|
+
* `p-4`). Classes the helper does not recognise only dedupe on an exact match.
|
|
8
|
+
* The compiler folds literal calls; this covers dynamic joins.
|
|
9
9
|
*/
|
|
10
10
|
export declare function twMerge(...inputs: ClassValue[]): string;
|
|
11
11
|
/** `cn` - `twMerge` over `clsx`, the shadcn/ui convention. */
|