Serving legacy JavaScript separately with Vite
Configure Vite's legacy plugin for older browsers and WebViews, then validate the modern ES module and SystemJS assets in the built HTML.
A legacy WebView may need syntax transforms and polyfills that modern browsers can skip. I use Vite’s legacy plugin to ship compatible assets only to the browser classes that need them.
Choosing what the site has to support
Start with the required browser versions and an actual representative device or managed WebView. A lower syntax target alone does not supply missing browser APIs. A polyfill alone cannot make a browser parse unsupported syntax. Delivering both additions to all users increases network and parse work in browsers that could run the normal bundle.
Differential serving keeps the modern ESM entry and creates a separate legacy build. The legacy path loads its runtime and polyfills before the app entry. Check the behavior with a clean profile because a warm cache can hide an accidental double download.
How the HTML selects a bundle
sequenceDiagram
participant B as Browser
participant H as Built HTML
participant M as Modern ESM entry
participant L as Legacy assets
B->>H: Request page
H-->>B: Return script entries
alt Modern path supported
B->>M: Load native module
else Legacy path required
B->>L: Load polyfills and then the SystemJS entry
end
The HTML includes a module entry for modern browsers and scripts for the legacy path. Browsers that support the module loading features the plugin checks can use the modern entry. Older implementations can load the SystemJS legacy build. Some browsers recognize modules while lacking the more recent features a particular modern chunk requires, which is why the plugin includes compatibility detection in addition to script attributes.
Configuring targets and polyfills
Install the plugin and the minifier the example uses. Set target browsers with Browserslist and identify API polyfills that the application’s compatibility tests actually need.
1
2
3
# Install the build dependencies
pnpm add -D @vitejs/plugin-legacy
pnpm add -D terser # Install Terser for legacy chunk minification
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// vite.config.ts
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import legacy from '@vitejs/plugin-legacy';
export default defineConfig({
plugins: [
react(),
legacy({
// 1. Supported browser targets in Browserslist syntax
targets: ['defaults', 'not IE 11', 'chrome >= 64', 'ios >= 12'],
// 2. Explicit polyfills for the legacy bundle
polyfills: [
'es.promise.finally',
'es.array.flat-map',
'es.object.from-entries',
],
// 3. Add modern polyfills only when required
modernPolyfills: false,
// 4. Emit modern chunks alongside legacy output
renderModernChunks: true,
}),
],
build: {
// Example modern build target. Check the installed plugin's target handling.
target: 'es2020',
},
});
The example leaves the modern polyfill list off. Keep that choice only if your modern browser floor provides the APIs the code uses. Check how your installed plugin version applies modern targets and build settings, especially when other plugins change the output.
Handling early Safari module behavior
Safari 10.1 is a historical case where module support and nomodule behavior did not line up. An affected browser could request both entries. The plugin’s generated HTML includes compatibility handling. The excerpt shows the shape of the workaround. Use the generated script for the plugin version installed in the project.
1
2
3
4
5
6
7
8
9
10
11
12
13
<script nomodule>
!function(){
var e=document,t=e.createElement("script");
if(!("noModule"in t)&&"onbeforeload"in t){
var n=!1;
e.addEventListener("beforeload",function(e){
if(e.target===t)n=!0;else if(!e.target.hasAttribute("nomodule")||!n)return;
e.preventDefault()
},!0);
t.type="module";t.src=".";e.head.appendChild(t);t.remove()
}
}();
</script>
Inspecting a production build
Run the build and check that the modern entry, legacy entry, and legacy polyfill asset are separate. The recorded output for this example showed 124.50 kB of modern code, 168.40 kB for the legacy entry, and 68.20 kB for the legacy polyfills before gzip.
Figure 1. The production build emits distinct entries and a legacy polyfill bundle.
Figure 2. The built HTML shows the modern module entry and generated legacy scripts.
Serve the built HTML with the same Content Security Policy and base path used in production. The plugin can add inline scripts, so verify that policy and script execution together. Test one current browser and one actual supported legacy browser with the cache cleared. Confirm that each runs the intended bundle, then test behavior that depends on the added polyfills.