Is there any way to avoid setting a public path in quasar config path? Just use the…
- User
Is there any way to avoid setting a public path in quasar config path? Just use the location deployed?
- Assistant
Is there any way to avoid setting a public path in quasar config path? Just use the location deployed?
As of my last update in April 2023, Quasar Framework, a Vue.js framework, typically requires specifying paths in its configuration for various purposes including deployment, building, and development settings. Public paths are particularly relevant for specifying the base path for your application when it's deployed. This is configured in the quasar.config.js file within the build property:
// quasar.config.js
module.exports = function (/* ctx */) {
return {
// ...
build: {
publicPath: 'my-app/',
// other build options...
},
// other Quasar options...
}
}
The publicPath is crucial when your app is not served from the root of your domain. For example, if your app is accessible from https://example.com/my-app/ rather than https://example.com/, you'd set the public path to 'my-app/'.
However, if you want your application to dynamically use the location where it's deployed (meaning you want it to detect the base URL or path and adjust accordingly), you might aim to avoid hardcoding this path in quasar.config.js.
To dynamically set the public path based on the deployment location, you can leave publicPath set to its default (''), which is designed for deployment at the domain root. Then, adjust the base path at runtime using a JavaScript snippet that uses __webpack_public_path__ in your entry file, often src/boot/* file or directly in src/index.template.html for Quasar projects:
In a boot file (e.g., src/boot/dynamic-public-path.js):
// Automatically adjust the webpack public path at runtime based on the location where it's served.
__webpack_public_path__ = window.location.pathname;
// To handle subdirectory correctly, you might need to use:
// __webpack_public_path__ = window.location.pathname + '/';
Make sure to include this boot file in your quasar.config.js:
// quasar.config.js
module.exports = function (/* ctx */) {
return {
boot: [
'dynamic-public-path',
// other boot files...
],
// Build, etc...
}
}
This method allows you to deploy the Quasar app to different paths without changing the quasar.config.js for each deployment environment, making your build process more flexible.