Skip to content

fix!: only serve dev static assets from publish dir as fallback#508

Open
Copilot wants to merge 5 commits intomainfrom
copilot/fix-vite-serve-dist-issue
Open

fix!: only serve dev static assets from publish dir as fallback#508
Copilot wants to merge 5 commits intomainfrom
copilot/fix-vite-serve-dist-issue

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.

Changes

  • Updated implementation to use only custom directories when provided
  • Added comprehensive tests
  • Documented the default behavior with @default JSDoc tag

💡 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 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

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
@serhalp
Copy link
Member

serhalp commented Feb 12, 2026

Note: this PR is ready as is. I just wanted to do some local testing to verify the fix (for #513 as well at the same time). I'll get to it eventually, so I'm keeping these open.

pieh
pieh previously approved these changes Feb 12, 2026
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot document the default here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the documentation to use @default JSDoc tag and more precisely describe the fallback behavior: build.publish from netlify.toml if configured, otherwise projectRoot.

Commit: 23e4a66

Updated the JSDoc comment for the staticFiles.directories option to use
the @default tag and more precisely describe the fallback behavior:
- Uses build.publish from netlify.toml if configured
- Otherwise falls back to projectRoot

This addresses the review comment to document the default more clearly.

Co-authored-by: serhalp <1377702+serhalp@users.noreply.github.com>
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

3 participants

Comments