From db3274912845ef407e634ef767e0e32034c027b7 Mon Sep 17 00:00:00 2001
From: Davide <43080019+edivados@users.noreply.github.com>
Date: Fri, 1 Sep 2023 19:08:23 +0200
Subject: [PATCH 01/18] return 405 for not implemented verbs on api routes
---
packages/start/api/index.ts | 4 ++++
packages/start/fs-router/router.js | 11 ++++++-----
2 files changed, 10 insertions(+), 5 deletions(-)
diff --git a/packages/start/api/index.ts b/packages/start/api/index.ts
index 398dce323..efcbcd03a 100644
--- a/packages/start/api/index.ts
+++ b/packages/start/api/index.ts
@@ -93,5 +93,9 @@ export function isApiRequest(request: Request) {
return Boolean(apiHandler);
}
+export function methodNotFound () {
+ return new Response(null, { status: 405 });
+}
+
export * from "../server/responses";
export type { APIEvent } from "./types";
diff --git a/packages/start/fs-router/router.js b/packages/start/fs-router/router.js
index 4acc5e710..e647716f3 100644
--- a/packages/start/fs-router/router.js
+++ b/packages/start/fs-router/router.js
@@ -386,17 +386,18 @@ export function stringifyAPIRoutes(
* @return {string}
*/
function _stringifyRoutes(/** @type {(RouteConfig)[]} */ routes) {
+ const methodNotFound = jsFile.addNamedImport("methodNotFound", "solid-start/api");
return (
`[\n` +
routes
.map(
i =>
`{\n${[
- ...API_METHODS.filter(j => i.apiPath?.[j]).map(
- v =>
- i.apiPath != null &&
- `${v}: ${jsFile.addNamedImport(v, path.posix.resolve(i.apiPath[v]))}`
- ),
+ ...API_METHODS.map(v => {
+ if (!i.apiPath || (i.componentPath && v === "GET")) return undefined;
+ else if (i.apiPath[v]) return `${v}: ${jsFile.addNamedImport(v, path.posix.resolve(i.apiPath[v]))}`;
+ else return `${v}: ${methodNotFound}`;
+ }),
i.componentPath ? `GET: "skip"` : undefined,
`path: ${JSON.stringify(i.path)}`
]
From e4703573fdda0d73c7456a7433780facb6da23d6 Mon Sep 17 00:00:00 2001
From: Davide <43080019+edivados@users.noreply.github.com>
Date: Fri, 1 Sep 2023 19:36:24 +0200
Subject: [PATCH 02/18] add test
---
test/api-routes-test.ts | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/test/api-routes-test.ts b/test/api-routes-test.ts
index 260555751..6f257e57e 100644
--- a/test/api-routes-test.ts
+++ b/test/api-routes-test.ts
@@ -131,6 +131,10 @@ test.describe("api routes", () => {
"src/routes/api/[param]/index.js": js`
import { json } from "solid-start/server";
export let GET = ({ params }) => json(params);
+ `,
+ "src/api/method-not-found.js": js`
+ import { json } from "solid-start/server";
+ export let GET = () => new Response();
`
}
});
@@ -280,5 +284,10 @@ test.describe("api routes", () => {
expect(res.headers.get("content-type")).toEqual("application/json; charset=utf-8");
expect(await res.json()).toEqual({ static: true });
});
+
+ test("should return status 405 for not implemented verbs", async () => {
+ let res = await fixture.requestDocument("/api/method-not-found", { method: "POST" });
+ expect(res.status).toEqual(405);
+ });
}
});
From 404e6e18e1e10e5c90f5015017a40e5fa7d69708 Mon Sep 17 00:00:00 2001
From: Davide <43080019+edivados@users.noreply.github.com>
Date: Fri, 1 Sep 2023 19:45:47 +0200
Subject: [PATCH 03/18] return 405 if route only exports component
---
packages/start/fs-router/router.js | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/packages/start/fs-router/router.js b/packages/start/fs-router/router.js
index e647716f3..495cb1858 100644
--- a/packages/start/fs-router/router.js
+++ b/packages/start/fs-router/router.js
@@ -394,8 +394,8 @@ export function stringifyAPIRoutes(
i =>
`{\n${[
...API_METHODS.map(v => {
- if (!i.apiPath || (i.componentPath && v === "GET")) return undefined;
- else if (i.apiPath[v]) return `${v}: ${jsFile.addNamedImport(v, path.posix.resolve(i.apiPath[v]))}`;
+ if (i.componentPath && v === "GET") return undefined;
+ else if (i.apiPath && i.apiPath[v]) return `${v}: ${jsFile.addNamedImport(v, path.posix.resolve(i.apiPath[v]))}`;
else return `${v}: ${methodNotFound}`;
}),
i.componentPath ? `GET: "skip"` : undefined,
From 68b7c5e9365651c2573c0fba0776ad2384263ad5 Mon Sep 17 00:00:00 2001
From: Davide <43080019+edivados@users.noreply.github.com>
Date: Fri, 1 Sep 2023 20:01:35 +0200
Subject: [PATCH 04/18] add test for page route with only default export
---
test/api-routes-test.ts | 17 +++++++++++++----
1 file changed, 13 insertions(+), 4 deletions(-)
diff --git a/test/api-routes-test.ts b/test/api-routes-test.ts
index 6f257e57e..ec17b7b28 100644
--- a/test/api-routes-test.ts
+++ b/test/api-routes-test.ts
@@ -132,9 +132,13 @@ test.describe("api routes", () => {
import { json } from "solid-start/server";
export let GET = ({ params }) => json(params);
`,
- "src/api/method-not-found.js": js`
- import { json } from "solid-start/server";
- export let GET = () => new Response();
+ "src/routes/method-not-found.jsx": js`
+ export default function Page() {
+ return
page
;
+ }
+ `,
+ "src/routes/api/method-not-found.js": js`
+ export function GET () { return new Response(); }
`
}
});
@@ -285,7 +289,12 @@ test.describe("api routes", () => {
expect(await res.json()).toEqual({ static: true });
});
- test("should return status 405 for not implemented verbs", async () => {
+ test("should return status 405 on page route for uninplemented verbs", async () => {
+ let res = await fixture.requestDocument("/method-not-found", { method: "POST" });
+ expect(res.status).toEqual(405);
+ });
+
+ test("should return status 405 on api route for uninplemented verbs", async () => {
let res = await fixture.requestDocument("/api/method-not-found", { method: "POST" });
expect(res.status).toEqual(405);
});
From 5f4c1863fe130d1fd0200e10a04f8e86abb08110 Mon Sep 17 00:00:00 2001
From: Davide <43080019+edivados@users.noreply.github.com>
Date: Fri, 1 Sep 2023 20:08:26 +0200
Subject: [PATCH 05/18] test description
---
test/api-routes-test.ts | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/test/api-routes-test.ts b/test/api-routes-test.ts
index ec17b7b28..b92651f2e 100644
--- a/test/api-routes-test.ts
+++ b/test/api-routes-test.ts
@@ -289,12 +289,12 @@ test.describe("api routes", () => {
expect(await res.json()).toEqual({ static: true });
});
- test("should return status 405 on page route for uninplemented verbs", async () => {
+ test("should return status 405 for uninplemented verbs on route with only a default export", async () => {
let res = await fixture.requestDocument("/method-not-found", { method: "POST" });
expect(res.status).toEqual(405);
});
- test("should return status 405 on api route for uninplemented verbs", async () => {
+ test("should return status 405 for uninplemented verbs on route with only a GET export", async () => {
let res = await fixture.requestDocument("/api/method-not-found", { method: "POST" });
expect(res.status).toEqual(405);
});
From b612a9180d56932bddb713e86603266fc76abddf Mon Sep 17 00:00:00 2001
From: Davide <43080019+edivados@users.noreply.github.com>
Date: Fri, 1 Sep 2023 20:33:15 +0200
Subject: [PATCH 06/18] small fix
---
packages/start/api/index.ts | 3 ++-
packages/start/fs-router/router.js | 8 +++++---
2 files changed, 7 insertions(+), 4 deletions(-)
diff --git a/packages/start/api/index.ts b/packages/start/api/index.ts
index efcbcd03a..92ec7585f 100644
--- a/packages/start/api/index.ts
+++ b/packages/start/api/index.ts
@@ -93,9 +93,10 @@ export function isApiRequest(request: Request) {
return Boolean(apiHandler);
}
-export function methodNotFound () {
+export function methodNotFound() {
return new Response(null, { status: 405 });
}
export * from "../server/responses";
export type { APIEvent } from "./types";
+
diff --git a/packages/start/fs-router/router.js b/packages/start/fs-router/router.js
index 495cb1858..2da283a5b 100644
--- a/packages/start/fs-router/router.js
+++ b/packages/start/fs-router/router.js
@@ -394,9 +394,11 @@ export function stringifyAPIRoutes(
i =>
`{\n${[
...API_METHODS.map(v => {
- if (i.componentPath && v === "GET") return undefined;
- else if (i.apiPath && i.apiPath[v]) return `${v}: ${jsFile.addNamedImport(v, path.posix.resolve(i.apiPath[v]))}`;
- else return `${v}: ${methodNotFound}`;
+ if (i.componentPath && v === "GET")
+ return undefined;
+ else if (i.apiPath?.[v])
+ return `${v}: ${jsFile.addNamedImport(v, path.posix.resolve(i.apiPath[v]))}`;
+ return `${v}: ${methodNotFound}`;
}),
i.componentPath ? `GET: "skip"` : undefined,
`path: ${JSON.stringify(i.path)}`
From 351d500503d84e242b2be04f0c4feb7341a93ba4 Mon Sep 17 00:00:00 2001
From: Davide <43080019+edivados@users.noreply.github.com>
Date: Fri, 1 Sep 2023 21:03:57 +0200
Subject: [PATCH 07/18] test all verbs
---
test/api-routes-test.ts | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/test/api-routes-test.ts b/test/api-routes-test.ts
index b92651f2e..c4413b992 100644
--- a/test/api-routes-test.ts
+++ b/test/api-routes-test.ts
@@ -290,13 +290,17 @@ test.describe("api routes", () => {
});
test("should return status 405 for uninplemented verbs on route with only a default export", async () => {
- let res = await fixture.requestDocument("/method-not-found", { method: "POST" });
- expect(res.status).toEqual(405);
+ ["POST", "PUT", "PATCH", "DELETE"].forEach(async method => {
+ let res = await fixture.requestDocument("/method-not-found", { method });
+ expect(res.status).toEqual(405);
+ })
});
test("should return status 405 for uninplemented verbs on route with only a GET export", async () => {
- let res = await fixture.requestDocument("/api/method-not-found", { method: "POST" });
- expect(res.status).toEqual(405);
+ ["POST", "PUT", "PATCH", "DELETE"].forEach(async method => {
+ let res = await fixture.requestDocument("/api/method-not-found", { method });
+ expect(res.status).toEqual(405);
+ });
});
}
});
From 5e32a06e1f39b31ba76f2aef27cbec308f715711 Mon Sep 17 00:00:00 2001
From: Davide <43080019+edivados@users.noreply.github.com>
Date: Fri, 1 Sep 2023 21:46:17 +0200
Subject: [PATCH 08/18] make it work in dev
---
packages/start/api/index.ts | 4 ----
packages/start/api/utils.ts | 3 +++
packages/start/fs-router/router.js | 4 ++--
3 files changed, 5 insertions(+), 6 deletions(-)
create mode 100644 packages/start/api/utils.ts
diff --git a/packages/start/api/index.ts b/packages/start/api/index.ts
index 92ec7585f..15858382e 100644
--- a/packages/start/api/index.ts
+++ b/packages/start/api/index.ts
@@ -93,10 +93,6 @@ export function isApiRequest(request: Request) {
return Boolean(apiHandler);
}
-export function methodNotFound() {
- return new Response(null, { status: 405 });
-}
-
export * from "../server/responses";
export type { APIEvent } from "./types";
diff --git a/packages/start/api/utils.ts b/packages/start/api/utils.ts
new file mode 100644
index 000000000..961b2ad51
--- /dev/null
+++ b/packages/start/api/utils.ts
@@ -0,0 +1,3 @@
+export function methodNotFound() {
+ return new Response(null, { status: 405 });
+}
diff --git a/packages/start/fs-router/router.js b/packages/start/fs-router/router.js
index 2da283a5b..fedf556ad 100644
--- a/packages/start/fs-router/router.js
+++ b/packages/start/fs-router/router.js
@@ -386,7 +386,7 @@ export function stringifyAPIRoutes(
* @return {string}
*/
function _stringifyRoutes(/** @type {(RouteConfig)[]} */ routes) {
- const methodNotFound = jsFile.addNamedImport("methodNotFound", "solid-start/api");
+ const methodNotFound = jsFile.addNamedImport("methodNotFound", "solid-start/api/utils");
return (
`[\n` +
routes
@@ -396,7 +396,7 @@ export function stringifyAPIRoutes(
...API_METHODS.map(v => {
if (i.componentPath && v === "GET")
return undefined;
- else if (i.apiPath?.[v])
+ else if (i.apiPath?.[v])
return `${v}: ${jsFile.addNamedImport(v, path.posix.resolve(i.apiPath[v]))}`;
return `${v}: ${methodNotFound}`;
}),
From 330ccf37b0a0466a58aad67e8b412bd8beb5850d Mon Sep 17 00:00:00 2001
From: Davide <43080019+edivados@users.noreply.github.com>
Date: Fri, 1 Sep 2023 21:52:07 +0200
Subject: [PATCH 09/18] single line test file
---
test/api-routes-test.ts | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/test/api-routes-test.ts b/test/api-routes-test.ts
index c4413b992..ae33d1197 100644
--- a/test/api-routes-test.ts
+++ b/test/api-routes-test.ts
@@ -133,9 +133,7 @@ test.describe("api routes", () => {
export let GET = ({ params }) => json(params);
`,
"src/routes/method-not-found.jsx": js`
- export default function Page() {
- return page
;
- }
+ export default function Page() { return page
; }
`,
"src/routes/api/method-not-found.js": js`
export function GET () { return new Response(); }
From cee998d188d85d80b6325e3bab64387063acf67f Mon Sep 17 00:00:00 2001
From: Davide <43080019+edivados@users.noreply.github.com>
Date: Fri, 1 Sep 2023 22:53:37 +0200
Subject: [PATCH 10/18] better test description
---
packages/start/api/index.ts | 1 -
test/api-routes-test.ts | 4 ++--
2 files changed, 2 insertions(+), 3 deletions(-)
diff --git a/packages/start/api/index.ts b/packages/start/api/index.ts
index 15858382e..398dce323 100644
--- a/packages/start/api/index.ts
+++ b/packages/start/api/index.ts
@@ -95,4 +95,3 @@ export function isApiRequest(request: Request) {
export * from "../server/responses";
export type { APIEvent } from "./types";
-
diff --git a/test/api-routes-test.ts b/test/api-routes-test.ts
index ae33d1197..4ac0586f7 100644
--- a/test/api-routes-test.ts
+++ b/test/api-routes-test.ts
@@ -287,14 +287,14 @@ test.describe("api routes", () => {
expect(await res.json()).toEqual({ static: true });
});
- test("should return status 405 for uninplemented verbs on route with only a default export", async () => {
+ test("should return 405 for undefined handlers on route with only a default export", async () => {
["POST", "PUT", "PATCH", "DELETE"].forEach(async method => {
let res = await fixture.requestDocument("/method-not-found", { method });
expect(res.status).toEqual(405);
})
});
- test("should return status 405 for uninplemented verbs on route with only a GET export", async () => {
+ test("should return 405 for undefined handlers on route with only a GET export", async () => {
["POST", "PUT", "PATCH", "DELETE"].forEach(async method => {
let res = await fixture.requestDocument("/api/method-not-found", { method });
expect(res.status).toEqual(405);
From da5a4a858f32acd65c650338028c6063d078a3eb Mon Sep 17 00:00:00 2001
From: Davide <43080019+edivados@users.noreply.github.com>
Date: Fri, 1 Sep 2023 23:21:20 +0200
Subject: [PATCH 11/18] cover GET handler in test
---
test/api-routes-test.ts | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/test/api-routes-test.ts b/test/api-routes-test.ts
index 4ac0586f7..7bbe17048 100644
--- a/test/api-routes-test.ts
+++ b/test/api-routes-test.ts
@@ -136,7 +136,7 @@ test.describe("api routes", () => {
export default function Page() { return page
; }
`,
"src/routes/api/method-not-found.js": js`
- export function GET () { return new Response(); }
+ export function POST () { return new Response(); }
`
}
});
@@ -294,8 +294,8 @@ test.describe("api routes", () => {
})
});
- test("should return 405 for undefined handlers on route with only a GET export", async () => {
- ["POST", "PUT", "PATCH", "DELETE"].forEach(async method => {
+ test("should return 405 for undefined handlers on route with only a POST export", async () => {
+ ["GET", "PUT", "PATCH", "DELETE"].forEach(async method => {
let res = await fixture.requestDocument("/api/method-not-found", { method });
expect(res.status).toEqual(405);
});
From 53d3b672da83ae0908c6656a9acec03ca8591906 Mon Sep 17 00:00:00 2001
From: Davide <43080019+edivados@users.noreply.github.com>
Date: Sat, 2 Sep 2023 00:01:33 +0200
Subject: [PATCH 12/18] it's method not allowed not method not found
---
test/api-routes-test.ts | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/test/api-routes-test.ts b/test/api-routes-test.ts
index 7bbe17048..808571624 100644
--- a/test/api-routes-test.ts
+++ b/test/api-routes-test.ts
@@ -132,10 +132,10 @@ test.describe("api routes", () => {
import { json } from "solid-start/server";
export let GET = ({ params }) => json(params);
`,
- "src/routes/method-not-found.jsx": js`
+ "src/routes/method-not-allowed.jsx": js`
export default function Page() { return page
; }
`,
- "src/routes/api/method-not-found.js": js`
+ "src/routes/api/method-not-allowed.js": js`
export function POST () { return new Response(); }
`
}
@@ -289,14 +289,14 @@ test.describe("api routes", () => {
test("should return 405 for undefined handlers on route with only a default export", async () => {
["POST", "PUT", "PATCH", "DELETE"].forEach(async method => {
- let res = await fixture.requestDocument("/method-not-found", { method });
+ let res = await fixture.requestDocument("/method-not-allowed", { method });
expect(res.status).toEqual(405);
})
});
test("should return 405 for undefined handlers on route with only a POST export", async () => {
["GET", "PUT", "PATCH", "DELETE"].forEach(async method => {
- let res = await fixture.requestDocument("/api/method-not-found", { method });
+ let res = await fixture.requestDocument("/api/method-not-allowed", { method });
expect(res.status).toEqual(405);
});
});
From 093fa03feec058b2bbfc5d75245f65b33ec05e94 Mon Sep 17 00:00:00 2001
From: Davide <43080019+edivados@users.noreply.github.com>
Date: Sat, 2 Sep 2023 00:07:54 +0200
Subject: [PATCH 13/18] test all handlers
---
test/api-routes-test.ts | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/test/api-routes-test.ts b/test/api-routes-test.ts
index 808571624..358e7530e 100644
--- a/test/api-routes-test.ts
+++ b/test/api-routes-test.ts
@@ -288,15 +288,19 @@ test.describe("api routes", () => {
});
test("should return 405 for undefined handlers on route with only a default export", async () => {
+ let res = await fixture.requestDocument("/method-not-allowed", { method: "GET" });
+ expect(res.status).toEqual(200);
["POST", "PUT", "PATCH", "DELETE"].forEach(async method => {
- let res = await fixture.requestDocument("/method-not-allowed", { method });
+ res = await fixture.requestDocument("/method-not-allowed", { method });
expect(res.status).toEqual(405);
})
});
test("should return 405 for undefined handlers on route with only a POST export", async () => {
+ let res = await fixture.requestDocument("/api/method-not-allowed", { method: "POST" });
+ expect(res.status).toEqual(200);
["GET", "PUT", "PATCH", "DELETE"].forEach(async method => {
- let res = await fixture.requestDocument("/api/method-not-allowed", { method });
+ res = await fixture.requestDocument("/api/method-not-allowed", { method });
expect(res.status).toEqual(405);
});
});
From 23a2021b34cf1f8be583ceda90958e7755f104b8 Mon Sep 17 00:00:00 2001
From: Davide <43080019+edivados@users.noreply.github.com>
Date: Sat, 2 Sep 2023 00:18:06 +0200
Subject: [PATCH 14/18] more method not found corrections
---
packages/start/api/utils.ts | 2 +-
packages/start/fs-router/router.js | 4 ++--
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/packages/start/api/utils.ts b/packages/start/api/utils.ts
index 961b2ad51..d9ee9cb3e 100644
--- a/packages/start/api/utils.ts
+++ b/packages/start/api/utils.ts
@@ -1,3 +1,3 @@
-export function methodNotFound() {
+export function methodNotAllowed() {
return new Response(null, { status: 405 });
}
diff --git a/packages/start/fs-router/router.js b/packages/start/fs-router/router.js
index fedf556ad..c0ea3ed79 100644
--- a/packages/start/fs-router/router.js
+++ b/packages/start/fs-router/router.js
@@ -386,7 +386,7 @@ export function stringifyAPIRoutes(
* @return {string}
*/
function _stringifyRoutes(/** @type {(RouteConfig)[]} */ routes) {
- const methodNotFound = jsFile.addNamedImport("methodNotFound", "solid-start/api/utils");
+ const methodNotAllowed = jsFile.addNamedImport("methodNotAllowed", "solid-start/api/utils");
return (
`[\n` +
routes
@@ -398,7 +398,7 @@ export function stringifyAPIRoutes(
return undefined;
else if (i.apiPath?.[v])
return `${v}: ${jsFile.addNamedImport(v, path.posix.resolve(i.apiPath[v]))}`;
- return `${v}: ${methodNotFound}`;
+ return `${v}: ${methodNotAllowed}`;
}),
i.componentPath ? `GET: "skip"` : undefined,
`path: ${JSON.stringify(i.path)}`
From 7cb0b1e09e7b97ea99b725a579ccdab64ff9ea32 Mon Sep 17 00:00:00 2001
From: Davide <43080019+edivados@users.noreply.github.com>
Date: Sat, 2 Sep 2023 02:31:43 +0200
Subject: [PATCH 15/18] flip apiRoutes and inlineServerFunctions middleware
order
---
packages/start/entry-server/index.ts | 10 +++++-----
packages/start/server/render.ts | 14 +++++++-------
2 files changed, 12 insertions(+), 12 deletions(-)
diff --git a/packages/start/entry-server/index.ts b/packages/start/entry-server/index.ts
index 25a1c2a46..648c79dde 100644
--- a/packages/start/entry-server/index.ts
+++ b/packages/start/entry-server/index.ts
@@ -1,4 +1,4 @@
-export { composeMiddleware, createHandler, default as StartServer } from "./StartServer";
+export { default as StartServer, composeMiddleware, createHandler } from "./StartServer";
export type { Middleware, MiddlewareFn, MiddlewareInput } from "./StartServer";
import { JSX } from "solid-js";
@@ -21,8 +21,8 @@ export const render = (
}
) =>
composeMiddleware([
- apiRoutes,
inlineServerFunctions,
+ apiRoutes,
import.meta.env.START_SSR === "async"
? _renderAsync(fn, options)
: import.meta.env.START_SSR === "streaming"
@@ -37,7 +37,7 @@ export const renderAsync = (
nonce?: string;
renderId?: string;
}
-) => composeMiddleware([apiRoutes, inlineServerFunctions, _renderAsync(fn, options)]);
+) => composeMiddleware([inlineServerFunctions, apiRoutes, _renderAsync(fn, options)]);
export const renderStream = (
fn: (context: PageEvent) => JSX.Element,
@@ -46,7 +46,7 @@ export const renderStream = (
nonce?: string;
renderId?: string;
}
-) => composeMiddleware([apiRoutes, inlineServerFunctions, _renderStream(fn, options)]);
+) => composeMiddleware([inlineServerFunctions, apiRoutes, _renderStream(fn, options)]);
export const renderSync = (
fn: (context: PageEvent) => JSX.Element,
@@ -55,4 +55,4 @@ export const renderSync = (
nonce?: string;
renderId?: string;
}
-) => composeMiddleware([apiRoutes, inlineServerFunctions, _renderSync(fn, options)]);
+) => composeMiddleware([inlineServerFunctions, apiRoutes, _renderSync(fn, options)]);
diff --git a/packages/start/server/render.ts b/packages/start/server/render.ts
index 8b2e8dc36..c6801079e 100644
--- a/packages/start/server/render.ts
+++ b/packages/start/server/render.ts
@@ -3,7 +3,7 @@ import { renderToStream, renderToString, renderToStringAsync } from "solid-js/we
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,
@@ -12,8 +12,8 @@ export function renderSync(
renderId?: string;
}
) {
- return () => apiRoutes({
- forward: inlineServerFunctions({
+ return () => inlineServerFunctions({
+ forward: apiRoutes({
async forward(event: FetchEvent): Promise {
if (
!import.meta.env.DEV &&
@@ -53,8 +53,8 @@ export function renderAsync(
renderId?: string;
}
) {
- return () => apiRoutes({
- forward: inlineServerFunctions({
+ return () => inlineServerFunctions({
+ forward: apiRoutes({
async forward(event: FetchEvent): Promise {
if (
!import.meta.env.DEV &&
@@ -96,8 +96,8 @@ export function renderStream(
onCompleteAll?: (info: { write: (v: string) => void }) => void;
} = {}
) {
- return () => apiRoutes({
- forward: inlineServerFunctions({
+ return () => inlineServerFunctions({
+ forward: apiRoutes({
async forward(event: FetchEvent): Promise {
if (
!import.meta.env.DEV &&
From 0470bdf67be6c88efbbef7f592e3735b64f88655 Mon Sep 17 00:00:00 2001
From: Davide <43080019+edivados@users.noreply.github.com>
Date: Sat, 2 Sep 2023 10:27:41 +0200
Subject: [PATCH 16/18] move methodNotAllowed function to api/index.ts
---
packages/start/api/index.ts | 7 +++++++
packages/start/api/utils.ts | 3 ---
packages/start/fs-router/router.js | 3 +--
3 files changed, 8 insertions(+), 5 deletions(-)
delete mode 100644 packages/start/api/utils.ts
diff --git a/packages/start/api/index.ts b/packages/start/api/index.ts
index 398dce323..2e81be321 100644
--- a/packages/start/api/index.ts
+++ b/packages/start/api/index.ts
@@ -8,6 +8,12 @@ import { MatchRoute, Method, Route } from "./types";
// @ts-ignore
var api = $API_ROUTES;
+// used by the compiled configuration of routes
+// @ts-ignore
+function methodNotAllowed() {
+ return new Response(null, { status: 405 });
+}
+
// This is copied from https://github.com/solidjs/solid-router/blob/main/src/utils.ts
function expandOptionals(pattern: string): string[] {
let match = /(\/?\:[^\/]+)\?/.exec(pattern);
@@ -95,3 +101,4 @@ export function isApiRequest(request: Request) {
export * from "../server/responses";
export type { APIEvent } from "./types";
+
diff --git a/packages/start/api/utils.ts b/packages/start/api/utils.ts
deleted file mode 100644
index d9ee9cb3e..000000000
--- a/packages/start/api/utils.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export function methodNotAllowed() {
- return new Response(null, { status: 405 });
-}
diff --git a/packages/start/fs-router/router.js b/packages/start/fs-router/router.js
index c0ea3ed79..52aa9478a 100644
--- a/packages/start/fs-router/router.js
+++ b/packages/start/fs-router/router.js
@@ -386,7 +386,6 @@ export function stringifyAPIRoutes(
* @return {string}
*/
function _stringifyRoutes(/** @type {(RouteConfig)[]} */ routes) {
- const methodNotAllowed = jsFile.addNamedImport("methodNotAllowed", "solid-start/api/utils");
return (
`[\n` +
routes
@@ -398,7 +397,7 @@ export function stringifyAPIRoutes(
return undefined;
else if (i.apiPath?.[v])
return `${v}: ${jsFile.addNamedImport(v, path.posix.resolve(i.apiPath[v]))}`;
- return `${v}: ${methodNotAllowed}`;
+ return `${v}: methodNotAllowed`;
}),
i.componentPath ? `GET: "skip"` : undefined,
`path: ${JSON.stringify(i.path)}`
From ac20e90a528fee5c6a78c0f4ea299ee03568f42d Mon Sep 17 00:00:00 2001
From: Davide <43080019+edivados@users.noreply.github.com>
Date: Sun, 3 Sep 2023 11:30:37 +0200
Subject: [PATCH 17/18] fix islands
---
packages/start/fs-router/router.js | 9 +++++----
packages/start/vite/plugin.js | 6 +++++-
2 files changed, 10 insertions(+), 5 deletions(-)
diff --git a/packages/start/fs-router/router.js b/packages/start/fs-router/router.js
index 52aa9478a..9db044820 100644
--- a/packages/start/fs-router/router.js
+++ b/packages/start/fs-router/router.js
@@ -378,7 +378,7 @@ export function stringifyPageRoutes(
export function stringifyAPIRoutes(
/** @type {(RouteConfig)[]} */ flatRoutes,
- /** @type {{ lazy?: boolean }} */ options = {}
+ /** @type {{ lazy?: boolean, islands?: boolean }} */ options = {}
) {
const jsFile = jsCode();
@@ -393,13 +393,14 @@ export function stringifyAPIRoutes(
i =>
`{\n${[
...API_METHODS.map(v => {
- if (i.componentPath && v === "GET")
- return undefined;
+ if (v === "GET" && i.componentPath)
+ return `${v}: "skip"`;
+ if (v === "POST" && options.islands && i.componentPath)
+ return `${v}: "skip"`;
else if (i.apiPath?.[v])
return `${v}: ${jsFile.addNamedImport(v, path.posix.resolve(i.apiPath[v]))}`;
return `${v}: methodNotAllowed`;
}),
- i.componentPath ? `GET: "skip"` : undefined,
`path: ${JSON.stringify(i.path)}`
]
.filter(Boolean)
diff --git a/packages/start/vite/plugin.js b/packages/start/vite/plugin.js
index 274542673..53e8973d0 100644
--- a/packages/start/vite/plugin.js
+++ b/packages/start/vite/plugin.js
@@ -206,6 +206,9 @@ function solidStartFileSystemRouter(options) {
/** @type {boolean} */
let lazy = true;
+ /** @type {boolean} */
+ let islands = false;
+
const babelOptions =
(/** @type {any} */ getBabelOptions) =>
async (/** @type {string} */ source, /** @type {string} */ id, /** @type {boolean} */ ssr) => {
@@ -226,6 +229,7 @@ function solidStartFileSystemRouter(options) {
// @ts-expect-error
config = _config;
+ islands = _config.solidOptions.experimental.islands;
lazy = _config.command !== "serve";
await config.solidOptions.router.init();
},
@@ -440,7 +444,7 @@ function solidStartFileSystemRouter(options) {
return {
code: code.replace(
"var api = $API_ROUTES;",
- stringifyAPIRoutes(config.solidOptions.router.getFlattenedApiRoutes(true), { lazy })
+ stringifyAPIRoutes(config.solidOptions.router.getFlattenedApiRoutes(true), { lazy, islands })
)
};
}
From 9326c3bfc5405a8fe701d81f61c14dc051828566 Mon Sep 17 00:00:00 2001
From: Davide <43080019+edivados@users.noreply.github.com>
Date: Tue, 10 Oct 2023 11:26:15 +0000
Subject: [PATCH 18/18] checking for islandRsouter instead of islands
---
packages/start/fs-router/router.js | 4 ++--
packages/start/vite/plugin.js | 9 ++++-----
2 files changed, 6 insertions(+), 7 deletions(-)
diff --git a/packages/start/fs-router/router.js b/packages/start/fs-router/router.js
index 9db044820..b2fa20df1 100644
--- a/packages/start/fs-router/router.js
+++ b/packages/start/fs-router/router.js
@@ -378,7 +378,7 @@ export function stringifyPageRoutes(
export function stringifyAPIRoutes(
/** @type {(RouteConfig)[]} */ flatRoutes,
- /** @type {{ lazy?: boolean, islands?: boolean }} */ options = {}
+ /** @type {{ lazy?: boolean, islandsRouter?: boolean }} */ options = {}
) {
const jsFile = jsCode();
@@ -395,7 +395,7 @@ export function stringifyAPIRoutes(
...API_METHODS.map(v => {
if (v === "GET" && i.componentPath)
return `${v}: "skip"`;
- if (v === "POST" && options.islands && i.componentPath)
+ if (v === "POST" && options.islandsRouter && i.componentPath)
return `${v}: "skip"`;
else if (i.apiPath?.[v])
return `${v}: ${jsFile.addNamedImport(v, path.posix.resolve(i.apiPath[v]))}`;
diff --git a/packages/start/vite/plugin.js b/packages/start/vite/plugin.js
index 8448c82e6..c01c4bfe2 100644
--- a/packages/start/vite/plugin.js
+++ b/packages/start/vite/plugin.js
@@ -206,9 +206,6 @@ function solidStartFileSystemRouter(options) {
/** @type {boolean} */
let lazy = true;
- /** @type {boolean} */
- let islands = false;
-
const babelOptions =
(/** @type {any} */ getBabelOptions) =>
async (/** @type {string} */ source, /** @type {string} */ id, /** @type {boolean} */ ssr) => {
@@ -229,7 +226,6 @@ function solidStartFileSystemRouter(options) {
// @ts-expect-error
config = _config;
- islands = _config.solidOptions.experimental.islands;
lazy = _config.command !== "serve";
await config.solidOptions.router.init();
},
@@ -444,7 +440,10 @@ function solidStartFileSystemRouter(options) {
return {
code: code.replace(
"var api = $API_ROUTES;",
- stringifyAPIRoutes(config.solidOptions.router.getFlattenedApiRoutes(true), { lazy, islands })
+ stringifyAPIRoutes(
+ config.solidOptions.router.getFlattenedApiRoutes(true),
+ { lazy, islandsRouter: !!config.solidOptions.experimental?.islandsRouter }
+ )
)
};
}