Skip to content

Commit 43853ec

Browse files
committed
Fix integration test and call handleError on prerender 404s
1 parent aa54c68 commit 43853ec

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

.changeset/prerender-spa-fallback.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ Generate a "SPA fallback" HTML file for scenarios where applications are prerend
1010
- The generated HTML would include the root index route which prevented hydration for any other paths
1111
- With this change, we now generate a "SPA Mode" file in `__spa-fallback__.html` that will allow you to hydrate for any non-prerendered paths
1212
- You can serve this file from your static file server for any paths that would otherwise 404 if you only want to pre-render _some_ routes in your `ssr:false` app and serve the others as a SPA
13+
- `npx sirv-cli build/client --single __spa-fallback__.html`

packages/react-router/lib/server-runtime/server.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export const createRequestHandler: CreateRequestHandlerFunction = (
110110
let normalizedPath = url.pathname
111111
.replace(/\.data$/, "")
112112
.replace(/^\/_root$/, "/")
113-
.replace(/\/$/, "");
113+
.replace(/.\/$/, "");
114114
let params: RouteMatch<ServerRoute>["params"] = {};
115115
let handleError = (error: unknown) => {
116116
if (mode === ServerMode.Development) {
@@ -135,6 +135,20 @@ export const createRequestHandler: CreateRequestHandlerFunction = (
135135
!_build.prerender.includes(normalizedPath) &&
136136
!_build.prerender.includes(normalizedPath + "/")
137137
) {
138+
errorHandler(
139+
new ErrorResponseImpl(
140+
404,
141+
"Not Found",
142+
`Refusing to SSR the path \`${normalizedPath}\` because \`ssr:false\` is ` +
143+
"set and the path is not included in the `prerender` config, so in " +
144+
"production the path will be a 404."
145+
),
146+
{
147+
context: loadContext,
148+
params,
149+
request,
150+
}
151+
);
138152
return new Response("Not Found", {
139153
status: 404,
140154
statusText: "Not Found",

0 commit comments

Comments
 (0)