Skip to content

Commit 9484442

Browse files
committed
return Response instead of sendWebResponse
1 parent 405baee commit 9484442

1 file changed

Lines changed: 65 additions & 72 deletions

File tree

packages/start/src/config/nitroPlugin.ts

Lines changed: 65 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,28 @@
1+
import { promises as fsp } from "node:fs";
2+
import path, { dirname, resolve } from "node:path";
13
import {
2-
_RequestMiddleware,
3-
_ResponseMiddleware,
44
createApp,
55
createEvent,
6-
EventHandler,
6+
type EventHandler,
77
eventHandler,
8-
EventHandlerObject,
98
getHeader,
10-
H3Event,
11-
sendWebResponse
129
} from "h3";
1310
import {
1411
build,
1512
copyPublicAssets,
1613
createNitro,
17-
Nitro,
14+
type Nitro,
15+
type NitroConfig,
1816
prepare,
1917
prerender,
20-
type NitroConfig
2118
} from "nitropack";
22-
import { promises as fsp } from "node:fs";
23-
import path, { dirname, resolve } from "node:path";
2419
import {
25-
Connect,
26-
EnvironmentOptions,
20+
type Connect,
21+
type EnvironmentOptions,
2722
isRunnableDevEnvironment,
28-
PluginOption,
29-
Rollup,
30-
ViteDevServer
23+
type PluginOption,
24+
type Rollup,
25+
type ViteDevServer,
3126
} from "vite";
3227

