Skip to content

Commit

Permalink
remove server context
Browse files Browse the repository at this point in the history
  • Loading branch information
ryansolid committed Nov 7, 2023
1 parent 76859a0 commit ddad4e1
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 110 deletions.
18 changes: 5 additions & 13 deletions packages/start-new/client/StartClient.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
// @ts-ignore
import App from "#start/app";
import { Router } from "@solidjs/router";
import { PageEvent } from "../server/types";

import { createRoutes } from "../shared/FileRoutes";
import { ServerContext } from "../shared/ServerContext";
import "./mount";

const routes = createRoutes();

function Dummy(props) {
return props.children;
}

export function StartClient() {
return (
<ServerContext.Provider value={{ routes } as unknown as PageEvent}>
<Router>
<Router>
<Dummy>
<Dummy>
<Dummy>
<App />
</Dummy>
<App />
</Dummy>
</Router>
</ServerContext.Provider>
</Dummy>
</Router>
);
}
70 changes: 33 additions & 37 deletions packages/start-new/server/StartServer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,46 @@ import { join } from "path";
import { Hydration, HydrationScript, NoHydration, ssr } from "solid-js/web";
import { renderAsset } from "./renderAsset";

import { ServerContext } from "../shared/ServerContext";

const docType = ssr("<!DOCTYPE html>");

