Skip to content

Commit

Permalink
better csr fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ryansolid committed Feb 3, 2023
1 parent ae36320 commit 0a27fc2
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 86 deletions.
4 changes: 3 additions & 1 deletion docs/api/createServerAction.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,6 @@ We return a `redirect` to tell the browser where to go upon successful submissio

## Reference

Refer to [createRouteAction](./createRouteAction) for API reference.
Refer to [createRouteAction](./createRouteAction) for API reference.

*Note: createServerAction$ does not work when application is configured to `ssr: false`*
2 changes: 2 additions & 0 deletions docs/api/createServerData.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,5 @@ export function routeData() {

Refer to [createRouteData](./createRouteData) for API reference.

*Note: `createServerData$` does not work when application is configured to `ssr: false`*

1 change: 1 addition & 0 deletions docs/api/createServerMultiAction.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ const [acting, act] = createServerMultiAction$(async (args) => {

Refer to [createRouteMultiAction](./createRouteMultiAction) for API reference.

*Note: `createServerMultiAction$` does not work when application is configured to `ssr: false`*
4 changes: 3 additions & 1 deletion docs/api/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const serverFunction1 = server$.createFetcher('/Log.tsx/logHello')

function Component() {
const logHello = serverFunction1;

logHello('Hello')
}
```
Expand All @@ -75,3 +75,5 @@ function Component() {
logHello('Hello')
}
```

*Note server$ do not work when application is configured to `ssr: false`*
2 changes: 1 addition & 1 deletion docs/api/vite.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ The vite plugin exposes the following options:
- `serverEntry` (_string_, default `"./entry-server.tsx"`): the file path where to the server entry.
- `prerenderRoutes` (_string[]_, default `[]`): list of route paths to prerender (currently only works with static adapter).
- `inspect` (_boolean_, default `true`): turns on whether vite inspect plugin is enabled.
- `ssr` (_boolean_, default `true`): toggles between client rendering and server rendering (ssr) mode.
- `ssr` (_boolean_, default `true`): toggles between client rendering and server rendering (ssr) mode. `server$` functions do not work with `ssr: false`.
- `islands` (_boolean_, default `false`): _experimental_ toggles on "islands" mode.
- `islandsRouter` (_boolean_, default `false`): _experimental_ toggles on hybrid "islands" routing.
File renamed without changes.
5 changes: 2 additions & 3 deletions packages/start/entry-server/StartServer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import { ssr } from "solid-js/web";
import Root from "~start/root";

import { RouteDefinition, Router as IslandsRouter } from "../islands/server-router";
import { fileRoutes } from "../root/FileRoutes";

import { fileRoutes } from "../root/FileRoutes";
import { ServerContext } from "../server/ServerContext";
import { FetchEvent, PageEvent } from "../server/types";
import DevRoot from "./DevRoot";

const rootData = Object.values(import.meta.glob("/src/root.data.(js|ts)", { eager: true }))[0] as {
default: RouteDataFunc;
Expand Down Expand Up @@ -83,7 +82,7 @@ export default function StartServer({ event }: { event: PageEvent }) {
return (
<ServerContext.Provider value={event}>
{devNoSSR ? (
<DevRoot />
<Root />
) : (
<MetaProvider tags={event.tags as ComponentProps<typeof MetaProvider>["tags"]}>
<StartRouter
Expand Down
81 changes: 28 additions & 53 deletions packages/start/vite/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,6 @@ function solidStartInlineServerModules(options) {
};
}

// import micromatch from "micromatch";
/**
* @param {fs.PathLike} path
*/
function touch(path) {
const time = new Date();
try {
fs.utimesSync(path, time, time);
} catch (err) {
fs.closeSync(fs.openSync(path, "w"));
}
}

/**
* @param {any} arr
*/
Expand Down Expand Up @@ -352,11 +339,8 @@ function solidStartFileSystemRouter(options) {
return {
code: code.replace(
"var fileRoutes = $FILE_ROUTES;",
!options.ssr && ssr ? "var fileRoutes = [];" :
stringifyPageRoutes(router.getNestedPageRoutes(), {
// if we are in SPA mode, and building the server bundle, we import
// the routes eagerly so that they can dead-code eliminate properly,
// for some reason, vite doesn't do it properly when the routes are
// loaded lazily.
lazy: ssr ? false : true
})
)
Expand Down Expand Up @@ -400,6 +384,31 @@ function solidsStartRouteManifest(options) {
};
}

/**
* @returns {import('vite').Plugin}
* @param {any} options
*/
function solidStartCsrDev(options) {
let csrDev = false;
return {
name: "solid-start-csr-dev",
async configResolved(config) {
csrDev = config.command === "serve" && options.ssr !== true;
},
async transform(code, id, transformOptions) {
const isSsr = transformOptions === null || transformOptions === void 0 ? void 0 : transformOptions.ssr;
if (isSsr && csrDev && code.includes("~start/root")) {
return {
code: code.replace(
"~start/root",
join(_dirname, "..", "dev", "CsrRoot.tsx")
)
};
}
}
};
}

async function resolveAdapter(config) {
if (typeof config.solidOptions.adapter === "string") {
return (await import(config.solidOptions.adapter)).default();
Expand Down Expand Up @@ -527,6 +536,7 @@ function expand(target, source = {}, parse = v => v) {
}
}


/**
* @returns {import('vite').Plugin}
* @param {any} options
Expand Down Expand Up @@ -640,42 +650,6 @@ function solidStartConfig(options) {
};
}

/**
* @param {string} locate
* @param {string} [cwd]
* @returns {string | undefined}
*/
function find(locate, cwd) {
cwd = cwd || process.cwd();
if (cwd.split(path.sep).length < 2) return undefined;
const found = fs.readdirSync(cwd).some(f => f === locate);
if (found) return path.join(cwd, locate);
return find(locate, path.join(cwd, ".."));
}

// const nodeModulesPath = find("node_modules", process.cwd());

// function detectAdapter() {
// let adapters = [];
// fs.readdirSync(nodeModulesPath).forEach(dir => {
// if (dir.startsWith("solid-start-")) {
// const pkg = JSON.parse(
// fs.readFileSync(path.join(nodeModulesPath, dir, "package.json"), {
// encoding: "utf8"
// })
// );
// if (pkg.solid && pkg.solid.type === "adapter") {
// adapters.push(dir);
// }
// }
// });

// // Ignore the default adapter.
// adapters = adapters.filter(adapter => adapter !== "solid-start-node");

// return adapters.length > 0 ? adapters[0] : "solid-start-node";
// }

const findAny = (path, name, exts = [".js", ".ts", ".jsx", ".tsx", ".mjs", ".mts"]) => {
for (var ext of exts) {
const file = join(path, name + ext);
Expand Down Expand Up @@ -737,6 +711,7 @@ export default function solidStart(options) {
return [
solidStartConfig(options),
solidStartFileSystemRouter(options),
!options.ssr && solidStartCsrDev(options),
options.islands ? islands() : undefined,
options.inspect ? inspect({ outputDir: join(".solid", "inspect"), build: true }) : undefined,
options.ssr && solidStartInlineServerModules(options),
Expand Down
50 changes: 26 additions & 24 deletions test/api-routes-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,31 +153,33 @@ test.describe("api routes", () => {
expect(await page.content()).toContain('{"hello":"world"}');
});

test("should render data from API route using server$.fetch", async ({ page }) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/server-fetch");
let dataEl = await page.waitForSelector("[data-testid='data']");
expect(await dataEl!.innerText()).toBe("harry-potter");

await app.goto("/", true);
await page.click("a[href='/server-fetch']");
dataEl = await page.waitForSelector("[data-testid='data']");
expect(await dataEl!.innerText()).toBe("harry-potter");
});

test("should render data from API route using serverData with server$.fetch", async ({
page
}) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/server-data-fetch");
let dataEl = await page.waitForSelector("[data-testid='data']");
expect(await dataEl!.innerText()).toBe("harry-potter");
if (ssr) {
test("should render data from API route using server$.fetch", async ({ page }) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/server-fetch");
let dataEl = await page.waitForSelector("[data-testid='data']");
expect(await dataEl!.innerText()).toBe("harry-potter");

await app.goto("/", true);
await page.click("a[href='/server-fetch']");
dataEl = await page.waitForSelector("[data-testid='data']");
expect(await dataEl!.innerText()).toBe("harry-potter");
});

await app.goto("/", true);
await page.click("a[href='/server-data-fetch']");
dataEl = await page.waitForSelector("[data-testid='data']");
expect(await dataEl!.innerText()).toBe("harry-potter");
});
test("should render data from API route using serverData with server$.fetch", async ({
page
}) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/server-data-fetch");
let dataEl = await page.waitForSelector("[data-testid='data']");
expect(await dataEl!.innerText()).toBe("harry-potter");

await app.goto("/", true);
await page.click("a[href='/server-data-fetch']");
dataEl = await page.waitForSelector("[data-testid='data']");
expect(await dataEl!.innerText()).toBe("harry-potter");
});
}

test("should return json from API route", async ({ page }) => {
test.skip(process.env.START_ADAPTER === "solid-start-cloudflare-pages");
Expand Down
6 changes: 3 additions & 3 deletions test/misc-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ test.describe("miscellaneous tests", () => {
runTests(true);
});

test.describe("without SSR", () => {
runTests(false);
});
// test.describe("without SSR", () => {
// runTests(false);
// });

function runTests(ssr) {
test.beforeAll(async () => {
Expand Down

0 comments on commit 0a27fc2

Please sign in to comment.