N
Nuxt7d ago
Snoozie

change router history base url

Hello, is there a way to change the base url of the router history ? I'd like to be able to do like in this vue project
No description
27 Replies
kapa.ai
kapa.ai7d ago
Hi @Snoozie. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Yes, you can change the base URL of the router history in Nuxt. In Nuxt 3, this is done using the app.baseURL configuration option in your nuxt.config.ts file. Here's how to set it up:
export default defineNuxtConfig({
app: {
baseURL: '/app/'
}
})
export default defineNuxtConfig({
app: {
baseURL: '/app/'
}
})
This will prefix all your routes with /app/, so a route like /about would become /app/about. If you're using Nuxt 2, the equivalent configuration would be:
export default {
router: {
base: '/app/'
}
}
export default {
router: {
base: '/app/'
}
}
When migrating from Nuxt 2 to Nuxt 3 (or using Nuxt Bridge), you would need to change from router.base to app.baseURL as shown in the Bridge documentation. You can also set this value at runtime using an environment variable:
NUXT_APP_BASE_URL=/app/ node .output/server/index.mjs
NUXT_APP_BASE_URL=/app/ node .output/server/index.mjs
This is particularly useful for deployment scenarios where you might need different base URLs for different environments. Note that when using a custom base URL, all your assets and links will be prefixed with this path, including the _nuxt directory where built assets are stored. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai7d ago
<@414109446810370049> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Snoozie
SnoozieOP7d ago
@kapa.ai it doesn't work in my project, maybe because I have a specific nitro configuration
kapa.ai
kapa.ai7d ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- I understand you're having issues with the app.baseURL configuration in your project. This could indeed be related to your Nitro configuration or other specific settings in your project. Based on the knowledge sources, there are a few things to check: 1. Environment Variables: Check if you have any environment variables that might be overriding your configuration. As one user discovered in a GitHub issue, having NUXT_APP_BASE_URL= in their .env file was overriding their baseURL setting in nuxt.config.ts Link to GitHub Issue. 2. Nitro Configuration: The Nitro runtime config can affect how the base URL is applied. You might need to check your Nitro configuration, especially if you're using custom settings:
nitro: {
runtimeConfig: {
app: {
baseURL: '/your-path/'
}
}
}

nitro: {
runtimeConfig: {
app: {
baseURL: '/your-path/'
}
}
}

3. Static Site Generation: If you're generating a static site, there are specific considerations. For example, one user found that when using npm run generate, the assets paths weren't correctly prefixed with the base URL Link to GitHub Issue. 4. SSR Setting: Your SSR configuration might affect how the base URL is applied. If you're using ssr: false for a static build, this could interact with the base URL behavior. 5. Compatibility Date: In some cases, setting a recent compatibility date might help:
export default defineNuxtConfig({
compatibilityDate: '2024-11-01',
app: {
baseURL: '/your-path/'
}
})

export default defineNuxtConfig({
compatibilityDate: '2024-11-01',
app: {
baseURL: '/your-path/'
}
})

6. Duplication Bug: There was a known issue in earlier versions where the baseURL would duplicate on page reloads, which was fixed in Nuxt 3.17 Link to GitHub Issue. If you could share more details about your specific Nitro configuration and how exactly it's not working (e.g., 404 errors, wrong paths), I could provide more targeted assistance. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai7d ago
<@414109446810370049> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Snoozie
SnoozieOP7d ago
@kapa.ai can you please tell me how to set the baseURL for static generated app
kapa.ai
kapa.ai7d ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- For a static generated Nuxt app, setting the baseURL requires specific configuration to ensure assets are correctly referenced. Here's how to properly set it up: Setting baseURL for a Static Generated App In your nuxt.config.ts file, set the app.baseURL property:
export default defineNuxtConfig({
app: {
baseURL: '/your-subfolder/'
},
// For static generation
ssr: false,
nitro: {
prerender: {
routes: ['/'] // Define routes to prerender
}
}
})
export default defineNuxtConfig({
app: {
baseURL: '/your-subfolder/'
},
// For static generation
ssr: false,
nitro: {
prerender: {
routes: ['/'] // Define routes to prerender
}
}
})
This configuration will ensure that all your assets and links are prefixed with the specified base URL when the app is generated. Important Considerations 1. Environment Variables: Make sure you don't have any conflicting environment variables. Check your .env file for any NUXT_APP_BASE_URL entries that might override your configuration in nuxt.config.ts Link to GitHub Issue. 2. Asset Paths: When using app.baseURL, all asset paths in the generated HTML should automatically include the base URL prefix (e.g., /your-subfolder/_nuxt/asset.js). 3. Compatibility Date: If you're experiencing issues, try setting a recent compatibility date:
export default defineNuxtConfig({
compatibilityDate: '2024-11-01',
app: {
baseURL: '/your-subfolder/'
}
})

