Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 2, 2025

The Netlify vite plugin now supports vite preview mode by implementing the configurePreviewServer hook, providing the same Netlify environment emulation during preview as it does during development.

Problem

Previously, the plugin only worked with vite dev through the configureServer hook. Users running vite preview to test their production builds locally would not get Netlify's environment features like functions, redirects, headers, and other platform-specific functionality.

Solution

Added configurePreviewServer hook that mirrors the functionality of configureServer:

const plugin: vite.Plugin = {
  name: 'vite-plugin-netlify',
  async configureServer(viteDevServer) {
    await setupNetlifyEnvironment(viteDevServer, options)
  },
  async configurePreviewServer(vitePreviewServer) {
    await setupNetlifyEnvironment(vitePreviewServer, options)  // Same setup!
  },
}

Key Changes

  • New Hook: Implemented configurePreviewServer following Vite's plugin API
  • Code Reuse: Extracted shared setupNetlifyEnvironment() function to eliminate duplication between dev and preview modes
  • Test Coverage: Added comprehensive unit tests covering hook existence, functionality, and error handling
  • Manual Verification: Confirmed the plugin works with vite preview, showing Netlify environment messages

Benefits

  • Users can now run vite preview and get full Netlify environment emulation
  • No breaking changes to existing vite dev functionality
  • Maintains code quality through shared implementation
  • Same feature set available in both development and preview modes

Testing

# Both commands now provide Netlify environment
npm run dev     # ✅ Works (existing)
npm run preview # ✅ Works (new!)

The plugin now logs the same "Environment loaded" and feature messages during preview mode, confirming that functions, redirects, headers, and other Netlify features are properly emulated.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits September 2, 2025 22:20
Co-authored-by: serhalp <1377702+serhalp@users.noreply.github.com>
Co-authored-by: serhalp <1377702+serhalp@users.noreply.github.com>
@serhalp serhalp changed the title [WIP] in https://github.com/netlify/primitives/, there's a vite plugin in packages/vite-plugin/. it only uses a dev server hook. add support for vite preview by using the configurePreviewServer hook (https://vite.dev/guide/api-plugin.html#configurepreviews... feat(vite-plugin): add support for vite preview Sep 2, 2025
@Copilot Copilot AI changed the title feat(vite-plugin): add support for vite preview Add configurePreviewServer support to vite-plugin for vite preview mode Sep 2, 2025
@Copilot Copilot AI requested a review from serhalp September 2, 2025 22:34
Copilot finished work on behalf of serhalp September 2, 2025 22:34
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.

2 participants