Skip to content

Commit

Permalink
support BASE_URL environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
leigh-johnson committed May 5, 2023
1 parent 8a199a0 commit b7eb6ec
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 11 deletions.
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
8 changes: 5 additions & 3 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 @@ -53,7 +53,8 @@ Vue.use(OverlayScrollbarsPlugin, {
})

// Directives
import './directives/longpress'
import './directives/longpress' "build:printnanny": "BASE_URL=/mainsail/ vite build --base=/mainsail/ && npm run build.zip",

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`
},
}

0 comments on commit b7eb6ec

Please sign in to comment.