Skip to content

cacheBustingQueryParam breaks relative paths when manifestRelativePaths = true #461

@jchafik

Description

@jchafik

When specifying cacheBustingQueryParam, the private cacheBusting is invoked, unintentionally prefixing the resulting string with /. This disrupts relative URLs, causing incorrect path resolution.

protected cacheBusting(path: string): string {

    const url = new URL(path, "https://cache.busting");
    url.searchParams.set(paramParts[0], paramParts.slice(1).join("="));
    return url.origin === "https://cache.busting"
      ? url.pathname + url.search
      : url.toString();

You could potentially add something like:

protected cacheBusting(path: string): string {
  if (typeof this.options.cacheBustingQueryParam !== "string") {
    return path;
  }

  const paramParts = this.options.cacheBustingQueryParam.split("=");

  if (paramParts.length === 1) {
    return path;
  }

  const url = new URL(path, "https://cache.busting");
  url.searchParams.set(paramParts[0], paramParts.slice(1).join("="));

  if (url.origin !== "https://cache.busting") {
    return url.toString();
  }

  const resolvedPath = url.pathname + url.search;

  // Preserve relative paths
  if (!path.startsWith("/") && resolvedPath.startsWith("/")) {
    return resolvedPath.substring(1);
  }

  return resolvedPath;
}

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