3328
export const clientDistDir = "node_modules/.solid-start/client-dist";
@@ -69,38 +64,33 @@ export function nitroPlugin(
6964
return await serverEntry.default(event).catch((e: unknown) => {
7065
console.error(e);
7166
viteDevServer.ssrFixStacktrace(e as Error);
67+
7268
if (
73-
getHeader(event, "content-type")?.includes(
74-
"application/json",
75-
)
69+
getHeader(event, "content-type")?.includes("application/json")
7670
) {
77-
return sendWebResponse(
78-
event,
79-
new Response(
80-
JSON.stringify(
81-
{
82-
status: 500,
83-
error: "Internal Server Error",
84-
message:
85-
"An unexpected error occurred. Please try again later.",
86-
timestamp: new Date().toISOString(),
87-
},
88-
null,
89-
2,
90-
),
71+
return new Response(
72+
JSON.stringify(
9173
{
9274
status: 500,
93-
headers: {
94-
"Content-Type": "application/json",
95-
},
75+
error: "Internal Server Error",
76+
message:
77+
"An unexpected error occurred. Please try again later.",
78+
timestamp: new Date().toISOString(),
9679
},
80+
null,
81+
2,
9782
),
83+
{
84+
status: 500,
85+
headers: {
86+
"Content-Type": "application/json",
87+
},
88+
},
9889
);
9990
}
100-
return sendWebResponse(
101-
event,
102-
new Response(
103-
`
91+
92+
return new Response(
93+
`
10494
<!DOCTYPE html>
10595
<html lang="en">
10696
<head>
@@ -109,28 +99,26 @@ export function nitroPlugin(
10999
<script type="module">
110100
import { ErrorOverlay } from '/@vite/client'
111101
document.body.appendChild(new ErrorOverlay(${JSON.stringify(
112-
prepareError(
113-
event.node.req,
114-
e,
115-
),
116-
).replace(/</g, "\\u003c")}))
102+
prepareError(
103+
event.node.req,
104+
e,
105+
),
106+
).replace(/</g, "\\u003c")}))
117107
</script>
118108
</head>
119109
<body>
120110
</body>
121111
</html>
122112
`,
123-
{
124-
status: 500,
125-
headers: {
126-
"Content-Type": "text/html",
127-
},
113+
{
114+
status: 500,
115+
headers: {
116+
"Content-Type": "text/html",
128117
},
129-
),
118+
},
130119
);
131-
})
132-
}
133-
),
120+
});
121+
}),
134122
);
135123

136124
viteDevServer.middlewares.use(async (req, res) => {
@@ -139,7 +127,7 @@ export function nitroPlugin(
139127
await h3App.handler(event);
140128
});
141129
};
142-
}
130+
},
143131
},
144132
{
145133
name: "solid-start-vite-plugin-nitro",
@@ -148,14 +136,14 @@ export function nitroPlugin(
148136
return {
149137
build: {
150138
commonjsOptions: {
151-
include: []
139+
include: [],
152140
},
153141
ssr: true,
154142
sourcemap: true,
155143
rollupOptions: {
156-
input: "~/entry-server.tsx"
157-
}
158-
}
144+
input: "~/entry-server.tsx",
145+
},
146+
},
159147
} satisfies EnvironmentOptions;
160148
}
161149

@@ -181,29 +169,34 @@ export function nitroPlugin(
181169
preset: "node-server",
182170
typescript: {
183171
generateTsConfig: false,
184-
generateRuntimeConfigTypes: false
172+
generateRuntimeConfigTypes: false,
185173
},
186174
...nitroConfig,
187175
dev: false,
188-
publicAssets: [{ dir: path.resolve(options.root, clientDistDir) }],
176+
publicAssets: [
177+
{ dir: path.resolve(options.root, clientDistDir) },
178+
],
189179
renderer: ssrEntryFile,
190180
rollupConfig: {
191-
plugins: [virtualBundlePlugin(getSsrBundle()) as any]
192-
}
181+
plugins: [virtualBundlePlugin(getSsrBundle()) as any],
182+
},
193183
};
194184

195185
const nitro = await createNitro(resolvedNitroConfig);
196186

197187
await buildNitroEnvironment(nitro, () => build(nitro));
198-
}
199-
}
188+
},
189+
},
200190
};
201-
}
202-
}
191+
},
192+
},
203193
];
204194
}
205195

206-
export async function buildNitroEnvironment(nitro: Nitro, build: () => Promise<any>) {
196+
export async function buildNitroEnvironment(
197+
nitro: Nitro,
198+
build: () => Promise<any>,
199+
) {
207200
await prepare(nitro);
208201
await copyPublicAssets(nitro);
209202
await prerender(nitro);
@@ -233,7 +226,7 @@ function virtualBundlePlugin(ssrBundle: Rollup.OutputBundle): PluginOption {
233226
if (content.type === "chunk") {
234227
const virtualModule: VirtualModule = {
235228
code: content.code,
236-
map: null
229+
map: null,
237230
};
238231
const maybeMap = ssrBundle[`${fileName}.map`];
239232
if (maybeMap && maybeMap.type === "asset") {
@@ -265,7 +258,7 @@ function virtualBundlePlugin(ssrBundle: Rollup.OutputBundle): PluginOption {
265258
return null;
266259
}
267260
return m;
268-
}
261+
},
269262
};
270263
}
271264

@@ -278,13 +271,13 @@ function removeHtmlMiddlewares(server: ViteDevServer) {
278271
const html_middlewares = [
279272
"viteIndexHtmlMiddleware",
280273
"vite404Middleware",
281-
"viteSpaFallbackMiddleware"
274+
"viteSpaFallbackMiddleware",
282275
];
283276
for (let i = server.middlewares.stack.length - 1; i > 0; i--) {
284277
if (
285278
html_middlewares.includes(
286279
// @ts-expect-error
287-
server.middlewares.stack[i].handle.name
280+
server.middlewares.stack[i].handle.name,
288281
)
289282
) {
290283
server.middlewares.stack.splice(i, 1);
@@ -303,6 +296,6 @@ function prepareError(req: Connect.IncomingMessage, error: unknown) {
303296
return {
304297
message: `An error occured while server rendering ${req.url}:\n\n\t${typeof e === "string" ? e : e.message
305298
} `,
306-
stack: typeof e === "string" ? "" : e.stack
299+
stack: typeof e === "string" ? "" : e.stack,
307300
};
308301
}

0 commit comments

Comments
 (0)