Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 30, 2025

Fixes #436

Problem

When using @netlify/vite-plugin, vite dev (or e.g. astro dev) was incorrectly serving files from the Netlify publish dir (e.g. dist/) instead of the source directory during vite dev.

This causes various problems, the most basic of which is that if you've ever previously run vite build (or e.g. `astro build) your dev server scans the production build output dir and serves assets from it. This is confusing and incorrect.

Solution

I can't think of any reason to ever augment the directories explicitly passed to @netlify/dev's options with a default directory. In every known use case (@netlify/vite-plugin, @netlify/nuxt...), we pass the known, exhaustive set of directories that should be used to serve static assets in dev, so we should respect these as is.

It seems reasonable to use some sensible defaults here when not provided (for hypothetical future or direct use cases).

This PR makes this change.

Original prompt

Issue Title: Using @vite/netlify-plugin causes Vite to serve dist instead of usual vite dev behaviour
Issue Description: Hi, I've noticed a very odd issue with your plugin. It appears @netlify/vite-plugin alters Vite's regular vite dev behaviour from serving transformed source to instead serving the dist folder, just like vite preview.

Here's a reproduction:
https://github.com/alextompkins/netlify-vite-plugin-serve-dist-issue
Fixes https://linear.app/netlify/issue/FRB-1969/using-vitenetlify-plugin-causes-vite-to-serve-dist-instead-of-usual

Comment by User db2967f5-c49c-428b-ae14-6c61855027c1:
netlify/primitives

Comment by User c8ba5c18-8b2b-41c2-8484-9ca4ad793475:
📋 I wasn't able to determine which GitHub repository to work in.

Please tell me which repository this work should be done in using the format owner/repository-name (e.g., github/copilot), then I'll automatically start working on it!

Comment by User c8ba5c18-8b2b-41c2-8484-9ca4ad793475:
Waiting for https://linear.app/netlify/profiles/serhalp to link their GitHub account. Click to authorize →

Comment by User :
This thread is for an agent session with githubcopilot.

Comment by User :
This comment thread is synced to a corresponding GitHub issue. All replies are displayed in both locations.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Fix Vite serving dist folder instead of source during dev Fix vite dev serving dist instead of source with SPA redirects Oct 30, 2025
Copilot finished work on behalf of serhalp October 30, 2025 15:29
Copilot AI requested a review from serhalp October 30, 2025 15:29
@serhalp
Copy link
Member

serhalp commented Nov 4, 2025

@copilot what does this have to do with SPAs and redirects? did you cross wires with another issue?

also, can you compare this with the behaviour implemented in https://github.com/netlify/cli/? when does it use static files in the publish dir during dev?

Copy link
Contributor Author

Copilot AI commented Nov 4, 2025

@copilot what does this have to do with SPAs and redirects? did you cross wires with another issue?

also, can you compare this with the behaviour implemented in https://github.com/netlify/cli/? whe...

The SPA redirect is directly related to the issue. Here's how:

  1. The reproduction has a SPA-style rewrite rule: /* -> /index.html (status 200)
  2. When a request comes in, the redirect handler matches it and rewrites to /index.html
  3. The redirect handler then looks for /index.html in the static directories
  4. With the old code, static directories were [dist, projectRoot, publicDir], so dist/index.html was found first
  5. The middleware sees result.type === 'redirect' (not 'static'), so it serves the response directly (line 117-120 in main.ts)
  6. This causes the built dist/index.html to be served instead of the source index.html transformed by Vite

Without the SPA redirect, the issue would still exist but be less obvious - direct requests for files that exist in both dist and source would serve from dist.

Regarding comparison with Netlify CLI: The CLI doesn't integrate with Vite's dev server, so it doesn't have this conflict. During vite dev, we explicitly want to serve from source directories only (projectRoot + publicDir), not from the build output. The fix ensures that when vite-plugin provides custom directories, those are used exclusively. When @netlify/dev is used standalone (no custom directories), it still falls back to the original behavior of using build.publish.

Commit: 71ed852

Copilot finished work on behalf of serhalp November 4, 2025 14:19
When using `@netlify/vite-plugin`, `vite dev` (or e.g. `astro dev`) was incorrectly serving files
from the Netlify publish dir (e.g. `dist/`) instead of the source directory during vite dev.

I can't think of any reason to ever augment the directories passed to `@netlify/dev`'s options with
the default directory. In every known use case (`@netlify/vite-plugin`, `@netlify/nuxt`...), we pass
a known set of directories that should be used to serve static assets in dev. It seems reasonable to
use some reasonable defaults here for future use cases.

This commit makes this change.

Co-authored-by: serhalp <1377702+serhalp@users.noreply.github.com>
@serhalp serhalp force-pushed the copilot/fix-vite-serve-dist-issue branch from 71ed852 to 3a6c743 Compare November 4, 2025 22:09
@serhalp serhalp changed the title Fix vite dev serving dist instead of source with SPA redirects fix!: only serve dev static assets from publish dir as fallback Nov 4, 2025
@serhalp serhalp marked this pull request as ready for review November 5, 2025 13:22
@serhalp serhalp requested a review from a team as a code owner November 5, 2025 13:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using @vite/netlify-plugin causes Vite to serve dist instead of usual vite dev behaviour

2 participants