-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Need a way to use static resources with router.base #4544
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
Comments
added an issue in the docs repo to document the It is by default set to true to move the
export default {
router: { base: '/t' }
} Accessing routes
Rewriting static content
Caveat: |
Thanks for taking care. For the record (and for possible future folks with the same problem), here's how we solved this issue in our project: Rewriting regular URLsFor CSS <img src="~static/a.jpg"> Rewriting dynamically created URLsFor cases where webpack cannot rewrite URLs due to dynamic data (e.g. <img :src="`${STATIC_PATH}/${filename}`"> Making const PRODUCTION_BASE_PATH = '/demo';
module.exports = {
// ...
// Set the router's base path
router: {
base: process.env.NODE_ENV === 'production'
? PRODUCTION_BASE_PATH
: '/'
},
// ...
// Rewrite all occurrences of STATIC_PATH
build: {
extend(config, { isDev }) {
config.plugins.push(new webpack.DefinePlugin({
STATIC_PATH: JSON.stringify(isDev ? '' : PRODUCTION_BASE_PATH)
}))
}
},
// ...
// Make STATIC_PATH available in Vue templates
plugins: [ '~/plugins/static-mixin.js' ],
// ...
}; This defines the global To make it available in Vue templates, we added the import Vue from 'vue'
Vue.mixin({
computed: {
STATIC_PATH: () => STATIC_PATH
}
}) |
@loilo Thanks for sharing! 👍 Small suggestion: You could use |
Just for reference, I've solved this by accessing the base option through the router like so, which seems to achieve the same thing:
|
Is there a way for dynamic routes to be cleanly handled by |
Hi, @/plugins/utils.js
adding this as a plugin in you nuxt.config.js like:
and using it in your page/components like:
Of course you can rename the injected function as you like. @altusgerona it could be useful for you |
Thanks @Kyserbyte. Will look into implementing this on future projects. I still hope that Nuxt can provide a cleaner solution for this. |
This Caveat is extremely important and is not documented here: https://nuxtjs.org/guides/directory-structure/static/#static-asset-prefix |
Vue 3, as I understand it, won't have mixins -- so the mixin method above won't work. We're using it now -- and it works fine -- but it looks like it's not future-proof. Is there a simple way to switch between the router base in (I'm using a router base -- i.e., |
Any way use this base in command line? I have different base href in different platform Angular has command |
Hi ,I want to use it in CSS like config
|
Same issue |
For some reason this error is only happening with css files... Any update on this? |
Seems that links inside the head() won't follow the router.base rule. Fixed by adding the path on them.
to
|
Any updates on this? |
Same issue here. My hole app keeps loading because I get a 404 on the _nuxt folder. Is there any solution? |
Same problem |
@manniL I created a minimal CodeSandbox. https://codesandbox.io/s/stoic-borg-d8t7ck?file=/nuxt.config.js
The link to the favicon doesn't get the |
I was encountering an identical issue, and was able to fix it by removing the leading slash from the favicon's href. That is, the head in
|
What problem does this feature solve?
Currently, there's no (documented) way to properly use static assets in sites deployed by Nuxt's
generate
command when therouter.base
coption is set to something other than/
. Resources requested as~/path/to/resource
are correctly rewritten to[router.base]/path/to/resource
. Unfortunately, there's no equivalent to that for static resources requested as/resource
.The real-world situation is simple:
http://localhost:3000/
.https://our-site.com/demo
, so in production it gets therouter.base
option set to/demo
.Therefore in local development all static resources should point to
/
whereas in production they should point to/demo/
.I have created a minimal repro that explains this by example, much better than I could do here in words.
To kickstart the repro, just run:
I believe this is the same issue that has been brought up in #2753.
It has been addressed with the introduction of the
render.static.prefix
option in #2755 which is however completely undocumented and it's unclear to me how it may solve the issue.What does the proposed changes look like?
If the
render.static.prefix
option is actually able to solve this (which I think it's meant to be), it should be documented.Otherwise, an additional
render.static.*
option could be introduced to control rewriting requests to static resources.The text was updated successfully, but these errors were encountered: