From 24e2b231fa5a557f9b2c78261ce5d11ba8821d41 Mon Sep 17 00:00:00 2001 From: Abdhesh Nayak Date: Mon, 4 Dec 2023 16:34:55 +0530 Subject: [PATCH] :rocket: Fixed issue with redirect --- .../{entry.server.jsx => entry.server.tsx} | 87 ++++++++----------- lib/app-setup/root.tsx | 11 ++- .../helpers/execute-query-with-context.ts | 5 +- 3 files changed, 44 insertions(+), 59 deletions(-) rename lib/app-setup/{entry.server.jsx => entry.server.tsx} (53%) diff --git a/lib/app-setup/entry.server.jsx b/lib/app-setup/entry.server.tsx similarity index 53% rename from lib/app-setup/entry.server.jsx rename to lib/app-setup/entry.server.tsx index 5c17749b6..d99d678c0 100644 --- a/lib/app-setup/entry.server.jsx +++ b/lib/app-setup/entry.server.tsx @@ -1,36 +1,35 @@ -import { PassThrough } from 'stream'; +/* eslint-disable no-param-reassign */ +import { PassThrough } from 'node:stream'; -import { Response } from '@remix-run/node'; +import type { EntryContext } from '@remix-run/node'; +import { createReadableStreamFromReadable } from '@remix-run/node'; import { RemixServer } from '@remix-run/react'; import isbot from 'isbot'; import { renderToPipeableStream } from 'react-dom/server'; const ABORT_DELAY = 5_000; -const handleBotRequest = ( - request, - responseStatusCode, - responseHeaders, - remixContext -) => - new Promise((resolve, reject) => { - let shellRendered = false; +function handleBotRequest( + request: Request, + responseStatusCode: number, + responseHeaders: Headers, + remixContext: EntryContext +) { + return new Promise((resolve, reject) => { const { pipe, abort } = renderToPipeableStream( , - { onAllReady() { - shellRendered = true; const body = new PassThrough(); responseHeaders.set('Content-Type', 'text/html'); resolve( - new Response(body, { + new Response(createReadableStreamFromReadable(body), { headers: responseHeaders, status: responseStatusCode, }) @@ -38,48 +37,41 @@ const handleBotRequest = ( pipe(body); }, - onShellError(error) { + onShellError(error: unknown) { reject(error); }, - onError(error) { - // eslint-disable-next-line no-param-reassign + 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); - } + console.error(error); }, } ); setTimeout(abort, ABORT_DELAY); }); +} -const handleBrowserRequest = ( - request, - responseStatusCode, - responseHeaders, - remixContext -) => - new Promise((resolve, reject) => { - let shellRendered = false; +function handleBrowserRequest( + request: Request, + responseStatusCode: number, + responseHeaders: Headers, + remixContext: EntryContext +) { + return new Promise((resolve, reject) => { const { pipe, abort } = renderToPipeableStream( , - { onShellReady() { - shellRendered = true; const body = new PassThrough(); + responseHeaders.set('Content-Type', 'text/html'); resolve( - new Response(body, { + new Response(createReadableStreamFromReadable(body), { headers: responseHeaders, status: responseStatusCode, }) @@ -87,32 +79,27 @@ const handleBrowserRequest = ( pipe(body); }, - onShellError(error) { + onShellError(error: unknown) { reject(error); }, - onError(error) { - // eslint-disable-next-line no-param-reassign + onError(error: unknown) { + console.error(error); 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); - } }, } ); setTimeout(abort, ABORT_DELAY); }); +} -export default ( - request, - responseStatusCode, - responseHeaders, - remixContext, - loadContext -) => { +export default function handleRequest( + request: Request, + responseStatusCode: number, + responseHeaders: Headers, + remixContext: EntryContext + // loadContext: AppLoadContext +) { return isbot(request.headers.get('user-agent')) ? handleBotRequest( request, @@ -126,4 +113,4 @@ export default ( responseHeaders, remixContext ); -}; +} diff --git a/lib/app-setup/root.tsx b/lib/app-setup/root.tsx index 4a44c5036..423888f2a 100644 --- a/lib/app-setup/root.tsx +++ b/lib/app-setup/root.tsx @@ -1,4 +1,4 @@ -import { HeadersFunction, redirect } from '@remix-run/node'; +import { HeadersFunction } from '@remix-run/node'; import { Link, Links, @@ -29,7 +29,6 @@ import { TopBar } from '~/components/organisms/top-bar'; import styleZenerSelect from '@oshq/react-select/index.css'; import stylesUrl from '~/design-system/index.css'; import rcss from 'react-highlightjs-logs/dist/index.css'; -import { IRemixCtx } from '../types/common'; export const links = () => [ { rel: 'stylesheet', href: stylesUrl }, @@ -206,10 +205,10 @@ ${URL_SUFFIX ? `window.URL_SUFFIX = ${`'${URL_SUFFIX}'`}` : ''} ); }; -export const loader = (ctx: IRemixCtx) => { - if (ctx?.request?.headers?.get('referer')) { - return redirect(ctx.request.url); - } +export const loader = () => { + // if (ctx?.request?.headers?.get('referer')) { + // return redirect(ctx.request.url); + // } const nodeEnv = process.env.NODE_ENV; return { diff --git a/lib/server/helpers/execute-query-with-context.ts b/lib/server/helpers/execute-query-with-context.ts index d4b54d55b..3f57ef066 100644 --- a/lib/server/helpers/execute-query-with-context.ts +++ b/lib/server/helpers/execute-query-with-context.ts @@ -41,16 +41,15 @@ export const ExecuteQueryWithContext = ( formatter: formatter, def?: any ): IExecutorResp { - const apiName = `[API] ${ + const apiName = `[#] ${ (q as any)?.definitions[0]?.selectionSet?.selections[0]?.name?.value || '' }`; const res: IExecutorResp = async (variables) => { const { transformer } = formatter; - console.time(apiName); - try { + console.time(apiName); const defCookie = headers.get('klsession') || headers.get('cookie') || null;