fix(web): preserve query in subpath redirects
This commit is contained in:
4 files changed
+233
-21
No files matched your search
@@ -2,6 +2,7 @@ import { defineConfig } from "vite";
|
||||
import vue from "@vitejs/plugin-vue";
|
||||
import tailwindcss from "@tailwindcss/vite";
|
||||
import path from "path";
|
||||
import { publicBasePathRedirectPlugin } from "./vitePublicBasePathRedirect";
|
||||
|
||||
const host = process.env.TAURI_DEV_HOST;
|
||||
const isTauri = !!host || !!process.env.TAURI_ENV_ARCH;
|
||||
@@ -63,7 +64,7 @@ const backendUrl = process.env.DBX_BACKEND_URL || "http://localhost:4224";
|
||||
export default defineConfig(async () => ({
|
||||
root: import.meta.dirname,
|
||||
base: viteBase,
|
||||
plugins: [vue(), tailwindcss()],
|
||||
plugins: [publicBasePathRedirectPlugin(publicBasePath), vue(), tailwindcss()],
|
||||
resolve: {
|
||||
alias: {
|
||||
"@": path.resolve(import.meta.dirname, "./src"),
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import type { Connect, Plugin } from "vite";
|
||||
|
||||
export function publicBasePathRedirectLocation(requestUrl: string | undefined, publicBasePath: string): string | null {
|
||||
if (!requestUrl || !publicBasePath || publicBasePath === "/") return null;
|
||||
|
||||
const queryStart = requestUrl.indexOf("?");
|
||||
const requestPath = queryStart >= 0 ? requestUrl.slice(0, queryStart) : requestUrl;
|
||||
if (requestPath !== publicBasePath) return null;
|
||||
|
||||
const query = queryStart >= 0 ? requestUrl.slice(queryStart) : "";
|
||||
return `${publicBasePath}/${query}`;
|
||||
}
|
||||
|
||||
export function publicBasePathRedirectMiddleware(publicBasePath: string): Connect.NextHandleFunction {
|
||||
return (request, response, next) => {
|
||||
const location = publicBasePathRedirectLocation(request.url, publicBasePath);
|
||||
if (!location) {
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
response.statusCode = 308;
|
||||
response.setHeader("Location", location);
|
||||
response.end();
|
||||
};
|
||||
}
|
||||
|
||||
export function publicBasePathRedirectPlugin(publicBasePath: string): Plugin {
|
||||
return {
|
||||
name: "dbx-public-base-path-redirect",
|
||||
configureServer(server) {
|
||||
server.middlewares.use(publicBasePathRedirectMiddleware(publicBasePath));
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user