You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This RFC proposes officially exporting Astro's internal vite-plugin-astro-preview to enable advanced vite preview setups, like enabling HTTPS, while preserving Astro's specific static routing logic.
Background & Motivation
As seen in past issues (#12880), users have found it difficult to enable HTTPS for the local static preview server. This problem stems from an inconsistency in how Astro commands handle Vite configuration. While astro dev correctly merge with a user's vite.config.js, allowing plugins like vite-plugin-mkcert to provide HTTPS, the astro preview command intentionally ignores this configuration. This creates a confusing developer experience and blocks advanced customizations, forcing users who require HTTPS for previewing their static build to find complex workarounds.
While the simplicity of astro preview is a valuable feature for most use cases, this limitation creates a significant hurdle for testing modern web features that require a secure context (e.g., some Service Workers, Web Authentication APIs). Currently, developers are forced to choose between:
Using astro preview only in HTTP mode and getting HTTPS in astro dev.
Using vite preview with HTTPS, which abandons Astro's "clean URL" routing and brings inconsistency with astro dev (e.g., /nested/page fails to resolve to /nested/page/index.html due to vite's default behavior).
This RFC proposes a solution that maintains the simplicity of the default command while providing a clear, officially supported "escape hatch" for users who need more control.
Provide a supported, official method for developers to enable HTTPS on the local static preview server.
Resolve the inconsistency where astro dev respects advanced Vite configurations but astro preview does not.
Offer a clear "escape hatch" for advanced preview server customization without complicating the default astro preview command.
Ensure developers can use vite preview directly without losing Astro's specific static routing logic.
Example
This RFC proposes officially exporting the internal vite-plugin-astro-preview via the astro package's exports map. This would enable developers to create a custom vite.config.js for advanced previews.
A user wanting to enable HTTPS could then use the following configuration and run vite preview:
vite.config.js
import{vitePluginAstroPreview}from'astro/internal/vite-plugin-astro-preview';import{defineConfig}from'vite';importmkcertfrom'vite-plugin-mkcert';// This config enables a preview server with both HTTPS and correct Astro routing.exportdefaultdefineConfig({plugins: [mkcert(),vitePluginAstroPreview({config: {outDir: newURL('./dist/',import.meta.url),base: '/',trailingSlash: 'ignore',},}),],preview: {https: true,},});
Deeper Analysis & Implementation Details
Looking at the Astro source code it's clear the fix is quite simple.
The Problem: configFile: false
The astro preview command programmatically creates a Vite server and explicitly disables loading any user configuration.
// ...previewServer=awaitpreview({configFile: false,// root cause, by designbase: settings.config.base,// ... other hardcoded optionsplugins: [vitePluginAstroPreview(settings)],// Only Astro's internal plugin is used});// ...
Effect: This design choice prevents Vite from discovering vite-plugin-mkcert or respecting the preview.https setting, making HTTPS impossible through the standard astro preview command.
The Solution: Expose the Internal Plugin
The logic for Astro's static routing already exists in vitePluginAstroPreview. The only change required is to make this plugin accessible to users. This can be done with a one-line change in Astro's package.json. Export path is a subject to change.
This change is non-breaking and simply exposes an internal tool that empowers developers to solve this problem themselves, aligning perfectly with the goal of providing a powerful escape hatch while keeping the default command simple.
It should be noted that docs should provide devs a clear path for astro to vite fallback in case of advanced (or not so advanced as in this case) static serving requirements. vite-plugin-mkcert has over 1 mil. monthly downloads which shows a clear demand for the feature overall.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
This RFC proposes officially exporting Astro's internal
vite-plugin-astro-previewto enable advanced vite preview setups, like enabling HTTPS, while preserving Astro's specific static routing logic.Background & Motivation
As seen in past issues (#12880), users have found it difficult to enable HTTPS for the local static preview server. This problem stems from an inconsistency in how Astro commands handle Vite configuration. While
astro devcorrectly merge with a user'svite.config.js, allowing plugins likevite-plugin-mkcertto provide HTTPS, theastro previewcommand intentionally ignores this configuration. This creates a confusing developer experience and blocks advanced customizations, forcing users who require HTTPS for previewing their static build to find complex workarounds.While the simplicity of
astro previewis a valuable feature for most use cases, this limitation creates a significant hurdle for testing modern web features that require a secure context (e.g., some Service Workers, Web Authentication APIs). Currently, developers are forced to choose between:astro previewonly in HTTP mode and getting HTTPS inastro dev.vite previewwith HTTPS, which abandons Astro's "clean URL" routing and brings inconsistency withastro dev(e.g.,/nested/pagefails to resolve to/nested/page/index.htmldue to vite's default behavior).This RFC proposes a solution that maintains the simplicity of the default command while providing a clear, officially supported "escape hatch" for users who need more control.
A working mvp demonstrating this issue and the proposed solution is available here: https://github.com/SanariSan/astro-expose-vite-preview-plugin
Goals
astro devrespects advanced Vite configurations butastro previewdoes not.astro previewcommand.vite previewdirectly without losing Astro's specific static routing logic.Example
This RFC proposes officially exporting the internal
vite-plugin-astro-previewvia theastropackage'sexportsmap. This would enable developers to create a customvite.config.jsfor advanced previews.A user wanting to enable HTTPS could then use the following configuration and run
vite preview:vite.config.jsDeeper Analysis & Implementation Details
Looking at the Astro source code it's clear the fix is quite simple.
The Problem:
configFile: falseThe
astro previewcommand programmatically creates a Vite server and explicitly disables loading any user configuration.File:
packages/astro/src/core/preview/static-preview-server.tsEffect: This design choice prevents Vite from discovering
vite-plugin-mkcertor respecting thepreview.httpssetting, making HTTPS impossible through the standardastro previewcommand.The Solution: Expose the Internal Plugin
The logic for Astro's static routing already exists in
vitePluginAstroPreview. The only change required is to make this plugin accessible to users. This can be done with a one-line change in Astro'spackage.json. Export path is a subject to change.Proposed Change:
packages/astro/package.jsonThis change is non-breaking and simply exposes an internal tool that empowers developers to solve this problem themselves, aligning perfectly with the goal of providing a powerful escape hatch while keeping the default command simple.
It should be noted that docs should provide devs a clear path for
astrotovitefallback in case of advanced (or not so advanced as in this case) static serving requirements.vite-plugin-mkcerthas over 1 mil. monthly downloads which shows a clear demand for the feature overall.Beta Was this translation helpful? Give feedback.
All reactions