Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/no-js-flash-inline.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/router": patch
---

Render a no-JS form post's outcome inline (#724). The page a no-JS submission redirects back to decodes the flash cookie asynchronously; that read suspended under the app's `<Loading>`, so the shell flushed the fallback and the form and its error arrived only as a streamed `<template>` a browser without JavaScript never swaps in. The router now reads submissions at its root on the server, so the decode holds the shell instead. No client bundle change.
9 changes: 9 additions & 0 deletions src/routers/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ export function Root(props: {
preload?: RoutePreloadFunc;
children: JSX.Element;
}) {
// A no-JS form post lands back here on a GET carrying the flash cookie,
// and that browser cannot swap streamed <template> chunks in. Read the
// submissions above the app's root (and so above any of its <Loading>
// boundaries) so the flash decode suspends this root slot, which holds the
// shell, instead of a boundary that would flush its fallback (#724). Runs
// before anything is created so the retry starts clean; a request with no
// flash cookie seeds [] without suspending. Server-only: folds out of
// client bundles.
isServer && props.routerState.submissions[0]();
const location = props.routerState.location;
const params = props.routerState.params;
const data = createMemo(
Expand Down
3 changes: 2 additions & 1 deletion src/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,8 @@ export function createRouterContext(
// malformed-cookie semantics. Created only when a flash cookie actually
// arrived (server-only by construction: `flash` is only ever set there), and
// - `lazy`: server memos compute eagerly by default — deferred to first
// read, a request whose submissions are never read never decodes;
// read, which `Root` makes on every server render (#724), so the
// decode suspends the root slot rather than an app boundary;
// - `transparent`: the memo exists on the server only, so its owner
// must not consume a hydration-id slot — the client, which seeds
// submissions as [] without ever creating this memo, would miss it
Expand Down
93 changes: 93 additions & 0 deletions test/server/flash-no-js-inline.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// #724: a no-JS round trip lands on a GET carrying the flash cookie. The
// browser has no runtime to swap out-of-order <template> chunks in, so the
// flash outcome must render inline in the shell — the decode (WebCrypto,
// async) holds the shell instead of suspending the route under a fallback.
import { For, Loading, createMemo } from "solid-js";
import { createRequestEvent, renderToStream } from "@solidjs/web";
import { provideRequestEvent } from "@solidjs/web/storage";
import { decodeFlashCookie, encodeFlashCookie } from "@solidjs/web/server-functions/server";
import { formAction, useSubmissions } from "../../src/data/action.js";
import { provideFlashDecoder } from "../../src/routing.js";
import { createRouter, query } from "../../src/index.js";

const getTodos = query(async () => ["first"], "todos-inline");

(globalThis as any).__SOLID_SECRET__ = "flash-no-js-inline-spec-secret";

const save = formAction(
Object.assign(
async () => {
throw new Error("unused");
},
{ url: "/_server/save-inline" }
)
);

const delay = (ms: number) => new Promise(r => setTimeout(r, ms));

// The decode is WebCrypto: its latency is the platform's, not a microtask.
// A few timer turns is enough to outlast the stream's shell drain.
provideFlashDecoder(async cookieHeader => {
await delay(30);
return decodeFlashCookie(cookieHeader);
});

describe("no-JS flash outcome renders inline (#724)", () => {
test("a submissions read under the root Loading is in the shell, not a streamed template", async () => {
function Page() {
const subs = useSubmissions(save);
const list = createMemo(() => getTodos());
return (
<main>
<ul>
<For each={list()}>{t => <li>{t}</li>}</For>
</ul>
<form method="post" action={save}>
<input name="title" />
</form>
<p class="err">{(subs.at(-1)?.error as any)?.issues?.title}</p>
</main>
);
}
const Router = createRouter({ routes: [{ path: "/", component: Page }] });

const cookie = (await encodeFlashCookie(
"/_server/save-inline",
{ issues: { title: "Too short" } },
[],
true
))!;
const event = createRequestEvent(
new Request("http://localhost/", { headers: { cookie: cookie.split(";")[0] } })
);
// pipe, not `.then(String)`: awaiting the whole stream renders every
// boundary inline, which hides exactly what a no-JS browser is sent first
const shell = await provideRequestEvent(
event,
() =>
new Promise<string>(resolve => {
renderToStream(() => (
<html>
<head />
<body>
<Router>
{props => (
<Loading fallback={<p class="fallback">loading</p>}>{props.children}</Loading>
)}
</Router>
</body>
</html>
)).pipe({
write(chunk: string) {
resolve(String(chunk));
},
end() {}
});
})
);
const html = shell;
expect(html).not.toContain('class="fallback"');
expect(html).toContain("<form");
expect(html).toContain('class="err">Too short</p>');
});
});
Loading