Files
zooqueen ad3c58d9a0 fix: confirm a saved profile, decide the upload store once, agree on ids
Three unrelated defects, one image.

The profile toast fired only after a session refresh. That refresh is a second
read: it can stall, and when it answers unauthenticated it clears the session,
which throws in useSession and takes down the subtree holding the toaster. A
save that had already persisted could therefore go unconfirmed. The save is
confirmed on the save now and the refresh follows it, so the confirmation
depends only on the thing it reports.

Document bytes are routed by NEXT_PUBLIC_UPLOAD_TRANSPORT. Three places read it
with three slightly different rules, and the two that chose a store fell through
to the database for any value they did not recognise -- so a transport named
under a variable nothing reads stored every PDF inline while the configuration
read as object storage, with nothing logged either way. One function resolves it
now: unset means the database, and an unrecognised value throws rather than
quietly picking a store. Reads still dispatch on each row's own type, so rows
written under either transport keep working.

entry.client and entry.server built different root trees -- StrictMode on the
client only, and a second child beside the router. useId numbers a component by
its position, and a parent's child count is part of that position, so every id
below the root diverged and hydration failed on the first component to ask for
one. Measured on the documents page: the header's menu trigger came back
radix-:R26j5: from the server and radix-:R8qcl: from the client. The trees are
identical now and all seven server-rendered ids survive hydration unchanged.

Also: .gitignore still named the license artifact by its pre-rebrand filename,
so the file the app actually writes was landing in git status untracked.
2026-08-14 09:24:44 -07:00

90 lines
3.2 KiB
TypeScript

import { StrictMode } from 'react';
import { i18n } from '@lingui/core';
import { I18nProvider } from '@lingui/react';
import { createReadableStreamFromReadable } from '@react-router/node';
import { isbot } from 'isbot';
import { PassThrough } from 'node:stream';
import type { RenderToPipeableStreamOptions } from 'react-dom/server';
import { renderToPipeableStream } from 'react-dom/server';
import type { AppLoadContext, EntryContext } from 'react-router';
import { ServerRouter } from 'react-router';
import { APP_I18N_OPTIONS } from '@hanzo/esign-lib/constants/i18n';
import { dynamicActivate, extractLocaleData } from '@hanzo/esign-lib/utils/i18n';
import { langCookie } from './storage/lang-cookie.server';
export const streamTimeout = 5_000;
export default async function handleRequest(
request: Request,
responseStatusCode: number,
responseHeaders: Headers,
routerContext: EntryContext,
_loadContext: AppLoadContext,
) {
let language = await langCookie.parse(request.headers.get('cookie') ?? '');
if (!APP_I18N_OPTIONS.supportedLangs.includes(language)) {
language = extractLocaleData({ headers: request.headers }).lang;
}
await dynamicActivate(language);
return new Promise((resolve, reject) => {
let shellRendered = false;
const userAgent = request.headers.get('user-agent');
// Ensure requests from bots and SPA Mode renders wait for all content to load before responding
// https://react.dev/reference/react-dom/server/renderToPipeableStream#waiting-for-all-content-to-load-for-crawlers-and-static-generation
const readyOption: keyof RenderToPipeableStreamOptions =
(userAgent && isbot(userAgent)) || routerContext.isSpaMode ? 'onAllReady' : 'onShellReady';
const { pipe, abort } = renderToPipeableStream(
// Shaped exactly like entry.client's tree, StrictMode included — see the
// note there. StrictMode does nothing on the server beyond occupying the
// same slot the client gives it.
<StrictMode>
<I18nProvider i18n={i18n}>
<ServerRouter context={routerContext} url={request.url} />
</I18nProvider>
</StrictMode>,
{
[readyOption]() {
shellRendered = true;
const body = new PassThrough();
const stream = createReadableStreamFromReadable(body);
responseHeaders.set('Content-Type', 'text/html');
resolve(
new Response(stream, {
headers: responseHeaders,
status: responseStatusCode,
}),
);
pipe(body);
},
onShellError(error: unknown) {
reject(error);
},
onError(error: unknown) {
responseStatusCode = 500;
// Log streaming rendering errors from inside the shell. Don't log
// errors encountered during initial shell rendering since they'll
// reject and get logged in handleDocumentRequest.
if (shellRendered) {
console.error(error);
}
},
},
);
// Abort the rendering stream after the `streamTimeout` so it has time to
// flush down the rejected boundaries
setTimeout(abort, streamTimeout + 1000);
});
}