Skip to content

fix(legacy)!: remove location.protocol!="file:" condition for modern android webview #20179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 16, 2025

Conversation

maliut
Copy link
Contributor

@maliut maliut commented Jun 8, 2025

Description

I'm packaging a static website generated by Vite into an Android app. I used plugin-legacy to support older Android phones. However, in modern WebViews, this causes both modern chunks and legacy chunks to be loaded, resulting in unexpected behavior.

Reproduction Steps

  1. Create a new Vite project and add plugin-legacy
  2. In Vite config set base: './'
  3. Add a line console.log('App initializing...') in main.js to track its execution
  4. Create an Android project containing only a WebView
  5. Place the Vite build output in the Android project's assets folder
  6. Load this HTML file in the Android WebView mWebView.loadUrl("file:///android_asset/index.html");
  7. Run the Android project and check Logcat to see the console log printed twice — once from the modern chunk and once from the legacy chunk
[INFO:CONSOLE:33] "App initializing...", source: file:///android_asset/assets/index.C48xOSVJ.js (33)
[INFO:CONSOLE:19] "App initializing...", source: file:///android_asset/assets/index-legacy.CqIw3Dqg.js (19)

Root Cause

The index.html generated by Vite build contains the following code:

<script type="module" crossorigin src="./assets/index.C48xOSVJ.js"></script>
<link rel="stylesheet" crossorigin href="./assets/index.0pCfBZ4Q.css">
<script type="module">import.meta.url;import("_").catch(()=>1);(async function*(){})().next();if(location.protocol!="file:"){window.__vite_is_modern_browser=true}</script>
<script type="module">!function(){if(window.__vite_is_modern_browser)return;console.warn("vite: loading legacy chunks, syntax error above and the same error below should be ignored");var e=document.getElementById("vite-legacy-polyfill"),n=document.createElement("script");n.src=e.src,n.onload=function(){System.import(document.getElementById('vite-legacy-entry').getAttribute('data-src'))},document.body.appendChild(n)}();</script>

This means __vite_is_modern_browser is only set to true when location.protocol!="file:". As a result, even if the modern chunk loads successfully, the legacy chunk is still loaded.

Solution

This pull request essentially reverts #8524.

Determining whether a browser is modern should only depend on the browser's capabilities. There's no causal relationship between checking if the protocol is file: and whether the browser is modern.

I read the original pull request. Their core requirement was to use only legacy chunks (and skip modern chunks) in a specific scenario (in this case, when loading via the file: protocol). Now, plugin-legacy already has the renderModernChunks option to meet their needs.

@sapphi-red sapphi-red changed the title fix(plugin-legacy): legacy chunks loaded in modern android webview fix(legacy): legacy chunks loaded in modern android webview Jun 11, 2025
@sapphi-red sapphi-red added plugin: legacy p3-minor-bug An edge case that only affects very specific usage (priority) breaking change labels Jun 11, 2025
@sapphi-red sapphi-red changed the title fix(legacy): legacy chunks loaded in modern android webview fix(legacy)!: legacy chunks loaded in modern android webview Jun 11, 2025
@sapphi-red sapphi-red changed the title fix(legacy)!: legacy chunks loaded in modern android webview fix(legacy)!: remove location.protocol!="file:" condition for modern android webview Jun 11, 2025
Copy link
Member

@sapphi-red sapphi-red left a comment

Choose a reason for hiding this comment

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

Makes sense to me.

Could you add a line to the document that tells renderModernChunks is also useful for usecases like #8524?
https://github.com/vitejs/vite/tree/9f583025499c6f2810c9f8547b62d12c27675b75/packages/plugin-legacy#rendermodernchunks

@sapphi-red sapphi-red added this to the 7.0 milestone Jun 11, 2025
@maliut
Copy link
Contributor Author

maliut commented Jun 11, 2025

Thank you for review! I have updated the document. I'm not sure if my expression is accurate and concise enough, feel free to make any modifications!

Copy link
Member

@sapphi-red sapphi-red left a comment

Choose a reason for hiding this comment

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

Looks good to me 👍

@patak-dev patak-dev merged commit a6d5997 into vitejs:main Jun 16, 2025
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change p3-minor-bug An edge case that only affects very specific usage (priority) plugin: legacy
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants