Skip to content

Commit

Permalink
Merge pull request #878 from GrabarzUndPartner/feature/fix-css-urls
Browse files Browse the repository at this point in the history
fix(preload): fix invalid css url paths and …
  • Loading branch information
ThornWalli authored Oct 30, 2023
2 parents c78083c + 961c4b2 commit 043845c
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/utils/preload.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { promises as fsPromises } from 'fs';
import { basename, dirname, join } from 'path';
import { basename, dirname, join, resolve } from 'path';
import { parseDocument } from 'htmlparser2';
import { load } from 'cheerio';
import { render } from 'dom-serializer';
Expand All @@ -16,7 +16,7 @@ export function optimizePreloads(moduleOptions, nuxt) {
}
}

nuxt.options.experimental.inlineSSRStyles = true;
nuxt.options.experimental.inlineSSRStyles = false;

nuxt.hook('nitro:init', nitro => {
nitro.hooks.hook('prerender:generate', async route => {
Expand Down Expand Up @@ -50,8 +50,15 @@ export function optimizePreloads(moduleOptions, nuxt) {
const filepath = join(publicDir, basename($el.attr('href')));
const fileContent = await fsPromises.readFile(filepath, 'utf-8');

Check warning on line 51 in src/utils/preload.mjs

View workflow job for this annotation

GitHub Actions / Install (ubuntu-latest, 19)

Found readFile from package "fs" with non literal argument at index 0

let urls = getUrlValues(fileContent);
urls = prepareUrls(urls, dir);

if (disableNuxtCritters) {
const css = fileContent.replace(/url\(.\//g, `url(${dir}/`);
const css = urls.reduce(
(result, [a, b]) => result.replace(a, b),
fileContent
);

$el.remove();
logger.info(
`Embed CSS File \`${basename($el.attr('href'))}\`; Route: \`${
Expand Down Expand Up @@ -85,3 +92,16 @@ export function optimizePreloads(moduleOptions, nuxt) {
});
});
}

function getUrlValues(css) {
return css.match(/url\(([^)]+)\)/g);
}

function prepareUrls(urls, relativeDir) {
return urls.map(url => {
return [
url,
`url(${resolve(relativeDir, url.replace(/^url\((.*)\)$/, '$1'))})`
];
});
}

0 comments on commit 043845c

Please sign in to comment.