Skip to content

Custom html attributes resolving #5098

Closed

Description

@putnikproj

Clear and concise description of the problem

In vanilla template, html file vite automatically resolves the src attribute of the img tag and other attributes. But what if my project has lazy-loading for images. I should write, for example, data-src attribute in the img tag. But vite doesn't understand, that this is a file's url, it doesn't resolve it, and doesn't put it into dist folder.

Suggested solution

Make some plugin or option, like webpack html-loader. It can "...specify which tags and attributes to process, filter them, filter urls and process sources starts with /".

Alternative

No response

Additional context

No response

Validations

Activity

  1. Yrobot commented on Jun 28, 2022

    @Yrobot

    Hi Guys,
    Is There any quick solutions for this? Is it possible for us to update the assetAttrsConfig in vite.config.ts?

    export const assetAttrsConfig: Record<string, string[]> = {
    link: ['href'],
    video: ['src', 'poster'],
    source: ['src', 'srcset'],
    img: ['src', 'srcset'],
    image: ['xlink:href', 'href'],
    use: ['xlink:href', 'href']
    }

  2. bluwy commented on Jun 28, 2022

    @bluwy
    Member

    Until #7643 is merged, you can use patch-package to make the tweaks yourself first.

  3. Yrobot commented on Jun 28, 2022

    @Yrobot

    Until #7643 is merged, you can use patch-package to make the tweaks yourself first.

    Thanks a lot

  4. added
    enhancement
    and removed on Dec 29, 2022
  5. froston commented on Jun 19, 2023

    @froston

    Hi, I'm having the same issue with data-preload="true" attribute needed for Azure B2C custom page layout.

    This:

    <link
      href="./style.css"
      rel="stylesheet"
      type="text/css"
      data-preload="true"
    />

    results in:

    <link rel="stylesheet" href="https://myurl/style-8f5b6679.css">

    Thanks!

  6. froston commented on Jul 3, 2023

    @froston

    Sharing a workaround for my issue:

    export default defineConfig({
      build: {
        rollupOptions: {
          input: {
            app: "template.html",
          },
        },
      },
      plugins: [
        {
          name: "add-data-preload-attr",
          transformIndexHtml(html) {
            return html.replace(
              new RegExp('rel="stylesheet"', "g"),
              'rel="stylesheet" data-preload="true"'
            );
          },
        },
      ],
    });
  7. ilyachernuha commented on Jul 15, 2023

    @ilyachernuha

    Sharing a workaround for my issue:

    export default defineConfig({
      build: {
        rollupOptions: {
          input: {
            app: "template.html",
          },
        },
      },
      plugins: [
        {
          name: "add-data-preload-attr",
          transformIndexHtml(html) {
            return html.replace(
              new RegExp('rel="stylesheet"', "g"),
              'rel="stylesheet" data-preload="true"'
            );
          },
        },
      ],
    });

    Hi! Thanks a lot @froston for sharing this! Really helps me in my project.

  8. locked and limited conversation to collaborators on Nov 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementfeat: html

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions