Skip to content

Commit

Permalink
fix(vite): Use default base string if no other path detected
Browse files Browse the repository at this point in the history
* Update Vite dep
* Use inlineViteConfig for vite-express middleware and detect correct base string
* Refactor vite.config.ts to detect correct base string
  • Loading branch information
FoxxMD committed Jun 5, 2024
1 parent 09969dd commit 1c84a74
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"tailwindcss": "^3.3.3",
"toad-scheduler": "^3.0.0",
"tsx": "^4.7.0",
"vite-express": "^0.13.0",
"vite-express": "^0.16.0",
"xml2js": "0.5.0",
"youtube-music-ts-api": "^1.7.0"
},
Expand Down Expand Up @@ -148,7 +148,7 @@
"typescript": "^5.3.3",
"typescript-eslint": "^7.0.1",
"typescript-json-schema": "~0.55",
"vite": "^5.0.11"
"vite": "^5.2.12"
},
"browserslist": {
"production": [
Expand Down
8 changes: 7 additions & 1 deletion src/backend/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@ export const initServer = async (parentLogger: Logger, appLoggerStream: PassThro
if(process.env.USE_HASH_ROUTER === undefined) {
process.env.USE_HASH_ROUTER = root.get('isSubPath');
}
ViteExpress.config({mode: isProd ? 'production' : 'development'});

ViteExpress.config({
mode: isProd ? 'production' : 'development',
inlineViteConfig: {
base: localDefined && local.pathname !== '/' ? local.toString() : '/'
}
});
try {
ViteExpress.listen(app, port, () => {
const start = stripIndents`\n
Expand Down
26 changes: 24 additions & 2 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import normalizeUrl from "normalize-url";
import { defineConfig } from 'vite';

export const generateBaseURL = (userUrl: string | undefined): URL => {
const base = normalizeUrl(userUrl, {removeSingleSlash: true});
const u = new URL(base);
if(u.port === '') {
if(u.protocol === 'https:') {
u.port = '443';
} else if(userUrl.includes(`${u.hostname}:80`)) {
u.port = '80';
}
}
return u;
}

export default defineConfig(() => {
let baseUrlStr = '/';
if(process.env.BASE_URL !== undefined && process.env.BASE_URL !== '') {
const baseUrl = generateBaseURL(process.env.BASE_URL);
if(baseUrl.pathname !== '/') {
baseUrlStr = baseUrl.toString();
}
}
console.debug(`[VITE] BASE_URL ENV: ${process.env.BASE_URL} | Base Url String: ${baseUrlStr}`);
return {
base: (process.env.BASE_URL ?? '/'),
base: baseUrlStr,
plugins: [react()],
build: {
sourcemap: true
Expand Down

0 comments on commit 1c84a74

Please sign in to comment.