export default defineNuxtConfig({
compatibilityDate: '2024-11-01',
app: {
baseURL: '/your-subfolder/'
}
})

4. Deployment: When deploying your static site to a subfolder, make sure the server is configured to serve the application from that subfolder. 5. Manual Path Handling: For dynamic paths in your code that aren't automatically processed, you can use the useMaybeAbsolutePath composable (available in Nuxt 3.15+):
<script setup>
const maybeAbsolute = useMaybeAbsolutePath();
const path = maybeAbsolute('/images/logo.png'); // Will include baseURL
</script>

<script setup>
const maybeAbsolute = useMaybeAbsolutePath();
const path = maybeAbsolute('/images/logo.png'); // Will include baseURL
</script>

If you're still experiencing issues after applying these configurations, it might be related to specific modules or plugins in your project that need additional configuration. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai7d ago
<@414109446810370049> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Snoozie
SnoozieOP7d ago
@kapa.ai when I set the baseURL, and then use nuxi generate to build and then npx serve .output/public to try, when I open the website, the npx serve console logs 404 trying to get assets under /booking/_nuxt/..., if I check the .output folder, I don't have any /booking/ folder in it
kapa.ai
kapa.ai7d ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- I understand your issue with the static generation and baseURL configuration. This is a common problem when serving Nuxt static sites from subfolders. Based on the knowledge sources, here's what's happening: When you set app.baseURL: '/booking/' in your Nuxt config and run nuxi generate, Nuxt correctly generates HTML files that reference assets with paths like /booking/_nuxt/.... However, the actual files are still generated in the .output/public/_nuxt/ directory without the /booking/ prefix in the folder structure. This is why you're seeing 404 errors - the server is looking for assets in /booking/_nuxt/ but they actually exist in just /_nuxt/. Solutions: 1. When using npx serve: You need to specify the base path when serving:
npx serve .output/public --single --listen 3000 --cors

npx serve .output/public --single --listen 3000 --cors

The --single flag is important as it redirects all requests to index.html, allowing client-side routing to work. 2. For production deployment: You have a few options: - Use a server with URL rewriting capabilities (like Apache or Nginx) to map the /booking/_nuxt/ requests to /_nuxt/ - Manually copy the _nuxt folder to a /booking/_nuxt/ location on your server - Use a different deployment approach that supports subfolder hosting 3. Alternative approach: If you're opening the static files directly from the file system (double-clicking the HTML file), this won't work properly as noted in this Stack Overflow post. You need a proper server to handle the routing. This behavior is by design in Nuxt's static generation. The HTML files correctly reference assets with the baseURL prefix, but the actual file structure doesn't include this prefix in the directories. If you're deploying to a production environment, I recommend using a proper web server with URL rewriting capabilities rather than npx serve for testing subfolder deployments. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai7d ago
<@414109446810370049> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Snoozie
SnoozieOP7d ago
It seems to work but now it says the type mime is text/html instead of script @kapa.ai
kapa.ai
kapa.ai7d ago
Let me check the knowledge base for you again. One moment! :books: Sorry @Snoozie, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
Snoozie
SnoozieOP7d ago
@kapa.ai It seems to work but now it says the type mime is text/html instead of script
kapa.ai
kapa.ai7d ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- I understand you're now facing a MIME type issue where your JavaScript files are being served with a text/html MIME type instead of the correct JavaScript MIME type. This is a common issue when serving static Nuxt sites, especially from subfolders. Based on the knowledge sources, this could be related to caching or server configuration issues: 1. Caching Issues: There's a reported issue where Nuxt sites deployed on Cloudflare Pages experienced similar problems. As mentioned in this GitHub issue, purging the Cloudflare cache resolved the issue. This suggests that sometimes cached JS files with incorrect MIME types can cause problems. 2. Server Configuration: When using simple static file servers like npx serve, they might not correctly set the MIME types for all files, especially when dealing with subfolder configurations. Here are some potential solutions: 1. Clear Browser Cache: First, try clearing your browser cache completely to ensure you're not loading cached files with incorrect MIME types. 2. Server Configuration: If you're using npx serve, try using a more robust static file server that correctly sets MIME types:
npx http-server .output/public -p 3000 --cors

