Skip to content

Commit

Permalink
🚀 Fixed issue with redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
abdheshnayak committed Dec 4, 2023
1 parent 3ff94ac commit e2b8692
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,118 +1,105 @@
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(
<RemixServer
context={remixContext}
url={request.url}
abortDelay={ABORT_DELAY}
/>,

{
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,
})
);

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(
<RemixServer
context={remixContext}
url={request.url}
abortDelay={ABORT_DELAY}
/>,

{
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,
})
);

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,
Expand All @@ -126,4 +113,4 @@ export default (
responseHeaders,
remixContext
);
};
}
11 changes: 5 additions & 6 deletions web/lib/app-setup/root.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HeadersFunction, redirect } from '@remix-run/node';
import { HeadersFunction } from '@remix-run/node';
import {
Link,
Links,
Expand Down Expand Up @@ -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 },
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions web/lib/server/helpers/execute-query-with-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,15 @@ export const ExecuteQueryWithContext = (
formatter: formatter<A, B, C>,
def?: any
): IExecutorResp<B, C> {
const apiName = `[API] ${
const apiName = `[#] ${
(q as any)?.definitions[0]?.selectionSet?.selections[0]?.name?.value || ''
}`;

const res: IExecutorResp<B, C> = async (variables) => {
const { transformer } = formatter;

console.time(apiName);

try {
console.time(apiName);
const defCookie =
headers.get('klsession') || headers.get('cookie') || null;

Expand Down

0 comments on commit e2b8692

Please sign in to comment.