-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Expose history api fallback options #7095
Comments
The alternative seems to be similar to #5720, but I'm curious if That should allow you to inject the extra scripts into
I don't understand what you mean here. Do you mean preventing |
We need The qiankun's wrapper html will execute logic like: // In this fetch we need vite's dev server to serve original index.html
const html = await fetch('/index.html').then(r => r.text());
const subAppDocument = parsetHtml(html);
subAppDocument.head.children.forEach(v => document.head.appendChild(v));
const container = document.getElementById('root');
container.empty();
container.appendChild(subAPpDocument.body.firstChild); |
I see. Thanks for the explanation! Besides the alternative mentioned in #5720, I wonder if a approach like:
Would work? 🤔 Hoping to reduce the need to introduce new options as Vite tries to avoid that as much as possible. |
I didn't find a way to resolve import fs from 'fs/promises';
import { defineConfig, Plugin } from 'vite'
import react from '@vitejs/plugin-react'
function setup(): Plugin {
return {
name: 'custom',
transformIndexHtml(html) {
return 'Hello WOrld';
},
resolveId(id) {
console.log('resolve', id);
if (id === '/custom.html') {
return id;
}
},
load(id) {
console.log('load', id);
return fs.readFile('index.html', 'utf-8');
}
};
}
// https://vitejs.dev/config/
export default defineConfig({
plugins: [setup(), react()]
}) When visiting Also I tried to add transformIndexHtml(html) {
return `
<!DOCTYPE html>
<html
<body>
<script type="module">
import('./custom.html');
</script>
</body>
</html>
`
},
async load(id) {
console.log('load', id);
const loaded = await this.load({id: '/index.html'});
return loaded;
}, This time vite complains about |
After some deeper investigation, I believe qiankun won't be able to support vite's dev mode at all, it's core |
FWIW I've been testing out the plugin too and it doesn't seem possible unfortunately. Looks like loading |
@bluwy You can refer to my custom plugin as an implement of hijacking |
Neat! Thanks for figuring out the plugin flow, that would be really helpful in the future. Do you think this feature request is still needed given the plugin solution? |
I for one would love to see a built-in solution for this. I came up with a very different plugin approach (see below), which seems to mostly work, though not perfectly[1]. Rationale for why this seems important to build into vite core: Vite seems to take the stance that:
However, for static file servers:
Therefore, it doesn't make much sense that Vite's dev server has this fallback behavior when it targets production environments that don't have it. It would be nice if there were a "correct" (that is, well tested) way to turn off the fallback behavior while keeping the rest of the serving behavior (HTML transformation, serving the right headers, etc). Update: I published the package vite-plugin-no-fallback to address this issue. [1] The plugin above seems to have broken the ability to use a <html>
…
<script type="module">
import Vue from 'vue';
import App from './App.vue';
new Vue({
render: (h) => h(App),
}).$mount('#app');
</script>
…
</html> |
@sapphi-red Thanks for linking here. I'm having a similar issue. I'd like to nest all my entry points including root index.html in /src/pages. I'm having trouble with both; getting rid of the trailing slash for nested pages and getting the root index.html to work in /src/pages. Tried everything in the docs and still no luck. |
Vite 3 can now officially support this with |
Amazing, thanks for sharing |
Clear and concise description of the problem
We're setting up a project simulating micro-frontend like qiankun in development, for this to work we need to respond index as a special "wrapper" html with qiankun runtime and a startup script pulling actual project, just like:
(This is only an example, actually we bundle qiankun runtime and entry script with esbuild.)
We then need to modify dev server's route into:
/__qiankun_wrapper__.html
which responds content described above/__local_qiankun_bundle__.js
responds a bundled version of qiankun/index.html
to serve as what vite did notWithout the ability to customize
historyApiFallback
middleware, it is not possible to setup a server exactly do these staffs, andhistoryApiFallback
is a special middleware that we cannot add another one before or after vite's middleware array.Suggested solution
Expose
server.apiHistoryFallbackOptions
to allow user customize this behavior.Alternative
Another solution is to allow a
server.historyApiFallback: false
option to intentionally disable history api fallback behavior, users can then add this middleware on demand.Additional context
No response
Validations
The text was updated successfully, but these errors were encountered: