Skip to content

Replace paths doesn't handle substrings correctly, and only replaces first instance of a url #22

@MalucoMarinero

Description

@MalucoMarinero

Hey there, I find staticify real handy in a prebuild and deploy scenario, there's just a few issues with replacePaths that mean it doesn't currently work completely for my use case:

  1. Substrings are often incorrectly replaced
@font-face {
  font-family: 'Portrait Web';
  src: url('/assets/font-portrait/Portrait-Regular-Web.eot');
  src: url('/assets/font-portrait/Portrait-Regular-Web.eot?#iefix') format('embedded-opentype'),
       url('/assets/font-portrait/Portrait-Regular-Web.woff2') format('woff2'),
       url('/assets/font-portrait/Portrait-Regular-Web.woff') format('woff');
  font-weight:  400;
  font-style:   normal;
  font-stretch: normal;
}

Consider CSS that looks like this, if Object.keys returns the woff path before the woff2 path, the hash for the .woff path will actually end up on the .woff2, meaning a broken link in production.

Should be easily fixed with a length sort of Object.keys(versions), so longer strings get handled before shorter strings.

  1. String.replace only replaces the first instance when used with strings.
    Usually not an issue in my builds, but if a file is referenced more than once replacePaths won't currently pick it up. Better to turn it into a new RegExp(url, "g"), the global flag will ensure it gets everything.

I think this code should fit the bill but I didn't want to create a pull request and be presumptive, let me know if you want me to create one.

    const replacePaths = fileContents => {
        return Object.keys(versions).sort(function(a, b) { return b.length - a.length })).reduce((f, url) => {
            return f.replace(new RegExp(url, "g"), getVersionedPath(url));
        }, fileContents);
    };

Cheers!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions