Skip to content

Commit

Permalink
fix duplicate middleware calls
Browse files Browse the repository at this point in the history
  • Loading branch information
edivados committed Sep 4, 2023
1 parent f351f42 commit 468ba05
Showing 1 changed file with 112 additions and 126 deletions.
238 changes: 112 additions & 126 deletions packages/start/server/render.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { JSX } from "solid-js";
import { renderToStream, renderToString, renderToStringAsync } from "solid-js/web";
import { apiRoutes } from "../api/middleware";
import { inlineServerFunctions } from "../server/middleware";
import { redirect } from "../server/responses";
import { FetchEvent, FETCH_EVENT, PageEvent } from "../server/types";
import { FETCH_EVENT, FetchEvent, PageEvent } from "../server/types";

export function renderSync(
fn: (context: PageEvent) => JSX.Element,
Expand All @@ -12,37 +10,33 @@ export function renderSync(
renderId?: string;
}
) {
return () => apiRoutes({
forward: inlineServerFunctions({
async forward(event: FetchEvent): Promise<Response> {
if (
!import.meta.env.DEV &&
!import.meta.env.START_SSR &&
!import.meta.env.START_INDEX_HTML
) {
return (
(await event.env.getStaticHTML?.("/index")) ?? new Response("Not Found", { status: 404 })
);
}

let pageEvent = createPageEvent(event);

let markup = renderToString(() => fn(pageEvent), options);
if (pageEvent.routerContext && pageEvent.routerContext.url) {
return redirect(pageEvent.routerContext.url, {
headers: pageEvent.responseHeaders
});
}

markup = handleIslandsRouting(pageEvent, markup);

return new Response(markup, {
status: pageEvent.getStatusCode(),
headers: pageEvent.responseHeaders
});
}
})
});
return () => async (event: FetchEvent) => {
if (
!import.meta.env.DEV &&
!import.meta.env.START_SSR &&
!import.meta.env.START_INDEX_HTML
) {
return (
(await event.env.getStaticHTML?.("/index")) ?? new Response("Not Found", { status: 404 })
);
}

let pageEvent = createPageEvent(event);

let markup = renderToString(() => fn(pageEvent), options);
if (pageEvent.routerContext && pageEvent.routerContext.url) {
return redirect(pageEvent.routerContext.url, {
headers: pageEvent.responseHeaders
});
}

markup = handleIslandsRouting(pageEvent, markup);

return new Response(markup, {
status: pageEvent.getStatusCode(),
headers: pageEvent.responseHeaders
});
}
}

export function renderAsync(
Expand All @@ -53,38 +47,34 @@ export function renderAsync(
renderId?: string;
}
) {
return () => apiRoutes({
forward: inlineServerFunctions({
async forward(event: FetchEvent): Promise<Response> {
if (
!import.meta.env.DEV &&
!import.meta.env.START_SSR &&
!import.meta.env.START_INDEX_HTML
) {
return (
(await event.env.getStaticHTML?.("/index")) ?? new Response("Not Found", { status: 404 })
);
}

let pageEvent = createPageEvent(event);

let markup = await renderToStringAsync(() => fn(pageEvent), options);

if (pageEvent.routerContext && pageEvent.routerContext.url) {
return redirect(pageEvent.routerContext.url, {
headers: pageEvent.responseHeaders
}) as Response;
}

markup = handleIslandsRouting(pageEvent, markup);

return new Response(markup, {
status: pageEvent.getStatusCode(),
headers: pageEvent.responseHeaders
});
}
})
});
return () => async (event: FetchEvent) => {
if (
!import.meta.env.DEV &&
!import.meta.env.START_SSR &&
!import.meta.env.START_INDEX_HTML
) {
return (
(await event.env.getStaticHTML?.("/index")) ?? new Response("Not Found", { status: 404 })
);
}

let pageEvent = createPageEvent(event);

let markup = await renderToStringAsync(() => fn(pageEvent), options);

if (pageEvent.routerContext && pageEvent.routerContext.url) {
return redirect(pageEvent.routerContext.url, {
headers: pageEvent.responseHeaders
}) as Response;
}

markup = handleIslandsRouting(pageEvent, markup);

return new Response(markup, {
status: pageEvent.getStatusCode(),
headers: pageEvent.responseHeaders
});
}
}

export function renderStream(
Expand All @@ -96,68 +86,64 @@ export function renderStream(
onCompleteAll?: (info: { write: (v: string) => void }) => void;
} = {}
) {
return () => apiRoutes({
forward: inlineServerFunctions({
async forward(event: FetchEvent): Promise<Response> {
if (
!import.meta.env.DEV &&
!import.meta.env.START_SSR &&
!import.meta.env.START_INDEX_HTML
) {
return (
(await event.env.getStaticHTML?.("/index")) ?? new Response("Not Found", { status: 404 })
);
}

let pageEvent = createPageEvent(event);

// Hijack after navigation with islands router to be async
// Todo streaming into HTML
if (import.meta.env.START_ISLANDS_ROUTER && event.request.headers.get("x-solid-referrer")) {
let markup = await renderToStringAsync(() => fn(pageEvent), baseOptions);

if (pageEvent.routerContext && pageEvent.routerContext.url) {
return redirect(pageEvent.routerContext.url, {
headers: pageEvent.responseHeaders
}) as Response;
}

markup = handleIslandsRouting(pageEvent, markup);

return new Response(markup, {
status: pageEvent.getStatusCode(),
headers: pageEvent.responseHeaders
});
}

const options = { ...baseOptions };
if (options.onCompleteAll) {
const og = options.onCompleteAll;
options.onCompleteAll = options => {
handleStreamingRedirect(pageEvent)(options);
og(options);
};
} else options.onCompleteAll = handleStreamingRedirect(pageEvent);
const { readable, writable } = new TransformStream();
const stream = renderToStream(() => fn(pageEvent), options);

if (pageEvent.routerContext && pageEvent.routerContext.url) {
return redirect(pageEvent.routerContext.url, {
headers: pageEvent.responseHeaders
});
}

handleStreamingIslandsRouting(pageEvent, writable);

stream.pipeTo(writable);

return new Response(readable, {
status: pageEvent.getStatusCode(),
return () => async (event: FetchEvent) => {
if (
!import.meta.env.DEV &&
!import.meta.env.START_SSR &&
!import.meta.env.START_INDEX_HTML
) {
return (
(await event.env.getStaticHTML?.("/index")) ?? new Response("Not Found", { status: 404 })
);
}

let pageEvent = createPageEvent(event);

// Hijack after navigation with islands router to be async
// Todo streaming into HTML
if (import.meta.env.START_ISLANDS_ROUTER && event.request.headers.get("x-solid-referrer")) {
let markup = await renderToStringAsync(() => fn(pageEvent), baseOptions);

if (pageEvent.routerContext && pageEvent.routerContext.url) {
return redirect(pageEvent.routerContext.url, {
headers: pageEvent.responseHeaders
});
}) as Response;
}
})
});

markup = handleIslandsRouting(pageEvent, markup);

return new Response(markup, {
status: pageEvent.getStatusCode(),
headers: pageEvent.responseHeaders
});
}

const options = { ...baseOptions };
if (options.onCompleteAll) {
const og = options.onCompleteAll;
options.onCompleteAll = options => {
handleStreamingRedirect(pageEvent)(options);
og(options);
};
} else options.onCompleteAll = handleStreamingRedirect(pageEvent);
const { readable, writable } = new TransformStream();
const stream = renderToStream(() => fn(pageEvent), options);

if (pageEvent.routerContext && pageEvent.routerContext.url) {
return redirect(pageEvent.routerContext.url, {
headers: pageEvent.responseHeaders
});
}

handleStreamingIslandsRouting(pageEvent, writable);

stream.pipeTo(writable);

return new Response(readable, {
status: pageEvent.getStatusCode(),
headers: pageEvent.responseHeaders
});
}
}

function handleStreamingIslandsRouting(pageEvent: PageEvent, writable: WritableStream<any>) {
Expand Down

0 comments on commit 468ba05

Please sign in to comment.