-
Notifications
You must be signed in to change notification settings - Fork 7
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
wrong baseURL in inline CSS #242
Comments
Hey @JohnCampionJr! I'm here to help you with your issue. I'm a bot that can assist you with bug fixes, answer questions, and guide you on becoming a contributor. I found a similar closed issue: Nuxt baseURL ignored when resolving cached font path. This issue was referenced by other discussions and commits, such as nuxt/fonts does not honour the nuxt baseUrl setting and Using BaseUrl breaks fonts [1]. To fix the issue where setting source.url = nuxt.options.dev
? joinRelativeURL(nuxt.options.app.baseURL, assetsBaseURL, file)
: joinURL(assetsBaseURL, file) To ensure that the font URLs are correctly prefixed with const assetsBaseURL = joinURL(nuxt.options.app.baseURL, options.prefix || '/_fonts'); This change ensures that Here is the updated code snippet: export function setupPublicAssetStrategy(options: ModuleOptions['assets'] = {}) {
const nuxt = useNuxt();
const assetsBaseURL = joinURL(nuxt.options.app.baseURL, options.prefix || '/_fonts');
const renderedFontURLs = new Map<string, string>();
function normalizeFontData(faces: FontFaceData | FontFaceData[]): NormalizedFontFaceData[] {
const data: NormalizedFontFaceData[] = [];
for (const face of Array.isArray(faces) ? faces : [faces]) {
data.push({
...face,
unicodeRange: face.unicodeRange === undefined || Array.isArray(face.unicodeRange) ? face.unicodeRange : [face.unicodeRange],
src: (Array.isArray(face.src) ? face.src : [face.src]).map((src) => {
const source = typeof src === 'string' ? parseFont(src) : src;
if ('url' in source && hasProtocol(source.url, { acceptRelative: true })) {
source.url = source.url.replace(/^\/\//, 'https://');
const file = [
filename(source.url.replace(/\?.*/, '')).replace(/^-+/, ''),
hash(source) + (extname(source.url) || formatToExtension(source.format) || ''),
].filter(Boolean).join('-');
renderedFontURLs.set(file, source.url);
source.originalURL = source.url;
source.url = joinURL(assetsBaseURL, file);
}
return source;
}),
});
}
return data;
}
// Register font proxy URL for development
addDevServerHandler({
route: assetsBaseURL,
handler: lazyEventHandler(async () => {
return eventHandler(async (event) => {
const filename = event.path.slice(1);
const url = renderedFontURLs.get(event.path.slice(1));
if (!url) {
throw createError({ statusCode: 404 });
}
const key = 'data:fonts:' + filename;
// Use storage to cache the font data between requests
let res = await storage.getItemRaw(key);
if (!res) {
res = await fetch(url).then(r => r.arrayBuffer()).then(r => Buffer.from(r));
await storage.setItemRaw(key, res);
}
return res;
});
}),
});
if (nuxt.options.dev) {
nuxt.options.routeRules ||= {};
nuxt.options.routeRules[joinURL(assetsBaseURL, '**')] = {
cache: {
maxAge: ONE_YEAR_IN_SECONDS,
},
};
}
nuxt.options.nitro.publicAssets ||= [];
const cacheDir = join(nuxt.options.buildDir, 'cache', 'fonts');
nuxt.options.nitro = defu(nuxt.options.nitro, {
publicAssets: [{
dir: cacheDir,
maxAge: ONE_YEAR_IN_SECONDS,
baseURL: assetsBaseURL,
}],
ignore: [`!${join(cacheDir, '**/*')}`],
prerender: {
ignore: [assetsBaseURL],
},
} satisfies NitroConfig);
// TODO: refactor to use nitro storage when it can be cached between builds
nuxt.hook('nitro:init', async (nitro) => {
if (nuxt.options.dev) {
return;
}
nitro.hooks.hook('rollup:before', async () => {
await fsp.rm(cacheDir, { recursive: true, force: true });
await fsp.mkdir(cacheDir, { recursive: true });
let banner = false;
for (const [filename, url] of renderedFontURLs) {
const key = 'data:fonts:' + filename;
// Use storage to cache the font data between builds
let res = await storage.getItemRaw(key);
if (!res) {
if (!banner) {
banner = true;
logger.info('Downloading fonts...');
}
logger.log(chalk.gray(' ├─ ' + url));
res = await fetch(url).then(r => r.arrayBuffer()).then(r => Buffer.from(r));
await storage.setItemRaw(key, res);
}
await fsp.writeFile(join(cacheDir, filename), res);
}
if (banner) {
logger.success('Fonts downloaded and cached.');
}
});
});
return { normalizeFontData };
}
const ONE_YEAR_IN_SECONDS = 60 * 60 * 24 * 365; This should resolve the 404 errors for font links when |
Resolved in v0.8.0. |
I'm not seeing a change in 0.8.0. Same symptoms. It's not the link to the font, it has the baseUrl. It is the actual location of the font file from the server |
@JohnCampionJr Would you be able to provide a reproduction? 🙏 |
Running it gets this console error Fonts are here though (and should not be) - note the missing baseurl |
@JohnCampionJr I believe this is an issue with prerendered pages only - can you confirm? |
@danielroe I'm not using prerendering in my main app that experiences this bug. Disabled this, no effect:
tried |
@danielroe I'm sorry to report this, but the bug persists in v0.9.0. Fonts continue to be served without the baseUrl: Loads the font just fine. |
@JohnCampionJr I think I totally misunderstood your issue. So the baseURL is being correctly rendered but the fonts aren’t served there? how are you running/deploying your built site? (Not with |
@danielroe That is correct. I'm just using nuxt last dev server. It's pretty easy to see the bug with the reproduction. Just run it and look at the console. |
sorry for the back and forth on this one - I thought you were talking about production CSS. hopefully should be resolved in v0.9.1 🙏 |
@danielroe confirming it is fixed - thank you so much!!! |
at last!! 🎉 |
Related to #96; this still is broken.
If I set
baseUrl
to/subapp
then Nuxt Fonts uses a link like:http://localhost:5001/subapp/_fonts/rP2rp2ywxg089UriCZaSExd86J3t9jz86Mvy4qCRAL19DksVat9XCm3w-ByvzuLy2fa.woff
However this results in a 404, but this link works (note no base url)
http://localhost:5001/_fonts/rP2rp2ywxg089UriCZaSExd86J3t9jz86Mvy4qCRAL19DksVat9XCm3w-ByvzuLy2fa.woff
The text was updated successfully, but these errors were encountered: