Skip to content
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

Support BASE_URL environment variable v2.5.1 #3

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"scripts": {
"serve": "vite serve",
"build": "vite build && npm run build.zip",
"build:printnanny": "BASE_URL=/mainsail/ vite build --base=/mainsail/ && npm run build.zip",
"format": "npm run format:base -- --write",
"format:base": "prettier .",
"format:check": "npm run format:base -- --check",
Expand Down Expand Up @@ -116,4 +117,4 @@
"engines": {
"node": "^16 || ^18"
}
}
}
3 changes: 2 additions & 1 deletion src/components/TheSelectPrinterDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,8 @@ export default class TheSelectPrinterDialog extends Mixins(BaseMixin) {
hostname: printer.socket.hostname,
port: printer.socket.port,
})
this.$socket.setUrl(this.protocol + '://' + printer.socket.hostname + ':' + printer.socket.port + '/websocket')
const basePath = import.meta.env.BASE_URL || ''; // https://vitejs.dev/guide/build.html#public-base-path
this.$socket.setUrl(`${this.protocol}://${printer.socket.hostname}:${printer.socket.port}${basePath}/websocket`)
this.$socket.connect()
}

Expand Down
6 changes: 4 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { registerSW } from 'virtual:pwa-register'

// noinspection JSUnusedGlobalSymbols
registerSW({
onOfflineReady() {},
onOfflineReady() { },
})

Vue.config.productionTip = false
Expand Down Expand Up @@ -54,6 +54,7 @@ Vue.use(OverlayScrollbarsPlugin, {

// Directives
import './directives/longpress'

import './directives/responsive-class'

// Echarts
Expand All @@ -76,7 +77,8 @@ Vue.use(VueResize)

const initLoad = async () => {
//load config.json
await fetch('/config.json')
const basePath = import.meta.env.BASE_URL || ''; // https://vitejs.dev/guide/build.html#public-base-path
await fetch(`${basePath}/config.json`)
.then((res) => res.json())
.then(async (file) => {
window.console.debug('Loaded config.json')
Expand Down
9 changes: 5 additions & 4 deletions src/store/farm/printer/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { GuiWebcamStateWebcam } from '@/store/gui/webcams/types'
// eslint-disable-next-line
export const getters: GetterTree<FarmPrinterState, any> = {
getSocketUrl: (state) => {
return state.socket.protocol + '://' + state.socket.hostname + ':' + state.socket.port + '/websocket'
const basePath = import.meta.env.BASE_URL || ''; // https://vitejs.dev/guide/build.html#public-base-path
return `${state.socket.protocol}://${state.socket.hostname}:${state.socket.port}${basePath}/websocket`
},

getSocketData: (state) => {
Expand Down Expand Up @@ -149,8 +150,8 @@ export const getters: GetterTree<FarmPrinterState, any> = {
element.substr(0, element.lastIndexOf('.')) === themeDir + '/' + acceptName &&
acceptExtensions.includes(element.substr(element.lastIndexOf('.') + 1))
)

return file ? '//' + state.socket.hostname + ':' + state.socket.port + '/server/files/config/' + file : null
const basePath = import.meta.env.BASE_URL || ''; // https://vitejs.dev/guide/build.html#public-base-path
return file ? `//${state.socket.hostname}:${state.socket.port}${basePath}/server/files/config/` + file : null
},

getLogo: (state, getters) => {
Expand Down Expand Up @@ -259,7 +260,7 @@ export const getters: GetterTree<FarmPrinterState, any> = {
) {
return (
state.data.print_stats.print_duration /
(state.data.print_stats.filament_used / state.current_file.filament_total) -
(state.data.print_stats.filament_used / state.current_file.filament_total) -
state.data.print_stats.print_duration
).toFixed(0)
}
Expand Down
3 changes: 2 additions & 1 deletion src/store/socket/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ export const actions: ActionTree<SocketState, RootState> = {

if ('$socket' in Vue.prototype) {
await Vue.prototype.$socket.close()
const basePath = import.meta.env.BASE_URL || ''; // https://vitejs.dev/guide/build.html#public-base-path
await Vue.prototype.$socket.setUrl(
state.protocol + '://' + payload.hostname + ':' + payload.port + '/websocket'
`${state.protocol}://${payload.hostname}:${payload.port}${basePath}/websocket`
)
await Vue.prototype.$socket.connect()
}
Expand Down
12 changes: 11 additions & 1 deletion src/store/socket/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import { RootState } from '@/store/types'

export const getters: GetterTree<SocketState, RootState> = {
getUrl: (state) => {
const basePath = import.meta.env.BASE_URL || ''; // https://vitejs.dev/guide/build.html#public-base-path
const port = state.port === 80 ? '' : `:${state.port}`

const url = `//${state.hostname}${port}${basePath}`;
// strip trailing slash from BASE_URL in this getter, since most socket/getUrl consumers seem to assume no trailing slash is present
if (url[url.length - 1] === "/") {
return url.slice(0, -1)
}
return url

return '//' + state.hostname + (state.port !== 80 ? ':' + state.port : '')
},

Expand All @@ -12,6 +22,6 @@ export const getters: GetterTree<SocketState, RootState> = {
},

getWebsocketUrl: (state, getters) => {
return state.protocol + ':' + getters['getUrl'] + '/websocket'
return `${state.protocol}:${getters['getUrl']}/websocket`
},
}