Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: RR7 - With basename configured, .data requests against root index not correctly routed #12295

Open
mpqmpqm opened this issue Nov 15, 2024 · 1 comment
Labels

Comments

@mpqmpqm
Copy link

mpqmpqm commented Nov 15, 2024

What version of React Router are you using?

7

Steps to Reproduce

Minimal repro

  1. Configure vite.config.base and vite.config.reactRouter.basename.
  2. Add a Link component to the root index, or an action against the root index.
  3. Observe that .data requests against root index are routed to localhost:5173/${basename}.data, rather than e.g. localhost:5173/${basename}/_root.data.
  4. Observe that .data requests against root index 404.

Expected Behavior

.data requests against root index should resolve accounting for configured basename.

Actual Behavior

.data requests against root index 404. .data requests against nested routes resolve as expected.

In minimal repro above, try clicking links to Home and submitting action on that route. Then try the same at About. Observe that About works while Home (root index) doesn't.

@mpqmpqm mpqmpqm added the bug label Nov 15, 2024
@mpqmpqm
Copy link
Author

mpqmpqm commented Nov 18, 2024

Here are the patches I issued to fix this in my Remix 2.14.0 project:

diff --git a/node_modules/@remix-run/react/dist/esm/single-fetch.js b/node_modules/@remix-run/react/dist/esm/single-fetch.js
index 96f10cd..4126369 100644
--- a/node_modules/@remix-run/react/dist/esm/single-fetch.js
+++ b/node_modules/@remix-run/react/dist/esm/single-fetch.js
@@ -300,6 +300,8 @@ function singleFetchUrl(reqUrl) {
   let url = typeof reqUrl === "string" ? new URL(reqUrl, window.location.origin) : reqUrl;
   if (url.pathname === "/") {
     url.pathname = "_root.data";
+  } else if (url.pathname === window.__remixContext.basename) {
+    url.pathname = window.__remixContext.basename.concat("_root.data");
   } else {
     url.pathname = `${url.pathname.replace(/\/$/, "")}.data`;
   }
diff --git a/node_modules/@remix-run/server-runtime/dist/server.js b/node_modules/@remix-run/server-runtime/dist/server.js
index 7e639d9..f2af2db 100644
--- a/node_modules/@remix-run/server-runtime/dist/server.js
+++ b/node_modules/@remix-run/server-runtime/dist/server.js
@@ -128,7 +128,12 @@ const createRequestHandler = (build, mode$1) => {
       }
     } else if (_build.future.v3_singleFetch && url.pathname.endsWith(".data")) {
       let handlerUrl = new URL(request.url);
-      handlerUrl.pathname = handlerUrl.pathname.replace(/\.data$/, "").replace(/^\/_root$/, "/");
+      handlerUrl.pathname = handlerUrl.pathname.replace(/\.data$/, "").replace(
+        _build.basename ?
+          /\/_root$/ :
+          /^\/_root$/,
+        "/"
+      );
       let singleFetchMatches = routeMatching.matchServerRoutes(routes, handlerUrl.pathname, _build.basename);
       response = await handleSingleFetchRequest(serverMode, _build, staticHandler, request, handlerUrl, loadContext, handleError);
       if (_build.entry.module.handleDataRequest) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant