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

Vite plugin calls resolvePluginConfig multiple times per request #8369

Closed
kiliman opened this issue Dec 23, 2023 · 4 comments
Closed

Vite plugin calls resolvePluginConfig multiple times per request #8369

kiliman opened this issue Dec 23, 2023 · 4 comments

Comments

@kiliman
Copy link
Collaborator

kiliman commented Dec 23, 2023

Reproduction

https://github.com/kiliman/remix-resolve-plugin-config

This shows that resolvePluginConfig is being called multiple times per request.

System Info

System:
    OS: macOS 14.2.1
    CPU: (12) arm64 Apple M2 Max
    Memory: 5.25 GB / 96.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.6.1 - ~/.nvm/versions/node/v20.6.1/bin/node
    Yarn: 1.22.21 - ~/.nvm/versions/node/v20.6.1/bin/yarn
    npm: 10.2.3 - ~/.nvm/versions/node/v20.6.1/bin/npm
    pnpm: 8.12.1 - ~/.nvm/versions/node/v20.6.1/bin/pnpm
    bun: 1.0.12 - ~/.bun/bin/bun
    Watchman: 2023.11.13.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome: 120.0.6099.129
    Chrome Canary: 121.0.6110.0
    Safari: 17.2.1
  npmPackages:
    @remix-run/dev: ^2.4.1 => 2.4.1 
    @remix-run/node: ^2.4.1 => 2.4.1 
    @remix-run/react: ^2.4.1 => 2.4.1 
    @remix-run/serve: ^2.4.1 => 2.4.1 
    vite: ^5.0.0 => 5.0.10

Used Package Manager

npm

Expected Behavior

Remix should only call resolvePluginConfig once at startup and when config changes.

// vite.config.ts

import { unstable_vitePlugin as remix } from "@remix-run/dev";
import { defineConfig } from "vite";
import tsconfigPaths from "vite-tsconfig-paths";

export default defineConfig({
  plugins: [
    remix({
      routes: () => {
        // this should only be called on startup and when config changes
        // not multiple times per request
        console.log("routes", Date.now());
        return {};
      },
    }),
    tsconfigPaths(),
  ],
});

Actual Behavior

resolvePluginConfig is called multiple times per request.

The image below shows the initial call, but when I opened the home page, you'll see it called the function again four times in a row.

image
@hi-ogawa
Copy link
Contributor

hi-ogawa commented Dec 24, 2023

I think this is a somewhat known issue and probably considered not so critical (as initial perf improvement is done by #7908).
Do you have any concrete issue which is blocked by this behavior?

Currently resolvePluginConfig is called during middleware (and other places as well), which means it will be called at least once for each Remix server-render/loader-data request:

viteDevServer.middlewares.use(async (_req, _res, next) => {
try {
let pluginConfig = await resolvePluginConfig();


Expected Behavior
Remix should only call resolvePluginConfig once at startup and when config changes.

I thought such behavior would make sense, so I attempted such refactoring sometime ago #8164
Additionally, I think Remix also needs to call resolvePluginConfig when a route file is added/removed (but not when simply modified).

Technically route files can live anywhere since routes option can be customized, but for now, my implementation is checking any file addition/removal of appDirectory and modification of vite.config.ts, which I think is a fair assumption.

@kiliman
Copy link
Collaborator Author

kiliman commented Dec 24, 2023

For most apps it's probably not an issue. But if you're using custom routes that scan the file system, it could cause performance problems for projects with many routes.

@richaquino
Copy link

Do you have any concrete issue which is blocked by this behavior?

Unless there's a better way to setup test routes (for quickly adding things without forms, like users), this really slows down tests.

i.e. If the grunge stack was converted to use vite, it'll suffer a bit because it sets up a create-user test route in its config. https://github.com/remix-run/grunge-stack/blob/main/remix.config.js

@pcattori
Copy link
Contributor

Fixed by #8164

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

No branches or pull requests

5 participants