npx http-server .output/public -p 3000 --cors

3. Content-Type Headers: For production deployment, ensure your server is configured to serve JavaScript files with the correct MIME type (application/javascript or text/javascript). 4. Rebuild the Project: Sometimes a clean rebuild can help:
rm -rf .nuxt .output node_modules/.cache
npm install
npx nuxi generate

rm -rf .nuxt .output node_modules/.cache
npm install
npx nuxi generate

5. Check for Redirects: If your server is redirecting requests for JS files to HTML files (common in SPA setups), this would cause the MIME type issue. Make sure your server configuration doesn't redirect .js file requests. If you're testing locally, I recommend using Nuxt's built-in preview command instead of npx serve:
npx nuxi preview
npx nuxi preview
This should handle the MIME types correctly as it uses Nitro's server which is designed to work with Nuxt's output. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai7d ago
<@414109446810370049> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Snoozie
SnoozieOP6d ago
@kapa.ai I am testing locally but if I use npx nuxi preview I have 404 errors for all the generated js files
kapa.ai
kapa.ai6d ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- I understand you're having issues with npx nuxi preview showing 404 errors for your generated JS files when testing locally. This is a known issue when using a custom baseURL with static generation. Based on the knowledge sources, there are a few specific issues that could be causing this: 1. Nitro Build Directory Configuration: There might be a mismatch between your Nitro output directory configuration and where nuxi preview is looking for files. As mentioned in this GitHub PR, there was a fix for the preview command to respect custom Nitro output directory configurations. 2. 404 Errors for _nuxt/ Paths: There have been several issues related to 404 errors for assets under the _nuxt/ path in development and preview modes. Recent fixes in PR #31543 and PR #31646 addressed some of these issues. 3. Base URL Handling: When using a custom baseURL (like '/booking/'), the preview command might not be correctly handling the base URL prefix for static assets. This was addressed in PR #28482 for development mode, but similar issues could affect the preview command. Here are some potential solutions: 1. Check Nuxt Version: Make sure you're using the latest version of Nuxt, as many of these issues have been fixed in recent releases. 2. Try Direct Server Execution: Instead of using nuxi preview, try running the Nitro server directly:
node .output/server/index.mjs

node .output/server/index.mjs

3. Verify File Structure: Check that your JS files are actually being generated in the expected location within the .output directory. 4. Alternative for Testing: For testing a static site with a base URL, you might need to use a different approach:
cd .output/public && npx http-server -p 3000

cd .output/public && npx http-server -p 3000

