Repository navigation
How to prevent URL transformation in CSS when using Vite et Express.js? #23851
|
Hello, export default function Main() {
...
return (
<html lang='fr'>
<script type='module' src='http://localhost:5173/@vite/client'></script>
<link rel='stylesheet' href='http://localhost:5173/src/public/styles/theme.css'/>
...
)}Here's my We can see here that to get I build the output folder with the same structure as my ...
rollupOptions: {
output: {
assetFileNames: (assetInfo) => {
for (let name of assetInfo.names){
if (name.endsWith('.css')) {
return 'assets/style/[name].[ext]'
}
if (name.includes('sans-latin')){
return 'assets/fonts/[name].[ext]'
}}
}}After running And the file can't find fonts any more. I've read the topic at #22646, I doubt that this applies to my case. How could I fix this ? Thanks. PS: Please don't bomb me with AI generated answer. |
Replies: 5 comments 2 replies
|
Vite rewrites every That URL is correct as long as the build output is served from the site root. If Express serves You can't turn the rewriting off, but you can make the URL match how Express serves the files: Option 1: relative URLs With a relative base, Vite makes CSS asset URLs relative to the CSS file. With your Option 2: set The URL then becomes Option 3: serve The existing One more thing: in production your template shouldn't link to |
|
Vite rewrites that CSS because you loaded it from Pick one of these. Don't mix them. Keep the CSS out of Vite Put <link rel="stylesheet" href="/styles/theme.css" />
Let Vite rewrite, and serve app.use(express.static('dist'))Then
If the file is imported or linked from |
|
One detail that may help narrow this down: is Vite treats the root |
|
I found a reponse here. export default defineConfig({
base: '',
})This solves the problem. The url is correctly written as : |
|
Vite defaults to Here are the standard ways to handle this depending on how you want your assets served: 1. Set a relative base ( If you want Vite to keep CSS asset references relative instead of absolute, set // vite.config.ts
import { defineConfig } from 'vite'
export default defineConfig({
base: './',
// your rollupOptions...
})Vite will calculate the relative distance between your CSS output ( 2. Align If Express mounts your build directory under a prefix, configure Vite's // vite.config.ts
export default defineConfig({
base: '/dist/', // or '/static/'
})// server.ts (Express)
app.use('/dist', express.static(path.join(__dirname, 'dist')))Vite will output 3. Bypass Vite bundling using root If you don't need Vite to hash or process the fonts, place them in Vite's root @font-face {
font-family: 'FontA';
src: url('/fonts/font-a.woff') format('woff');
}Vite ignores URLs starting with 4. Advanced URL rewriting via If you need finer control over how asset URLs are rewritten specifically for CSS: // vite.config.ts
export default defineConfig({
experimental: {
renderBuiltUrl(filename, { hostType }) {
if (hostType === 'css') {
// Generates relative URLs from the CSS file to the asset
return { relative: true }
}
},
},
})If this helps resolve the font URLs with your Express setup, please mark it as the answer so others running into this can find it! |
I found a reponse here.
The answer is using the base url. https://vite.dev/guide/build.html#public-base-path
This solves the problem. The url is correctly written as :
../fonts/font-a.wofffrom then on.