Skip to content

Commit

Permalink
Fix Vite client route requests with pre-processed search params (#8740)
Browse files Browse the repository at this point in the history
Co-authored-by: Mark Dalgleish <mark.john.dalgleish@gmail.com>
  • Loading branch information
cythrawll and markdalgleish authored Feb 13, 2024
1 parent dd1c09b commit d284e52
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/vite-whatwg-querystring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/dev": minor
---

Vite: Fix issue where client route file requests fail if search params have been parsed and serialized before reaching the Remix Vite plugin
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
- crismali
- CSFlorin
- cysp
- cythrawll
- d4vsanchez
- dabdine
- daganomri
Expand Down
17 changes: 14 additions & 3 deletions packages/remix-dev/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,13 @@ const getHash = (source: BinaryLike, maxLength?: number): string => {
return typeof maxLength === "number" ? hash.slice(0, maxLength) : hash;
};

const isClientRoute = (id: string): boolean => {
return (
id.endsWith(CLIENT_ROUTE_QUERY_STRING) ||
id.endsWith(`${CLIENT_ROUTE_QUERY_STRING}=`) // Needed in case url gets preprocessed by any WHATWG searchParam serializer
);
};

const resolveChunk = (
ctx: RemixPluginContext,
viteManifest: Vite.Manifest,
Expand Down Expand Up @@ -1090,8 +1097,12 @@ export const remixVitePlugin: RemixVitePlugin = (remixUserConfig = {}) => {
cssModulesManifest[id] = code;
}

if (id.endsWith(CLIENT_ROUTE_QUERY_STRING)) {
if (isClientRoute(id)) {
let routeModuleId = id.replace(CLIENT_ROUTE_QUERY_STRING, "");
routeModuleId = routeModuleId.replace(
`${CLIENT_ROUTE_QUERY_STRING}=`,
""
);
let sourceExports = await getRouteModuleExports(
viteChildCompiler,
ctx,
Expand Down Expand Up @@ -1520,7 +1531,7 @@ export const remixVitePlugin: RemixVitePlugin = (remixUserConfig = {}) => {
let useFastRefresh = !ssr && (isJSX || code.includes(devRuntime));
if (!useFastRefresh) return;

if (id.endsWith(CLIENT_ROUTE_QUERY_STRING)) {
if (isClientRoute(id)) {
return { code: addRefreshWrapper(ctx.remixConfig, code, id) };
}

Expand Down Expand Up @@ -1607,7 +1618,7 @@ function addRefreshWrapper(
): string {
let route = getRoute(remixConfig, id);
let acceptExports =
route || id.endsWith(CLIENT_ROUTE_QUERY_STRING)
route || isClientRoute(id)
? [
"clientAction",
"clientLoader",
Expand Down

0 comments on commit d284e52

Please sign in to comment.