export function StartServer(props) {
const context = props.context;
const parsed = new URL(context.request.url);
const path = parsed.pathname + parsed.search;
return (
<ServerContext.Provider value={context}>
<Router
out={context.routerContext}
url={join(import.meta.env.BASE_URL, path)}
base={import.meta.env.BASE_URL}
>
<NoHydration>
{docType as unknown as any}
<props.document
assets={<>{context.assets.map(m => renderAsset(m))}</>}
scripts={
<>
<HydrationScript />
<script innerHTML={`window.manifest = ${JSON.stringify(context.manifest)}`} />
<script
type="module"
src={
import.meta.env.MANIFEST["client"].inputs[
import.meta.env.MANIFEST["client"].handler
].output.path
}
/>
</>
}
>
{!import.meta.env.START_ISLANDS ? (
<Hydration>
<App />
</Hydration>
) : (
<Router
out={context.routerContext}
url={join(import.meta.env.BASE_URL, path)}
base={import.meta.env.BASE_URL}
>
<NoHydration>
{docType as unknown as any}
<props.document
assets={<>{context.assets.map(m => renderAsset(m))}</>}
scripts={
<>
<HydrationScript />
<script innerHTML={`window.manifest = ${JSON.stringify(context.manifest)}`} />
<script
type="module"
src={
import.meta.env.MANIFEST["client"].inputs[
import.meta.env.MANIFEST["client"].handler
].output.path
}
/>
</>
}
>
{!import.meta.env.START_ISLANDS ? (
<Hydration>
<App />
)}
</props.document>
</NoHydration>
</Router>
</ServerContext.Provider>
</Hydration>
) : (
<App />
)}
</props.document>
</NoHydration>
</Router>
);
}
5 changes: 2 additions & 3 deletions packages/start-new/server/handler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ export async function createPageEvent(ctx: FetchEvent) {
const serverManifest = import.meta.env.MANIFEST["ssr"];
const prevPath = ctx.request.headers.get("x-solid-referrer");
const mutation = ctx.request.headers.get("x-solid-mutation") === "true";
const pageEvent: PageEvent = {
...ctx,
const pageEvent: PageEvent = Object.assign(ctx, {
manifest: await clientManifest.json(),
assets: [
...(await clientManifest.inputs[clientManifest.handler].assets()),
Expand All @@ -69,7 +68,7 @@ export async function createPageEvent(ctx: FetchEvent) {
tags: [],
$type: FETCH_EVENT,
$islands: new Set<string>()
};
});

return pageEvent;
}
6 changes: 2 additions & 4 deletions packages/start-new/server/islands/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Component, ComponentProps, lazy, sharedConfig } from "solid-js";
import { Hydration, NoHydration } from "solid-js/web";
// import { useRequest } from "../server/ServerContext";
import { getRequestEvent, Hydration, NoHydration } from "solid-js/web";
// import { IslandManifest } from "./types";
import { useRequest } from "../../shared/ServerContext";
import { splitProps } from "./utils";

declare module "solid-js" {
Expand Down Expand Up @@ -47,7 +45,7 @@ export function createIslandReference<T extends Component<any>>(

return ((compProps: ComponentProps<T>) => {
if (import.meta.env.SSR) {
const context = useRequest();
const context = getRequestEvent();
const [, props] = splitProps(compProps, ["children"] as any);
const [, spreadProps] = splitProps(compProps, [] as any);

Expand Down
54 changes: 25 additions & 29 deletions packages/start-new/server/spa/StartServer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,37 @@ import { join } from "path";
import { NoHydration, ssr } from "solid-js/web";
import { renderAsset } from "../renderAsset";

import { ServerContext } from "../../shared/ServerContext";

const docType = ssr("<!DOCTYPE html>");

export function StartServer(props) {
const context = props.context;
const parsed = new URL(context.request.url);
const path = parsed.pathname + parsed.search;
return (
<ServerContext.Provider value={context}>
<Router
out={context.routerContext}
url={join(import.meta.env.BASE_URL, path)}
base={import.meta.env.BASE_URL}
>
<NoHydration>
{docType as unknown as any}
<props.document
assets={<>{context.assets.map(m => renderAsset(m))}</>}
scripts={
<>
<script innerHTML={`window.manifest = ${JSON.stringify(context.manifest)}`} />
<script
type="module"
src={
import.meta.env.MANIFEST["client"].inputs[
import.meta.env.MANIFEST["client"].handler
].output.path
}
/>
</>
}
></props.document>
</NoHydration>
</Router>
</ServerContext.Provider>
<Router
out={context.routerContext}
url={join(import.meta.env.BASE_URL, path)}
base={import.meta.env.BASE_URL}
>
<NoHydration>
{docType as unknown as any}
<props.document
assets={<>{context.assets.map(m => renderAsset(m))}</>}
scripts={
<>
<script innerHTML={`window.manifest = ${JSON.stringify(context.manifest)}`} />
<script
type="module"
src={
import.meta.env.MANIFEST["client"].inputs[
import.meta.env.MANIFEST["client"].handler
].output.path
}
/>
</>
}
></props.document>
</NoHydration>
</Router>
);
}
7 changes: 3 additions & 4 deletions packages/start-new/shared/FileRoutes.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { useContext } from "solid-js";
import { getRequestEvent, isServer } from "solid-js/web";
import lazyRoute from "./lazyRoute";

import { pageRoutes as routeConfigs } from "./routes";
import { ServerContext } from "./ServerContext";

export function createRoutes() {
function createRoute(route) {
Expand All @@ -23,7 +22,7 @@ export function createRoutes() {
return routes;
}

let routes;
export const FileRoutes = () => {
const context = useContext(ServerContext);
return context.routes as any;
return isServer ? getRequestEvent().routes : (routes || (routes = createRoutes()));
};
5 changes: 2 additions & 3 deletions packages/start-new/shared/HttpHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { onCleanup } from "solid-js";
import { isServer } from "solid-js/web";
import { useRequest } from "./ServerContext";
import { getRequestEvent, isServer } from "solid-js/web";

export function HttpHeader(props: { name: string; value: string; append?: boolean }) {
if (isServer) {
const pageContext = useRequest();
const pageContext = getRequestEvent();
if (props.append) {
pageContext!.appendResponseHeader(props.name, props.value);
} else {
Expand Down
5 changes: 2 additions & 3 deletions packages/start-new/shared/HttpStatusCode.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { onCleanup } from "solid-js";
import { isServer } from "solid-js/web";
import { useRequest } from "./ServerContext";
import { getRequestEvent, isServer } from "solid-js/web";

export function HttpStatusCode(props: { code: number, text?: string }) {
if (isServer) {
const context = useRequest();
const context = getRequestEvent();
context!.setResponseStatus(props.code, props.text);
onCleanup(() => {
context!.setResponseStatus(200);
Expand Down
13 changes: 0 additions & 13 deletions packages/start-new/shared/ServerContext.tsx

This file was deleted.

1 change: 0 additions & 1 deletion packages/start-new/shared/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ export { ErrorBoundary as DefaultErrorBoundary } from "./ErrorBoundary";
export { FileRoutes } from "./FileRoutes";
export { HttpHeader } from "./HttpHeader";
export { HttpStatusCode } from "./HttpStatusCode";
export { useRequest } from "./ServerContext";

0 comments on commit ddad4e1

Please sign in to comment.