Then manually navigate to <http://localhost:3000/booking/> in your browser. 5. Check for Environment Variables: As mentioned in our previous conversation, make sure you don't have any conflicting environment variables like NUXT_APP_BASE_URL that might be affecting the behavior. If these solutions don't work, you might need to consider a workaround for local testing, such as temporarily removing the baseURL configuration for local development and only adding it for production builds. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai6d ago
<@414109446810370049> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Snoozie
SnoozieOP6d ago
the .output folder doesn't contain the any folder named with the baseURL @kapa.ai
kapa.ai
kapa.ai6d ago
Let me check the knowledge base for you again. One moment! :books: Sorry @Snoozie, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
Snoozie
SnoozieOP6d ago
@kapa.ai the .output folder doesn't contain the any folder named with the baseURL
kapa.ai
kapa.ai6d ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- You're correct - the .output folder doesn't contain a folder named with your baseURL (like /booking/), and this is actually expected behavior. Based on the knowledge sources, this is how Nuxt's static generation with a custom baseURL works: When you set app.baseURL: '/booking/' in your Nuxt config and run nuxi generate, Nuxt generates: 1. HTML files in .output/public/ that reference assets with paths like /booking/_nuxt/... 2. The actual asset files in .output/public/_nuxt/ without creating a /booking/ folder structure This is confirmed in this GitHub issue where a user had the same problem. The HTML files correctly include the baseURL prefix in asset references, but the actual file structure doesn't mirror this. This creates a disconnect between: - Where the HTML is looking for assets (/booking/_nuxt/...) - Where the assets actually exist (just in /_nuxt/...) This is why you're seeing 404 errors when testing locally. The server is looking for files in a path that doesn't exist in the actual file structure. For local testing, you'll need to use a server with URL rewriting capabilities or manually create the folder structure. For production deployment, you would typically use a web server configuration (like Apache or Nginx) that can handle this routing situation. This behavior is by design in Nuxt's static generation system, though it can be confusing when first encountered. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai6d ago
<@414109446810370049> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Snoozie
SnoozieOP6d ago
@kapa.ai how can I use an env variable in the nuxt config
kapa.ai
kapa.ai6d ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- You can use environment variables in your Nuxt config file in several ways. Here's how to do it properly: Using Environment Variables in nuxt.config.ts The simplest way is to use process.env directly in your nuxt.config.ts file:
export default defineNuxtConfig({
runtimeConfig: {
apiSecret: process.env.NUXT_API_SECRET || '',
public: {
apiBase: process.env.NUXT_PUBLIC_API_BASE || '/api'
}
}
})
export default defineNuxtConfig({
runtimeConfig: {
apiSecret: process.env.NUXT_API_SECRET || '',
public: {
apiBase: process.env.NUXT_PUBLIC_API_BASE || '/api'
}
}
})
Nuxt CLI has built-in dotenv support, so it will automatically load variables from your .env file during development and build time Link to Nuxt docs. Important Considerations 1. Environment Variables vs. Runtime Config: - Environment variables are only available during build time in the nuxt.config.ts file - For runtime access, you should use the runtimeConfig option which can be accessed with useRuntimeConfig() 2. Naming Convention for Runtime Config: - Runtime config values are automatically replaced by matching environment variables at runtime - The environment variables must be prefixed with NUXT_ and use underscores for nested properties - Example: NUXT_API_SECRET for runtimeConfig.apiSecret 3. For Non-Runtime Config Parts: If you need to use environment variables in other parts of the config (like routeRules), you can use process.env directly:
export default defineNuxtConfig({
routeRules: {
'/backend/**': {
proxy: {
to: `${process.env.NUXT_BACKEND_URL}/**`,
headers: { accept: 'application/json' }
}
},
},
})

export default defineNuxtConfig({
routeRules: {
'/backend/**': {
proxy: {
to: `${process.env.NUXT_BACKEND_URL}/**`,
headers: { accept: 'application/json' }
}
},
},
})

4. Type Definitions: If you encounter a TypeScript error like "Cannot find name process", you need to install Node.js type definitions:
npm i --save-dev @types/node

npm i --save-dev @types/node

Then add node to the types field in your tsconfig Link to GitHub issue. 5. Environment-Aware Configuration: Nuxt also supports environment-specific configuration using the $production, $development, and $env keys Link to Nuxt docs. Remember that for local development, your .env file will be automatically loaded, but in production, you'll need to set the actual environment variables on your server or hosting platform. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai6d ago
<@414109446810370049> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?